The title “Bash Script for SSH Login with Password” means executing a bash script so you can directly log in to the server without the need to type the SSH command or password. A bash script to automate the SSH login is helpful (for certain automation) and avoids the need for key based login setup. However, we are describing the methodologies to automate SSH login for things such as Raspberry Pi. The methods for automating SSH login in these manners are not for use on unmanaged web servers (such as one running PHP, Apache2, MySQL, or WordPress and having a public IP address).
Bash Script for SSH Login
The easiest method is the below:
1 2 3 4 5 6 7 8 9 10 11 | # !/bin/bash read -p 'Username: ' user read -sp 'Password: ' pass if (( $user == "rootuser" && $pass == "rootpass" )) then echo -e "\nWelcome! You are logged in\n" else echo -e "\nUnsuccessful login attempt\n" fi |
Now do the steps to execute the script:
---
1 2 3 4 | chmod +x login.sh sh login.sh # or # ./login.sh |
That is probably prone to failure in different situations. Another method is by installing a package named expect
:
1 2 | apt install expect -y # dnf install expect |
The easiest script I know is like the one below:
1 2 3 4 5 6 | #!/usr/bin/expect -f spawn ssh linuxhint@192.168.1.103 expect "Password:*" send "yourpassword\r" expect "$ " interact |
yourpassword
written above is your real password. You have to make it executable and run:
1 2 3 4 | chmod +x login.sh sh login.sh # or # ./login.sh |
It works great for known hosts. However, it can not handle additional dialogues. I found the below script described by www.golinuxcloud.com
which addresses that issue :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #!/usr/bin/expect set USER [lindex $argv 0] set HOST [lindex $argv 1] set PWD [lindex $argv 2] log_file /var/log/ssh_tmp.log set timeout 30 log_user 1 set send_slow {1 .01} send_log "Connecting to $HOST using $USER user\n" eval spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o Connecttimeout =30 "$USER\@$HOST" expect { timeout { send_user "timeout while connecting to $HOST\n"; exit } "*No route to host*" { send_user "$HOST not reachable\n"; exit } "*assword: " { send -s $PWD\r } } expect { timeout { send_user "timeout waiting for prompt\n"; exit } "*]#" { send_user "Login successful to $HOST\n" } } send "hostname\r" expect { "*]#" { send "exit\r" } } send_user "Disconnected\n" close |
I have kept the script on GitHub as gist for my usage. You can wget
it and use it.
Another method described to achieve a similar result is by installing a package named sshpass
. The advantage of the SSH Pass package is that you can use it with key-based login too.
1 2 3 | echo 'YourPassword' > passwordFile.txt chmod 600 passwordFile.txt sshpass -f /path/to/passwordFile.txt /usr/bin/ssh -p 8484 root@111.22.33.444 |
However, if you want key-based login, then perform the below steps.
1 2 3 | mkdir -p ~/.ssh cd ~/.ssh ssh-keygen -type dsa -i mysshkeys |
Press Return when prompted for passphrase. Press Return a second time to confirm. There will be two files in the ~/.ssh
directory, mysshkey.pub
and mysshkey
. mysshkey.pub
is the public key, you’ll put this on remote servers. mysshkey
is your private key.
On the server you wish to SSH into. Login to the remote server:
1 2 3 4 5 | mkdir -p ~/.ssh # Copy and paste the contents of mysshkey.pub into ~/.ssh/authorized_keys # Make sure that ~/.ssh/authorized_keys is chmod'd to 600 # Now, on your local machine you run the following command: ssh -i ~/.ssh/mysshkey <remote_server_ip> |
And you will be logged in without being prompted for a password. The last method is the safest one.
Tagged With bash script for ssh login with password