Linux – Send Password in ssh script


I made a script called sshconnect for some users, that takes 2 variables , usertype and sitenumber so users use the script to ssh to that sitenumber with the usertype.

For example the user type the following: sshconnect read@11 and the script will let the user ssh to site number 11 (mapped to an IP address) with the username read.

but when this happens the user will connect to that site and be asked to enter the password for the username read. But I do not want the user to know or enter the password himself, rather let the script send the password.

Is there a way to send the password in the script?


Best Answer

There are various possibilities. You can use expect script or simple sshpass, but the password will be written in the script itself so the user can read it from there (this is not a security).

In short:

sshpass -p password ssh user@host

The better solution is passwordless authentication using key pair.

In short, you will create a new key pair, store it in the server and incorporate it in your script:

ssh-keygen -t rsa -f key_file -P '' # prepare the key pair
ssh-copy-id user@host               # enter the password once
ssh -i key_file user@host           # logs in without password
Related Question