3

I am using Jsch library to connect with my server. After connecting i am passing command which require password to proceed further hence i am passing my password in command only 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 code git ask password to proceed further. Note: i cannot disable password for git fetch origin.

2 Answers 2

1

I tried your code to access my linux box - it does log in successfully, but then fails to send any commands. I'm not sure if that's the problem you are having - but I will add my solution here, just in case.

Moved the shellStream.println(); command to its own function:

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

Had to make shellChannel and shellStream global variables in the process.

Changed shellStream.println(); to shellStream.print("\n");, as the aforementioned 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 shellChannel.connect(); twice in your code - I removed the last one.

Here's 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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Keyboard-interactive here mean the password must be tipped with a keyboard. SSH is quite peaky about that, even if there's ways to pass a password through the command line.

The best way would be to use a pubkey auth, but if that's not a option, try to login using only password

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

Also you may just send the password using

session.setPassword("password");

2 Comments

I used session.setConfig("PreferredAuthentications", "password"); but it is still asking for password in my console. my console out put : [mPassword for 'gitlab.xxx.xxx':
session.setPassword("password"); this one is also not working. It is still asking for the password. Even i tried echo 'password' and again it failed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.