Search code examples
sshpasswordsplink

Entering password to remote ssh through Plink after establishing a connection


I'm trying to establish a connection through Plink, after that I want to ssh user@server and then enter a password. I did the following:

plink.exe -ssh user1@server_ip1 -pw password1 -t < "cmd.txt"

in the cmd.txt there are the below commands (I tried both with and without the semicolons):

ssh user2@server2;
password2

Everything works fine until the step of password2. The password is not entered. I tried the below (with and without the semicolons):

ssh user2@server2;
password2;
echo hello2;

to make sure an enter is pressed, but it didn't work as well.


Solution

  • First of all, I believe there's more elegant solution to what you are trying to achieve:
    Execute commands on remote server behind another server (jumphost) using Plink


    Anyway, to answer your literal question:

    You are running ssh remotely. As you seem to know, you need to run it with pseudo terminal (-t) for ssh to believe it runs in an interactive terminal so it accepts the password.

    With pseudo terminal, the input is not queued, but works only when it is asked for. So you need to feed the password only after you get the prompt. As such, you should use techniques as describe here:


    If you want to stick purely with plink-based solution, as a workaround, you can shortly wait before feeding the password. For some examples, see:


    Another thing are the semicolons. Those should not be in the input file.