Code Done Right!

SSH connection

The below script allows for making SSH connection without the hassle of entering credentials every time. It is worth noting that, for security reasons, such script should be used only on trusted machines as you are providing login credentials in the script.

This short code just saves you a few keystrokes, and combined with an alias makes your life easier if you are using SSH connection frequently.

Required packages

  • expect

The script makes use of expect package, it is a tool for interacting with shell in accordance with script.

To install expect run the following

sudo apt install expect

SSH connection script

#!/usr/bin/expect -f

spawn ssh ${USER}@${IP}
expect "assword:"
send "${PASSWORD}\r"
interact

In order to automate the connection you have to change some variables. Amend the following to reflect your connection

  • ${USER} – username for the server
  • ${IP} – IP of the server
  • ${PASSWORD} – username password

Note! “assword:” is not a typo, as this mitigates the distinction between words “Password:” and “password:” which are in fact different.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.