11

I am trying to make a java program that will set up a SSH connection for me on my macbook. It prompts me for the username, and then the IP address, then it is supposed to do "ssh username@ip".

Below is my code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class SSH {
    public static void main(String[] args) throws Exception {
    boolean rep = true;
    String username = (null);
    String IPAdress = (null);
    while (rep) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Username:  ");
        username = scanner.next();
        System.out.print("\nIP Adress:  ");
        IPAdress = scanner.next();
        System.out.println("\n\nIs this correct?\nUsername:  " + username + "\nIP Adress:  " + IPAdress + "\nY/N");
        char responce = scanner.next().charAt(0);

        if (responce == 'Y' || responce == 'y') {
            rep = false;
            scanner.close();
        } else if (responce == 'N' || responce == 'n') {

        } else {
            Error displayErrorMessage = new Error();
            displayErrorMessage.displayError();
        }
    }
    String SSHStartup = username + "@" + IPAdress;
    System.out.println("Running command:  ssh " + SSHStartup);
    String[] command = { "/bin/bash, -c , ssh " + SSHStartup };
    Process p = Runtime.getRuntime().exec(command);
    p.waitFor();
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = "";
    StringBuffer output = new StringBuffer();
    while ((line = reader.readLine()) != null) {
        output.append(line + "\n");
    }
}
}

I know, its messy, and now well indented, but instead of executing the command, it gives me this:

Exception in thread "main" java.io.IOException: Cannot run program "/bin/bash, -c , ssh root@(ip here)": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:485)
at SSH.main(SSH.java:32)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:248)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 3 more

For the purpose of this post, I have removed the IP address, but when I compile and run it, I try the actual one, and it gives me the same error.

Help?

3
  • What does ls -l /bin/bash returns? Commented Apr 27, 2016 at 21:40
  • @Tunaki, it returns -r-xr-xr-x 1 root wheel 628496 Dec 2 22:36 /bin/bash Commented Apr 27, 2016 at 22:30
  • bugs.eclipse.org/bugs/show_bug.cgi?id=509628 , there is bug with Eclipse since long time. I could get this resolve by mentioning full path of adb in the programme itself. String[] temp = { "/Users/vikram-anna/Library/Android/sdk/platform-tools/adb", "start-server" }; p = Runtime.getRuntime().exec(temp); Commented Dec 22, 2016 at 9:26

1 Answer 1

23

This is how you're invoking bash:

String[] command = { "/bin/bash, -c , ssh " + SSHStartup };
Process p = Runtime.getRuntime().exec(command);

Your command array contains a single value, namely the string "/bin/bash, -c , ssh ...". Java is trying to use the entire string as the name of a single file and failing to execute a file with that name.

You should construct the command as an array containing the command and its arguments as a sequence of strings, instead of a single string, as follows:

String[] command = { "/bin/bash", "-c", "ssh " + SSHStartup };
Sign up to request clarification or add additional context in comments.

3 Comments

What would you suggest I do? Should I create a file with the command "/bin/bash", "-c", "ssh " + SSHStartup in it?
Do you see the last line in my answer? The second code block? That's what you should do.
Sorry, I didn't know that two pairs of "" could cause me over an hour of frustration. Thanks for solving my issue! :)

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.