1

Currently when I connect from windows to our linux host server, I use putty with my credentials in a .bat file to automatically access the machine. However the connection does not automatically direct me to the terminal shell but there's a secondary interactive login prompt that requires me to manually input another username and password, as of now I don't know how to get pass these with automation.

My question, is it possible to automate these specifically on java or just a .bat?

One of my co-workers managed to do it on VB script using the "sendkey" method. The information online states "sendkey" send one or more keystrokes to the active window as if they were typed at the keyboard. May I know is there a counterpart of sendkey in Java in JSCH or SSHJ?

NOTE: I'm not after to connect directly to the terminal, what I'm after is to automate the interactive login as if I were manually typing it like what the sendkey does.

1 Answer 1

1

Injecting typed characters into a ssh session is pretty simple. Just inject the characters to be written into the OutputStream provided by sshj. I assume that you log-in and then execute something that requires the interactive console login.

I simulate the executed process using the following simple script named test.sh. It reads a typed line and prints it.

#/bin/bash
read varname
echo You typed $varname

I am executing this script using a modified Exec example from sshj:

final Session session = ssh.startSession();
try {
    Command cmd = session.exec("/home/username/test.sh");
    OutputStream out = cmd.getOutputStream();
    out.write("1234567\n".getBytes()); // we type 1234567
    out.flush();
    System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
    cmd.join(5, TimeUnit.SECONDS);
    System.out.println("\n** exit status: " + cmd.getExitStatus());
} finally {
    session.close();
}

If you execute the script using the sshj code you will get the result

You typed 1234567

By reading the inputStream line by line you can wait for the line that asks for the username or password and send the right data to the server, simulating your input.

Sign up to request clarification or add additional context in comments.

Comments

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.