Running Commands on a Remote Linux Server over SSH

Hello,
I'm trying to create a ksh script to ssh to a remote server, enter the password and a couple commands. For security reasons I have changed the login, password and ip in my example.

#!/bin/ksh

ssh -t -t username@12.5.5.3 << EOF
password
cd bin
pwd
EOF

When I run it. It stalls at the password prompt and it will only continue when I manually enter it. I see the errors are produced due it entering the info too quickly. Is there a way for it to accept the password via script and then wait a certain amount of time before executing the commands? Also need it to exit the remote server when it completes. Any help is much appreciated, thanks.

Example results:

# /home/test/testscript.ksh
username@12.5.5.3's password:      <----STALLS HERE. ONLY PROCEEDS WHEN I ENTER THE PASSWORD MANUALLY.
password
cd bin
pwd
[username@remoteserver ~]$ password
[1] 27729
-bash: pas: command not found
-bash: sword: command not found
[1]+  Exit 127                pas
[username@remoteserver ~]$ cd bin
[username@remoteserver bin]$ pwd
/home/user/bin
[username@remoteserver bin]$       <----STOPS AND STILL ON REMOTE SERVER. 
1 Like

The short answer is:
use the expect command to do this
or
install ssh keys for the user running the command - install on the remote box.
For ssh keys:
ssh-keygen: password-less SSH login

That's because ssh (as AFAIK any other terminal program) reads the password from the interactive terminal, NOT stdin which in your case is redirected to the "here document". Use other authentication methods as e.g. "public key auth" ( man ssh ).
After finishing and closing the "here document", it continues reading stdin (now your terminal again). Put an exit at the end of the "here doc".

Is there a way to have the user input the password and have the script complete?

Wishful results:

# /home/test/testscript.ksh

username@12.5.5.3's password:      USER INPUTS PASSWORD HERE
[username@remoteserver ~]$ cd bin
[username@remoteserver bin]$ pwd
/home/user/bin
[username@remoteserver bin]$ exit      
#                                                    RETURNS TO ORIGINAL SERVER

Yes. Change your script from posting #1 as follows:

  1. Delete the line with word password
  2. Add exit between pwd and EOF

That worked perfected, Thank you!!! :slight_smile: