I am writing a java code to run PuTTY
by using the plink
version of it which is a command line implementation of PuTTY
.
I used the Process Builder to run it from cmd and the connection from plink
gets established. But I wasn't able to send the linux commands to it to run on the server and after checking it's working in an actual command prompt, I found out that after the command to open plink
, there is a new process created. So I am guessing to make my commands work on plink I need to find that process and then send commands to it.
This is my code so far which establishes the connection using plink.
import java.io.*;
public class RunCmdCommandsStackOverflow {
public static void main(String[] args) throws Exception {
String cmd1 = new String("plink -ssh hostname -P 2222 -l username -pw password");
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", cmd1);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) { break; }
System.out.println(line);
}
}
}
I want to run linux commands on the server after creating a connection in the java code.
I have already tried to create a connection using the Jsch
package but the security settings on my server seem to prohibit me from accessing it using that. PuTTY
works fine for this and plink
is also working fine in a regular cmd
window.