Commercial
The title “Bash Script for SSH Login with Password” means executing a bash script so you possibly can immediately log in to the server with out the necessity to sort the SSH command or password. A bash script to automate the SSH login is useful (for sure automation) and avoids the necessity for key primarily based login setup. Nonetheless, we’re describing the methodologies to automate SSH login for issues reminiscent of Raspberry Pi. The strategies for automating SSH login in these manners usually are not to be used on unmanaged internet servers (reminiscent of one operating PHP, Apache2, MySQL, or WordPress and having a public IP handle).
Bash Script for SSH Login
The best methodology is the beneath:
|
# !/bin/bash
learn –p ‘Username: ‘ person learn –sp ‘Password: ‘ cross
if (( $person == “rootuser” && $cross == “rootpass” )) then echo –e “nWelcome! You’re logged inn” else echo –e “nUnsuccessful login attemptn” fi |
Now do the steps to execute the script:
|
chmod +x login.sh sh login.sh # or # ./login.sh |
That’s most likely susceptible to failure in numerous conditions. One other methodology is by putting in a bundle named anticipate
:
|
apt set up anticipate –y # dnf set up anticipate |
The best script I do know is just like the one beneath:
|
#!/usr/bin/anticipate –f spawn ssh linuxhint@192.168.1.103 anticipate “Password:*” ship “yourpasswordr” anticipate “$ “ work together |
yourpassword
written above is your actual password. You must make it executable and run:
|
chmod +x login.sh sh login.sh # or # ./login.sh |
It really works nice for recognized hosts. Nonetheless, it can’t deal with further dialogues. I discovered the beneath script described by www.golinuxcloud.com
which addresses that challenge :
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/anticipate 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 utilizing $USER usern” eval spawn ssh –o UserKnownHostsFile=/dev/null –o StrictHostKeyChecking=no –o Connecttimeout =30 “$USER@$HOST” anticipate { timeout { send_user “timeout whereas connecting to $HOSTn”; exit } “*No path to host*” { send_user “$HOST not reachablen”; exit } “*assword: “ { ship –s $PWDr } } anticipate { timeout { send_user “timeout ready for promptn”; exit } “*]#” { send_user “Login profitable to $HOSTn” } } ship “hostnamer” anticipate { “*]#” { ship “exitr” } } send_user “Disconnectedn” shut |
I’ve stored the script on GitHub as gist for my utilization. You may wget
it and use it.
One other methodology described to attain the same result’s by putting in a bundle named sshpass
. The benefit of the SSH Move bundle is that you should use it with key-based login too.
|
echo ‘YourPassword’ > passwordFile.txt chmod 600 passwordFile.txt sshpass –f /path/to/passwordFile.txt /usr/bin/ssh –p 8484 root@111.22.33.444 |
Nonetheless, if you would like key-based login, then carry out the beneath steps.
|
mkdir –p ~/.ssh cd ~/.ssh ssh–keygen –sort dsa –i mysshkeys |
Press Return when prompted for passphrase. Press Return a second time to verify. There will probably be two information within the ~/.ssh
listing, mysshkey.pub
and mysshkey
. mysshkey.pub
is the general public key, you’ll put this on distant servers. mysshkey
is your non-public key.
On the server you want to SSH into. Login to the distant server:
|
mkdir –p ~/.ssh # Copy and paste the contents of mysshkey.pub into ~/.ssh/authorized_keys # Make certain that ~/.ssh/authorized_keys is chmod‘d to 600 # Now, on your native machine you run the following command: ssh –i ~/.ssh/mysshkey <remote_server_ip> |
And you’ll be logged in with out being prompted for a password. The final methodology is the most secure one.