67,513
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args){
int returncode =-10;
FtpToSever fts = new FtpToSever();
fts.setIp("172.23.19.127");
fts.setUser("root");
fts.setPassword("`123qwer");
fts.connect();
//fts.upload(sendFiles);
//returncode = fts.execute("ls");
returncode = fts.execute("java -jar /home/eam/TestCommand.jar \n");
System.out.println(">>>>>>>>>>>>>>>"+returncode);
fts.loginOut();
} /**
* 执行shell命令
* */
public int execute(String command) {
int returnCode = 0;
try {
// Create and connect session.
// Create and connect channel.
excechannel = (ChannelExec)sshSession.openChannel("exec");
excechannel.setCommand(command);
// excechannel.setInputStream(null);
BufferedReader input = new BufferedReader(new InputStreamReader(excechannel
.getInputStream()));
excechannel.connect();
System.out.println("The remote command is: " + command);
// Get the output of remote command.
String line;
while ((line = input.readLine()) != null) {
stdout.add(line);
}
input.close();
// Get the return code only after the channel is closed.
if (excechannel.isClosed()) {
returnCode = excechannel.getExitStatus();
}
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return returnCode;
}