Automatically enter SSH password with script Automatically enter SSH password with script linux linux

Automatically enter SSH password with script


First you need to install sshpass.

  • Ubuntu/Debian: apt-get install sshpass
  • Fedora/CentOS: yum install sshpass
  • Arch: pacman -S sshpass

Example:

sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no YOUR_USERNAME@SOME_SITE.COM

Custom port example:

sshpass -p "YOUR_PASSWORD" ssh -o StrictHostKeyChecking=no YOUR_USERNAME@SOME_SITE.COM:2400

Notes:

  • sshpass can also read a password from a file when the -f flag is passed.
    • Using -f prevents the password from being visible if the ps command is executed.
    • The file that the password is stored in should have secure permissions.


After looking for an answer for the question for months, I finally found a better solution: writing a simple script.

#!/usr/bin/expectset timeout 20set cmd [lrange $argv 1 end]set password [lindex $argv 0]eval spawn $cmdexpect "assword:"send "$password\r";interact

Put it to /usr/bin/exp, then you can use:

  • exp <password> ssh <anything>
  • exp <password> scp <anysrc> <anydst>

Done!


Use public key authentication: https://help.ubuntu.com/community/SSH/OpenSSH/Keys

In the source host run this only once:

ssh-keygen -t rsa # ENTER to every fieldssh-copy-id myname@somehost

That's all, after that you'll be able to do ssh without password.