use of com.trilead.ssh2.StreamGobbler in project maven-scm by apache.
the class ExtConnection method open.
/**
* {@inheritDoc}
*/
public void open() throws AuthenticationException, CommandAbortedException {
connection = new Connection(host, port);
try {
// TODO: connection timeout?
connection.connect();
} catch (IOException e) {
String message = "Cannot connect. Reason: " + e.getMessage();
throw new AuthenticationException(message, e, message);
}
File privateKey = getPrivateKey();
try {
boolean authenticated;
if (privateKey != null && privateKey.exists()) {
authenticated = connection.authenticateWithPublicKey(userName, privateKey, getPassphrase());
} else {
authenticated = connection.authenticateWithPassword(userName, password);
}
if (!authenticated) {
String message = "Authentication failed.";
throw new AuthenticationException(message, message);
}
} catch (IOException e) {
closeConnection();
String message = "Cannot authenticate. Reason: " + e.getMessage();
throw new AuthenticationException(message, e, message);
}
try {
session = connection.openSession();
} catch (IOException e) {
String message = "Cannot open session. Reason: " + e.getMessage();
throw new CommandAbortedException(message, message);
}
String command = "cvs server";
try {
session.execCommand(command);
} catch (IOException e) {
String message = "Cannot execute remote command: " + command;
throw new CommandAbortedException(message, message);
}
InputStream stdout = new StreamGobbler(session.getStdout());
InputStream stderr = new StreamGobbler(session.getStderr());
stderrReader = new BufferedReader(new InputStreamReader(stderr));
setInputStream(new LoggedDataInputStream(stdout));
setOutputStream(new LoggedDataOutputStream(session.getStdin()));
}
use of com.trilead.ssh2.StreamGobbler in project new-cloud by xie-summer.
the class SSHTemplate method processStream.
/**
* 从流中获取内容
* @param is
*/
private void processStream(InputStream is, LineProcessor lineProcessor) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new StreamGobbler(is)));
String line = null;
int lineNum = 1;
while ((line = reader.readLine()) != null) {
try {
lineProcessor.process(line, lineNum);
} catch (Exception e) {
logger.error("err line:" + line, e);
}
lineNum++;
}
lineProcessor.finish();
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
close(reader);
}
}
use of com.trilead.ssh2.StreamGobbler in project warn-report by saaavsaaa.
the class LinuxExecUtil method execute.
public void execute(final String commandText) {
try {
System.out.println("exec : " + commandText);
Session sess = conn.openSession();
sess.execCommand(commandText);
System.out.println("Here is some information about the remote host:");
InputStream standardOut = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(standardOut));
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
System.out.println("ExitCode: " + sess.getExitStatus());
sess.close();
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(2);
}
}
Aggregations