2

I have a program that can ssh into a remote host and remotely execute commands after that. Commands like mkdir and cd work but when I try to execute the command sudo su - username the program just hangs. I was wondering if there's anything missing/wrong in my code.

JSch jSch = new JSch();
Channel channel = null;
Session session = null;
InputStream in = null;        
String  username;
OutputStream  os  = null;;

try {   
    Properties conf = new Properties();
    conf.put("StrictHostKeyChecking", "no");

    jSch.addIdentity("id_rsa");
    jSch.setConfig(conf);
    session = jSch.getSession("username", "hostname", 22);      

    String cmd = "mkdir test";
    session.connect();   //   creating the ssh connection        

    channel = (ChannelExec) session.openChannel("exec");        
    ((ChannelExec)channel).setCommand(cmd);
    channel.setInputStream(null);
    in = channel.getInputStream(null);        
    channel.connect();  

    byte[] tmp = new byte[1024];        
    while (true) { 
        while (in.available() > 0) {
            int i = in.read(tmp, 0, 1024);
            if (i < 0) {
                break;
            }
        }
        if (channel.isClosed()) {
            break;
        }      
        try {
            Thread.sleep(1000);  // to wait for long running process ..                
        } catch (Exception ee) {
        }
        String value = new String(tmp);            
        System.out.println("input stream " + value);
    }     
}catch(Exception  e){
    e.printStackTrace();
}finally{
    channel.disconnect();
    session.disconnect();
    if(in!=null)
    in.close();
}

Also, I need to ssh from this host to another host after I sudo, so basically I need to ssh to a remote host via a gateway kind of a thing and then connect to a database, once this problem gets fixed.

Any light on this will be greatly appreciated.

Thanks.

Arnold Spence
  • 21,942
  • 7
  • 74
  • 67
dehsams123
  • 251
  • 2
  • 7
  • 18
  • 1
    http://serverfault.com/questions/107187/sudo-su-username-while-keeping-ssh-key-forwarding – blearn Jun 27 '12 at 20:22
  • You have a `catch` which does nothing, why not print the stack trace because it can be the one possibly causing "my program hangs" and you're not catching the error raised – Adel Boutros Jun 27 '12 at 20:23
  • The Program just hangs up after executing the sudo su - username command.. any solution for that – dehsams123 Jun 27 '12 at 20:29
  • tried that, theres not exception thrown. the program doesnt exit from while(true) block. So if I insert a System.out.println("here") statement, it keeps printing that. – dehsams123 Jun 27 '12 at 20:34
  • Seems to me that that first `while (true)` is never quit in this code? The `break` statement will only exit inner while loop. – Maarten Bodewes Jun 27 '12 at 20:34
  • No, The channel.isClosed() condition if met, executes the break statement, which basically means that channel.isClosed() always evaluates to false. Not being able to figure out why. – dehsams123 Jun 27 '12 at 20:39
  • Furthermore, as the inputstream of the SSH session itself is not closed, waiting for a -1 on reading bytes will not work I guess, you may want to parse the response. – Maarten Bodewes Jun 27 '12 at 20:40
  • I think I answered that one before you actually posted the question :) – Maarten Bodewes Jun 27 '12 at 20:41
  • @owlstead : I need to sudo su - user and then ssh into another host. Can you give some examples on how to go about it. – dehsams123 Jun 27 '12 at 20:55
  • Nah, I can only show you why the thing hangs in this case, and it is because you are making the wrong assumptions on the input stream - it's not closed just because you executed a command remotely. You can do two things: execute a single command on the remote host by supplying it to the `ssh` command line, or you can parse the output of the ssh command, possilby waiting for the command prompt for instance, keeping in mind the comment by blearn. – Maarten Bodewes Jun 27 '12 at 21:02
  • This might help you http://stackoverflow.com/questions/14220550/jsch-sudo-su-command-tty-error – madhuram2468 May 03 '14 at 17:02
  • Check [this][1] , it might help you !! [1]: http://stackoverflow.com/questions/14220550/jsch-sudo-su-command-tty-error – madhuram2468 May 03 '14 at 17:03

1 Answers1

1

The sudo command will require the pty. Refer to http://www.jcraft.com/jsch/examples/Sudo.java.html for doing sudo on the exec channel, and as for jump-hosts, refer to http://www.jcraft.com/jsch/examples/JumpHosts.java.html

ymnk
  • 1,145
  • 7
  • 7
  • I need to change to user deploy and then ssh into the next server. However when I execute sudo su - username command the progam doesnt terminate. But other commands like mkdir works just fine. So I need to figure out a way to first ssh into host1 then change to user 'username' and then ssh into host2.. :( – dehsams123 Jun 28 '12 at 14:14