Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to remote Windows machine with JSch

I want to execute command on remote system and want to get result of the same.

I tried following code:

package rough;
import java.io.InputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class RemoteSSH {

    public static void main(String[] args) {
        String host = "\\\\10.209.110.114";
        String user = "Administrator";
        String password = "Admin123";
        String command1 = "ipconfig";
        try {
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();
            System.out.println("Connected");

            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command1);
            channel.setInputStream(null);
            ((ChannelExec) channel).setErrStream(System.err);

            InputStream in = channel.getInputStream();
            channel.connect();
            byte[] tmp = new byte[1024];
            while (true) {
                while (in.available() > 0) {
                    int i = in.read(tmp, 0, 1024);
                    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(1000);
                } catch (Exception ee) {
                }
            }
            channel.disconnect();
            session.disconnect();
            System.out.println("DONE");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 }

But I am getting following error:

com.jcraft.jsch.JSchException: java.lang.IllegalArgumentException: protocol = socket host = null
    at com.jcraft.jsch.Util.createSocket(Util.java:258)
    at com.jcraft.jsch.Session.connect(Session.java:186)
    at com.jcraft.jsch.Session.connect(Session.java:145)
    at rough.RemoteSSH.main(RemoteSSH.java:23)
Caused by: java.lang.IllegalArgumentException: protocol = socket host = null
    at sun.net.spi.DefaultProxySelector.select(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at com.jcraft.jsch.Util.createSocket(Util.java:252)
    ... 3 more

Any solution for it? I have already tried with pstool also but not getting correct output.

After removing the \\\\ from the hostname, I'm getting:

com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused: connect
    at com.jcraft.jsch.Util.createSocket(Util.java:258)
    at com.jcraft.jsch.Session.connect(Session.java:186)
    at com.jcraft.jsch.Session.connect(Session.java:145)
    at rough.RemoteSSH.main(RemoteSSH.java:23)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
like image 439
Aditya Avatar asked Aug 11 '16 08:08

Aditya


People also ask

How do I connect to a JSch remote server?

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.

What is JSch connection?

JSch is the Java implementation of SSH2 that allows us to connect to an SSH server and use port forwarding, X11 forwarding, and file transfer. Also, it is licensed under the BSD style license and provides us with an easy way to establish an SSH connection with Java.

What is Jcraft JSch?

JSch is a pure Java implementation of SSH2. JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs. JSch is licensed under BSD style license.

How do I run multiple commands in JSch?

Executing multiple commands at once on the remote server using Jsch. We must add a semicolon(;) between each command we want to run. So, the format will be first_command;second_command;third_command.


1 Answers

My wild guess is that you have no SSH server running on the target machine.

Note that Windows does not have any built-in SSH server.

Make sure you install some SSH server first.
See Is IIS SFTP natively supported by Windows Server 2012 R2?

Or use a different technology to run the command:
Best way to run remote commands on a Windows server from Java?

like image 161
Martin Prikryl Avatar answered Sep 30 '22 08:09

Martin Prikryl