nshastri
nshastri

Reputation: 33

How to connect the putty through Java code and pass command to terminal?

I have tried with below code. but putty is launching and shuts imidiatlly. in command.txt contain ls -lrt code.

Runtime r = Runtime.getRuntime();
    //Runtime r2 = Runtime.getRuntime();
    Process p = null;
    //Process p2 = null;
    String s = "D:\\Nandan\\putty.exe -ssh -l ***** -pw ******** XX.XX.XX.XX -m D:\\Nandan\\command.txtx";
    //String s2 = "ls -lrt";
    try
    {
        p = r.exec(s);
        p.waitFor();

    } catch (Exception e)
    {
        System.out.println("Exception error :"+e.getMessage());
        e.printStackTrace();
    }

Upvotes: 2

Views: 18976

Answers (3)

samar ranjan Nayak
samar ranjan Nayak

Reputation: 131

Fixing the issue with the code, that was being asked as part of the question.

-m option for the putty instructs the server to start the commands present in the file instead of the shell so as soon as commands are completed the session closes. To make the putty session interactive we need to add -t option as well as in the File given as input to -m option we need to add "/bin/bash" as the last command.

putty.exe -ssh -l username -pw NextGenCne password -m D:\test.txt -t

The content of the D:\test.txt file is as below.

cd /home/username/mydirectory
/bin/bash

So the complete program will be something like below.

Runtime r = Runtime.getRuntime();
    //Runtime r2 = Runtime.getRuntime();
    Process p = null;
    //Process p2 = null;
    String s = "D:\\Nandan\\putty.exe -ssh -l ***** -pw ******** XX.XX.XX.XX -m D:\\Nandan\\command.txtx -t";
    //String s2 = "ls -lrt";
    try
    {
        p = r.exec(s);
        p.waitFor();

    } catch (Exception e)
    {
        System.out.println("Exception error :"+e.getMessage());
        e.printStackTrace();
    }

Note: If the aim is to open somehow putty terminal from java code then the above approach is okay, if the goal is just to execute some commands on the server and capture the output then go for JSch as already mentioned in the above answers.

Upvotes: 0

Vladi
Vladi

Reputation: 1990

I success to make it like that

import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Dictionary;
import java.util.Hashtable;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public static void puttyConnection(String user, String hostName, String password)
        throws JSchException, IOException {
    Session session = new JSch().getSession(user, hostName, 22);
    session.setPassword(password);
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();
    Channel channel = session.openChannel("shell");
    OutputStream ops = channel.getOutputStream();
    PrintStream ps = new PrintStream(ops, true);

    channel.connect();
    InputStream input = channel.getInputStream();

    ps.println("Put Here your command");
    ps.println("Put Here another command");
    ps.close();
    try {
        printResult(input, channel);
    } catch (Exception e) {
        e.printStackTrace();
    }
    channel.disconnect();
    session.disconnect();
}

about the function printResult

    private static void printResult(InputStream input, Channel channel) throws Exception {
    int SIZE = 1024;
    byte[] tmp = new byte[SIZE];
    while (true) {
        while (input.available() > 0) {
            int i = input.read(tmp, 0, SIZE);
            if (i < 0)
                break;
            System.out.print(new String(tmp, 0, i));
        }
        if (channel.isClosed()) {
            System.out.println("exit-status: " + channel.getExitStatus());
            break;
        }
        try {
            Thread.sleep(300);
        } catch (Exception ee) {
        }
    }
}

Upvotes: 0

Naveen Ramawat
Naveen Ramawat

Reputation: 1445

Putty is ssh client, So you instead of calling putty you can directly used java ssh library JSch.jar for execute any operation on a linux machine. below is sample code for the same

Session session = new JSch().getSession(user, hostName, 22);        
session.setPassword(password);
java.util.Properties config = new java.util.Properties(); 
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand("some command here");
String result = IOUtils.toString(channel.getInputStream());
channel.disconnect();
session.disconnect();

to learn more please go through the link http://www.jcraft.com/jsch/examples/

Upvotes: 5

Related Questions