Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending commands to remote server through ssh by Java with JSch

Tags:

java

ssh

jsch

I'm trying to set up a class so that I can ssh into a remote server (I have the IP, username, and password) and then send a command like echo "test" and then receive back the output (e.g., "test"). I'm using JSch to do this but I don't understand how to do it.

import com.jcraft.jsch.*;

public class ConnectSSH {

public int execute (String command) {
    
    JSch jsch   = new JSch();
    String ip   = "00.00.00.00";
    String user = "root";
    String pass = "password";
    int port    = 22;

    Session session = jsch.getSession(user, ip, port);   
    session.setPassword(pass);
    session.connect();

    ...

I'm stuck after connecting.

like image 365
snario Avatar asked May 09 '13 18:05

snario


2 Answers

Try this:

JSch jsch=new JSch();
Session session=jsch.getSession(remoteHostUserName, RemoteHostName, 22);
session.setPassword(remoteHostpassword);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();

ChannelExec channel=(ChannelExec) session.openChannel("exec");
BufferedReader in=new BufferedReader(new InputStreamReader(channel.getInputStream()));
channel.setCommand("pwd;");
channel.connect();

String msg=null;
while((msg=in.readLine())!=null){
  System.out.println(msg);
}

channel.disconnect();
session.disconnect();
like image 149
Shamnad Avatar answered Nov 01 '22 03:11

Shamnad


shamnu's answer above was right. I couldn't add a comment to it, so here are a few examples to enhance his answer. One is how to do a remote execution of 'ls -l', another of 'mkdir', and another of a local to remote copy. All done with version 0.1.51 of jsch (http://www.jcraft.com/jsch/).

  public void remoteLs() throws JSchException, IOException {
    JSch js = new JSch();
    Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
    s.setPassword("mypassword");
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    s.setConfig(config);
    s.connect();

    Channel c = s.openChannel("exec");
    ChannelExec ce = (ChannelExec) c;

    ce.setCommand("ls -l");
    ce.setErrStream(System.err);

    ce.connect();

    BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
    String line;
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
    }

    ce.disconnect();
    s.disconnect();

    System.out.println("Exit code: " + ce.getExitStatus());

  }



  public void remoteMkdir() throws JSchException, IOException {
    JSch js = new JSch();
    Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
    s.setPassword("mypassword");
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    s.setConfig(config);
    s.connect();

    Channel c = s.openChannel("exec");
    ChannelExec ce = (ChannelExec) c;

    ce.setCommand("mkdir remotetestdir");
    ce.setErrStream(System.err);

    ce.connect();

    BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
    String line;
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
    }

    ce.disconnect();
    s.disconnect();

    System.out.println("Exit code: " + ce.getExitStatus());

  }

  public void remoteCopy() throws JSchException, IOException, SftpException {
    JSch js = new JSch();
    Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
    s.setPassword("mypassword");
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    s.setConfig(config);
    s.connect();

    Channel c = s.openChannel("sftp");
    ChannelSftp ce = (ChannelSftp) c;

    ce.connect();

    ce.put("/home/myuser/test.txt","test.txt");

    ce.disconnect();
    s.disconnect();    
  }
like image 38
gageorge Avatar answered Nov 01 '22 05:11

gageorge