简体   繁体   中英

Java Command Prompt Exit

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM