protected void creation() throws Exception {
JSch jsch = new JSch()
session = jsch.getSession(userName, host, port)
session.setPassword(password)
Properties config = new Properties()
config.put("StrictHostKeyChecking", "no")
session.setConfig(config)
session.setTimeout(CONNECT_TIMEOUT)
session.setConfig("PreferredAuthentications", "password,keyboard-interactive")
session.setServerAliveInterval(1000 * 60 * 2)
session.connect()
}
public String sendCommand(String command) throws Exception {
if(!isConnected())
throw new JSchException("Session is not connected, command exec faild.")
final ChannelExec exec = (ChannelExec)session.openChannel("exec")
ByteArrayOutputStream out = new ByteArrayOutputStream()
exec.setCommand(command)
exec.setOutputStream(out)
exec.setExtOutputStream(out)
exec.connect()
final Thread thread = new Thread() {
public void run() {
while(!exec.isEOF()) {
try { Thread.sleep(500L)} catch(Exception e) {}
}
}
}
thread.setDaemon(true)
thread.start()
thread.join(EXEC_TIMEOUT)
thread.interrupt()
if(thread.isAlive()) {
throw new JSchException("Exec Time Out Error")
} else {
try {
exec.disconnect()
out.close()
} catch (Exception e) {
}
byte[] lens = out.toByteArray()
String result = new String(lens, charset)
if(result.startsWith("bash") &&result.indexOf("command not found") != -1)
return ""
return result
}
}
ssh是什么呢?欢迎分享,转载请注明来源:内存溢出
评论列表(0条)