How to send password in command using ssh command

I am using Jsch library to connect to my server. Once connected, I pass a command that requires a password to continue, so I only pass my password in a command, but nothing happens.

code:

JSch jsch = new JSch();
            jsch.removeAllIdentity();
            Session session = jsch.getSession(user, host, port);
            session.setPassword(password);
            .setConfig("StrictHostKeyChecking", "no");
            session.setConfig("PubkeyAuthentication", "no");
            System.out.println("Establishing Connection...");
            session.setConfig("PreferredAuthentications",
                    "publickey,keyboard-interactive,password");
            session.connect();
            System.out.println("Connection established.");
        System.out.println("Crating SFTP Channel.");`    
        Channel shellChannel = session.openChannel("shell");
        shellChannel.connect();
        ((ChannelShell) shellChannel).setPty(true);
        shellChannel.setInputStream(System.in);
        shellChannel.setOutputStream(System.out);
        PrintStream shellStream = new PrintStream(
                shellChannel.getOutputStream());

        shellChannel.connect();
        shellStream
                .println("cd /usr/local/apache2/; ls; cd ../www; ls; git fetch origin; <mypasssword>");
        shellStream.flush();
        System.out.println("SFTP Channel created.");`

      

When I run this git code, ask for the password to continue. Note. I can't seem to disable the password for the git fetch source.

+3


source to share


2 answers


I tried your code to access my Linux box - it registers successfully but then fails to send any commands. I'm not sure if you have this problem - but I'll add my solution here, just in case.

Moved the command shellStream.println();

to its own function:

public static void sendCommand(String c) {
    shellStream.print(c + "\n");
    shellStream.flush();
}

      

It was necessary to make shellChannel

and shellStream

global variables in the process.

Changed shellStream.println();

to shellStream.print("\n");

as the above refused to work.

After this line of your code:



shellStream = new PrintStream(shellChannel.getOutputStream());

      

Added my command sequence:

Thread.sleep(1000); // wait for it to connect    
sendCommand("sudo su"); // the command I tried
Thread.sleep(1000); // not sure how long you need to wait
sendCommand("mypassword");
Thread.sleep(1000);
// etc.

      

By the way, you are calling twice shellChannel.connect();

in your code - I removed the last one.

Here is the final working version of your code:

import java.io.IOException;
import java.io.PrintStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class MyShell {

    static String user = "daniel";
    static String host = "localhost";
    static int port = 22;
    static String password = "mypass";
    static Session session;
    static Channel shellChannel;
    static PrintStream shellStream;

    public static void main(String[] args) throws JSchException, IOException,
            InterruptedException {

        JSch jsch = new JSch();
        jsch.removeAllIdentity();
        session = jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setConfig("PubkeyAuthentication", "no");
        System.out.println("Establishing Connection...");
        session.setConfig("PreferredAuthentications",
                "publickey,keyboard-interactive,password");
        session.connect();
        System.out.println("Connection established.");
        System.out.println("Crating SFTP Channel.");

        shellChannel = session.openChannel("shell");
        shellChannel.connect();
        ((ChannelShell) shellChannel).setPty(true);
        shellChannel.setInputStream(System.in);
        shellChannel.setOutputStream(System.out);
        shellStream = new PrintStream(shellChannel.getOutputStream());

        Thread.sleep(1000);
        sendCommand("sudo su");
        Thread.sleep(1000);
        sendCommand("mypass");
        Thread.sleep(1000);
        sendCommand("ls");

    }

    public static void sendCommand(String c) {
        shellStream.print(c + "\n");
        shellStream.flush();
    }
}

      

+1


source


session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");

      

Keyboard-interactive

here means that a password must be configured on the keyboard. SSH is pretty good at this, even if there are ways to pass the password through the command line.

The best way would be to use auth pubkey, but if that's not an option, try logging in with only password

session.setConfig("PreferredAuthentications", "password");

      




Also you can just send the password using

session.setPassword("password");

      

0


source







All Articles