1

I am trying to execute multiple commands on remote machine though SSH using the JSch library. i am almost there but stuck in one command.

I am using two remote machines lets say RM1 and RM2

Able to do following steps,

  1. Able to connect to remote machine RM1.
  2. Able to execute single/multiple commands on RM1 (followed this https://www.journaldev.com/246/jsch-example-java-ssh-unix-server and https://stackoverflow.com/a/5831846/8773024 ).
  3. Able to connect to remote machine RM1 and again connect to another remote machine RM2.

Not able to do this step, 1. Once i will be connected to remote machine RM2 after this not able to execute any command.

Please let me know if anyone can help me on this.

I tried this code:

String host="IP";
String user="username";
String password="password";
String command1="pwd";
String command2="ssh -tt user@ip";
String command3="pwd";

java.util.Properties config = new java.util.Properties(); 
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session=jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");        

Channel channel1=session.openChannel("exec");
((ChannelExec)channel1).setCommand(command1;command2;command3);

channel1.setInputStream(null);
((ChannelExec)channel1).setErrStream(System.err);            
InputStream in1=channel1.getInputStream();
channel1.connect();
byte[] tmp=new byte[1024];
while(true){
  while(in1.available()>0){
    int i=in1.read(tmp, 0, 1024);
    if(i<0)break;
    System.out.print(new String(tmp, 0, i));
  }
  if(channel1.isConnected()){
    System.out.println("exit-status: "+channel1.getExitStatus());
    break;
  }
  try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
System.out.println("DONE");
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
DmUser
  • 41
  • 7
  • Did you try executing `pwd;ssh -tt user@ip;pwd` in a normal SSH terminal client connected to the RM1? Does it do what you want? => It does not! – So why do you expect it to work in Java? – Martin Prikryl Feb 07 '20 at 10:05
  • Btw, to do the "jump", you should use port forwarding (See JSch [`JumpHosts` example](http://www.jcraft.com/jsch/examples/JumpHosts.java.html)), instead of executing `ssh` on the jump server. – Martin Prikryl Feb 07 '20 at 10:06
  • @MartinPrikryl manually these command does not work on SSH terminal; i had tried this " pwd;ls;pwd; " in my code and it worked, so i thought of doing some workaround. – DmUser Feb 07 '20 at 10:40
  • @MartinPrikryl I checked link shared by you and followed same, but now i am getting error "Auth fail" Please let me know if you can help me in this? – DmUser Feb 27 '20 at 05:49
  • @MartinPrikryl hey, i have added new code as well in my question, please check and let me know? I am new in java language, so i kept this code as simple as possible. – DmUser Feb 27 '20 at 08:45
  • Where are you getting the exception? Show us [JSch log file](https://stackoverflow.com/q/47411185/850848) for both sessions. + How are you authenticating from RM1 to RM2 when connecting manually? – Martin Prikryl Feb 27 '20 at 08:57
  • @MartinPrikryl I have added log as well. I have added comment ("code is failing in this step") where my code is failing please check once. actually i had one doubt, do i need to add RM1 public key? – DmUser Feb 27 '20 at 08:59
  • I am able to connect to connect to RM1, and then ssh to RM2 and then run multiple commands on RM2 (manually) – DmUser Feb 27 '20 at 09:04
  • But how are you authenticating to RM2? Show us output of `ssh -v user@rm2`. Are you are using public key authentication? Then you need to specify the private key in your code. – Martin Prikryl Feb 27 '20 at 09:19
  • Should i add private key of RM1? can i take reference from here https://www.codota.com/code/java/methods/com.jcraft.jsch.JSch/addIdentity?snippet=5ce6b2bc7e03440004e9b48d. i tried following : String privKey = xyz //(priv key of RM1); jsch.addIdentity(privKey ); session.connect(); // connecting to RM2 but it failed with error " system can not find the path specified" – DmUser Feb 27 '20 at 10:58
  • I guess that the file does not exist on your *local* machine, right? – Martin Prikryl Feb 27 '20 at 10:59
  • @MartinPrikryl previous error got fixed. I added private key step in my code, but still getting auth fail error. i have updated my code as well as error. please check. – DmUser Feb 27 '20 at 16:22
  • What is *"priv_k of RM1"*? You need to add a private key for authentication to RM2. – Martin Prikryl Feb 27 '20 at 20:47
  • @MartinPrikryl private key of which machine ? localhost, RM1 or RM2 ? i have tried with private key of RM1 machine and localhost. – DmUser Feb 28 '20 at 07:11
  • Again, *"private key **for authentication to RM2**"*. – Martin Prikryl Feb 28 '20 at 07:13
  • @MartinPrikryl RM2 was down thats why i was not able to connect. I tried with some other remote machine and i was able to connect and print "ls" output, even without using private key. Thanks you so much Martin for following up. :) :) – DmUser Mar 02 '20 at 10:25

1 Answers1

0

Try executing pwd;ssh -tt user@ip;pwd in a normal SSH terminal client connected to the RM1. It won't work, So it won't work in Java either.

To do the "jump", you should use port forwarding (See JSch JumpHosts example), instead of executing ssh on the jump server.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992