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?

3
  • what server you are using? Commented Jan 9, 2017 at 7:40
  • @LAIDANIYoucef Linux machine Commented Jan 9, 2017 at 7:41
  • why you want putty, i don't think you need putty try to use command = "ssh ipadress yourcommand"; Commented Jan 9, 2017 at 7:48

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I want to show the terminal of putty, i didn't succeeded to do so with plink
If you want to display terminal, you should say so in the question.
0

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

Comments

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.