Script auto login ssh

Share this post to your friends !

WriteBash - There are many tools that allow you to manage the login ssh to multiple Linux servers, through which you can perform login ssh tasks very quickly. However, sometimes you need some tool of your own or you do not trust the tools for free. In this article, I will give you a script that can help you login ssh quickly.

Basically this script uses the expect command to pass the username and password information to the ssh command.

About the ssh script

This script consists of two basic parts: the first part I call sub script – it performs the declaration of information to be able to execute the login ssh; The second part is called the main script – it’s a menu that allows you to select the server you need to perform ssh.

SSH sub script

For easy management, you can create a directory and put all of these sub scripts into that directory. Each server you need to perform ssh, you declare a separate sub script. Note that the script below does not use /bin/bash, but uses /usr/bin/expect instead

Script content:

# Script by: Danie Pham
# Website:
# Script date: 26-12-2017
# Script ver: 1.0

set user root
set password abc123@@
set ipaddr
spawn ssh $user@$ipaddr
expect "*?assword:*"
send -- "$password\r"
expect "$"

You replace the information below with your system:

  • root: replace with your username.
  • abc123@@: replace with your password.
  • replace with your IP of server.

In the above example, I will create the first sub script and name it server-01.

SSH main script

This main script uses the case command to create a simple menu so you can select the server you want to ssh quickly.

# Script by: Danie Pham 
# Website: 
# Script date: 26-12-2017 
# Script ver: 1.0

echo "Select the server to ssh"
echo "---------------------"
echo "1) server-01"
echo "2) server-02"
echo "---------------------"
read NUM
case $NUM in
cd /home/scripts
cd /home/scripts
echo "Select again"

In my example, I put the sub scripts in the /home/scripts directory. I have server-01 and server-02. How many servers do you need, just add as many sub script files to the directory, then call it in the main script. The main script you can place anywhere you want, for example, I will put in /homeand name it

Permissions for scripts

You run the following commands to grant executable permissions to scripts:

chmod 700 /home/
chmod 700 /home/scripts
chmod 700 /home/scripts/*

Use the login ssh script

When using the script, if you encounter the below error:

/usr/bin/expect: bad interpreter: No such file or directory

Please execute the following command to install the expect package (for CentOS, with an Ubuntu based OS, you can try replacing yum with apt-get).

yum install expect tcl -y
script-auto-login-ssh Script auto login ssh
Use auto login ssh script

In the demo image above, the main script will call sub script server-01 when you press number 1. Sub script server-01 will use expect to automatically send username and password to ssh command. To exit the script, press ctrl + C.


By using the expect command, you can write yourself a menu that manages the ssh servers of your Linux system. It’s simple, easy to use and you write so you do not have to worry about being exposed when using other free software.

If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.

Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.