java - JSch: Run command after Multi-Level ssh -


i using jsch run commands after multi-level ssh:

public static void main(string[] args) {      string user="user0";     string ip="ip0";     int port=22;     string password="password0";             jsch jsch= new jsch();             session session=null;             channelexec channel=null;             try {                 session=(jsch.getsession(user, ip, port));                 session.setconfig("stricthostkeychecking", "no");                 session.setpassword(password);                 session.connect();                 string dir=reomte_dir;                   string cmd1=somecomplexcommand;                 string cmd2=somemorecomplexcommand;                 channel = (channelexec) session.openchannel("exec");                 channel.setinputstream(null);                 channel.setcommand("ssh user1@ip1_passwordlesslogin;ssh user2@ip2_passwordlesslogin; "+cmd1+" ; "+cmd2+" ;");                 channel.setpty(true);                 channel.connect(4000);                 string res = null;                     bufferedreader input = new bufferedreader(new inputstreamreader(channel.getinputstream()));                     bufferedreader error = new bufferedreader(new inputstreamreader(channel.geterrstream()));                     if ((res = error.readline()) == null) {                         res = input.readline()+input.readline()+input.readline()+input.readline();                     } else {                          res = "-1";                     }                 system.out.println("result:"+res);              } catch (jschexception e) {                 e.printstacktrace();             }catch (ioexception e) {                 e.printstacktrace();             }finally {                 channel.disconnect();                 session.disconnect();             }                } 

but doesn't give desired result.

infact channel.getinputstream() hangs. if remove multilevel ssh, works fine! doing wrong??

i got hint from: multi-level ssh login in java , multiple commands using jsch not able code running.

your command wrong.

your command execute first command ssh user1@ip1_passwordlesslogin.

and wait finish before executing second command.

  • the first ssh command never finishes, forever keep waiting user type commands.
  • even if first ssh ever finished, second ssh executed on initial host, not 1 ip1.

you need this:

ssh user1@ip1_passwordlesslogin ssh user2@ip2_passwordlesslogin "<cmd1> ; <cmd2>" 

this tells first ssh execute second ssh on ip1; , second ssh execute <cmd1> ; <cmd2> on ip2.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -