0

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.

  • Use native Java SSH library, like JSch, instead of driving a console application. – Martin Prikryl Jul 12 '18 at 11:53
  • @MartinPrikryl I have written there that I have already tried JSch but it doesn't even establish the connection. Seems like there are security settings to prevent it on the comapny servers. Which is why I tried to use a console app. – Pratik Mayekar Jul 12 '18 at 12:16
  • Sorry, I didn't notice. Though it makes no sense. While a server can indeed decide to *close connection* based on a client library/software, it cannot *reject connection* based on that. As at connection time, it cannot know yet, what the client is like. Moreover, there's nothing that prevents you from faking Plink in JSch. => Show us your JSch code as well as a [log file](https://stackoverflow.com/q/47411185/850848). – Martin Prikryl Jul 12 '18 at 12:29
  • @MartinPrikryl The server has RSA Token based authentication system. I tried it many times to connect using JSch and tried multiple solutions to the problems I faced which other people had too and many solutions worked for them too. But not for me unfortunately, it didn't work for me. I had asked a specific question for the JSch issue. https://stackoverflow.com/questions/51166570/connect-to-a-linux-system-secured-by-rsa-securid-using-ssh2-in-java – Pratik Mayekar Jul 12 '18 at 12:35
  • @MartinPrikryl someone else asked a similar question which according to me didn't get the solution I can use. Maybe you can help out. https://stackoverflow.com/questions/19439517/connect-to-unix-using-ssh-and-sftp-via-java-rsa-secuerid-token – Pratik Mayekar Jul 12 '18 at 12:40

0 Answers0