0

I am trying to run a Java program to shell out commands on a remote (Linux) machine. I can get the putty.exe to run and then connect to the machine using SSH keys. But am not able to run the actual commands such as "bash" "ps-ef" or "ls -la". Currently using the Java runtime.exec, not sure if using the java.lang.ProcessBuilder would help? What am I doing wrong ? Any help/guidance would be greatly appreciated.. Thanks in advance

package hello;

import java.io.*;
public class RuntimeExample {




     public static void main(String args[]) throws IOException {



    try{     


    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec(new String[]{"C:\\Users\\yky90455\\Desktop\\putty.exe","[email protected]","bash", "ps -ef"});


    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line;

    System.out.printf("Output of running the command is:");

    while ((line = br.readLine()) != null) {
        System.out.println(line);

      }                                                             

    } catch (Exception e) {
        e.printStackTrace();
    }
}   

}
4
  • 5
    Why don't you use a SSH library for that, instead of using putty? Commented Sep 19, 2014 at 8:33
  • 1
    The whole point of Java is abstract away from the OS. Always search for a platform independent alternative before resorting to calling native commands. In your example simply use an SSH implementation for Java - such as sshj. Commented Sep 19, 2014 at 8:44
  • Thanks for your quick response. I will check out sshj and Jsch. Commented Sep 19, 2014 at 8:53
  • Begs the next question, possibly another thread. What is the best SSH library that will facilitate using SSH keys to connect to a remote linux machine and execute commands Commented Sep 19, 2014 at 9:52

4 Answers 4

2

try Jsch From here to get the shell scrips executed from Java to some remote Linux machine. I have worked on this and it was really fun.although you may find little shortage of docs for understanding this but you can overcome that easily.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your quick response. I will check out sshj and Jsch.
@joule_wonder: Welcome.But make sure to mark this answer if you go for Jsch :)
0

Also consider ExpectJ which is a wrapper around TCL Expect. The project does not appear to have any active development since mid 2010, but I have used it for SSH in the past.

http://expectj.sourceforge.net/apidocs/expectj/SshSpawn.html

Comments

0

Thanks for all your answers. I tried Ganymed SSH-2 library. It works well for the basic commands on the remote machine. I will have to explore other APIs in case I run into any limitation with SSH-2.

Comments

-1
public class triggerPutty {
    public static void main(String[] a) {
        try {
            String command = "putty.exe [email protected] -pw password -m C:\\containing_comman.txt";
            Runtime r = Runtime.getRuntime();
            Process p = null;
            p = r.exec(command);
            p.waitFor();
            p.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }    
    }
}

-m helps to run your command from that file.
You can keep N number of commands in that file.. ## Heading ##

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.