Reputation: 303
I have a short question concerning the Runtime.getRuntime.exec("") command in java.
I am trying to make a tunnel to a gateway computer via SSH:
String tunnel = "C:\\Program Files\\PuTTY\\putty.exe -ssh -X -P " + localPort + " " + tempUsername + "@" + localIP
+ " -pw " + tempPassword + " -L " + tunnelPort + ":" + gatewayName + ":"+gatewayPort;
Runtime.getRuntime().exec(tunnel);
This bit of code works properly except the annoying fact that a command prompt appears.
Now I tried to exit the prompt after executing the code:
String tunnel = "C:\\Program Files\\PuTTY\\putty.exe -ssh -X -P " + localPort + " " + tempUsername + "@" + localIP
+ " -pw " + tempPassword + " -L " + tunnelPort + ":" + gatewayName + ":"+gatewayPort;
String cmdCommands [] = {tunnel, "exit"};
Runtime.getRuntime().exec(cmdCommands);
Is it possible to close the command prompt in a similar way like I do or are there better ways? (This code doesnt work)
Upvotes: 0
Views: 1410
Reputation: 5094
You'll need to either use an actual SSH library directly instead of putty, as in the comments, or capture the IO streams of the exec
Process p = Runtime.getRuntime().exec(cmdCommands);
InputStream is = p.getInputStream();
OutputStream os = p.getOutputStream();
os.write("exit\n");
It's generally a bad idea to hard code \n, for platform reasons, but you get the idea. Also, you will need to pick the right OutputStream. There are several subclasses(buffered etc.) which may be useful.
Upvotes: 1