如何通过 ssh 在远程机器上运行应用程序?
我尝试使用 JSCH,这样:
Properties props = new Properties();
props.put("StrictHostKeyChecking", "no");
String host = "111.111.11.111";
String user = "myuser";
String pwd = "mypass";
// int port = 22;
JSch jsch = new JSch();
Session session = jsch.getSession(user, host);
session.setConfig(props);
session.setPassword(pwd);
session.connect();
System.out.println(session.getServerVersion());
System.out.println("Conected!");
// String command = "ps -ef;date;hostname";
String command = "myapplication someFileAsParameter";
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// channel.setInputStream(System.in);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
我有这个:
bash:myapplication:找不到命令
当尝试通过 shell 执行此命令时,工作正常。
不一定需要是JSCH,可以是任何东西。