1

I'm building an app that needed to open a new SSH connection for the user, and then send automatically a command. The app need to run on Windows, i thought about using putty to the SSH client and then send the command but the command don't sent to putty. Anyone have any idea how can i implement this?

This is my code:

String command = puttyPath + " -ssh user@localhost"
process =  Runtime.getRuntime().exec(command);
OutputStream out = process.getOutputStream();
Writer writer = new OutputStreamWriter(out);
writer.write(secondCommand);
writer.flush();
process.waitFor(600_000, TimeUnit.SECONDS);

Is there any possible to send only one command to putty and the putty will know to send it to the remote server?

MrAirman
  • 77
  • 1
  • 9

2 Answers2

1

PuTTY is a GUI application. Do not try to automate it.

Use the Plink instead. It's a console application from PuTTY package.

It supports input streams, what you code attempts to use. And also it allows support specifying the command on its command line:

String command = plinkPath + " -ssh user@localhost " + secondCommand;

Though even that is not the correct approach. Use some native Java SSH library, like the JSch, instead of using an external application.

See the JSch Exec.java example.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

You can try to add for your command the "-m" and a file with secondCommand. For more read about enter link description here

Zoltanik
  • 189
  • 2
  • 18