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: コマンドが見つかりません
このコマンドをシェルで実行してみると、うまくいきます。
必ずしも JSCH である必要はなく、何でもかまいません。