How to log in using jsch in java

Asked 2 years ago, Updated 2 years ago, 29 views

When I used the jsch library to connect to a device where an ssh server is started, I asked for the username Login: and I couldn't complete the login. How can I fix it to complete the login successfully?
When I googled, I saw an article saying that in this case, I have to write a program interactively like the expect command in the shell script, but I would like to complete the login successfully and automatically execute any command.

import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.util.Hashtable;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;


public class ZombieEx {
    private static final String hostname = "192.168.10.2";
    private static final String userid = "super";
    private static final String password = "admin";

    public static void main(String[]args) {
        try{
            ZombieExtest=new ZombieEx();
            test.doProc();
        } catch(JSchExceptionex){
            System.out.println(ex);
        } catch(ConnectExceptionex){
            System.out.println(ex.toString());
        } catch(IOExceptionex){
            System.out.println(ex);
        }
    }

    private void doProc()throws JSchException, IOException, ConnectException {
        JSch jsch = new JSch();

        // Do not perform HostKey checks
        Hashtable config = new Hashtable();
        config.put("StrictHostKeyChecking", "no");
        jsch.setConfig(config);

        // connection session
        Session session = jsch.getSession(userid, hostname, 22);
        session.setPassword(password);
        session.connect();

        // exec command remotely
        String command = "ls-l";
        ChannelExec channel=(ChannelExec) session.openChannel("exec");
        channel.setCommand(command);
        channel.connect();

        // get stdout
        InputStream in=channel.getInputStream();
        byte [ ] tmp = new byte [1024];
        while(true){
            while(in.available()>0){
                inti = 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(Exceptionex){

            }
        }

        // disconnect
        channel.disconnect();
        session.disconnect();
    }
}

▪記Additional
In the execution environment, the client is a MacBook and the server is a router.In this environment, even if you do the above, you cannot automatically log in.Ask for your username, LLogin: 」.Also, confirm the username and password using the ssh command.I have confirmed that I can log in manually without any problems.

·Additional 2
As Yamamoto-san heliac2001 pointed out, I understand that the user name is ignored.Therefore, we found that in our environment, you cannot log in with a single command, and you need to log in interactively.Does anyone know of a library where you can log in interactively automatically (wait for a message from the server and automatically log in by sending your username and password in order in response to this message)?Thank you for your cooperation.

java

2022-09-30 10:27

2 Answers

com.jcraft.jsch.Session You seem to have obtained the authentication method to use from the PreferredAuthentications setting in the class (I didn't know the official repository, so I'll show you my gist link).

When I ran it in my environment, the settings were as follows:

gssapi-with-mic, publickey, keyboard-interactive, password

It seems that the previous one will be used preferentially.

By explicitly writing password first (or only password), I think it is possible to have the password authenticated.Of course, the server must be compatible.

This means that password authentication might be performed by writing the following (line 3 setConfig added):

//connection Session
Session session = jsch.getSession(userid, hostname, 22);
session.setConfig("PreferredAuthentications", "password");
session.setPassword(password);
session.connect();

In terms of nature, I think it is the same problem as the link below.


2022-09-30 10:27

If it's ssh, how about using key authentication?
Place the public key on the server side and the private key on the client side.
Leave the private key passphrase blank and pass the interactive ID/passphrase input
You can log in.


2022-09-30 10:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.