Aditya
Aditya

Reputation: 928

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)

Upvotes: 5

Views: 9549

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202682

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?

Upvotes: 6

Related Questions