Script auto login ssh

by Daniel Pham
Published: Updated:

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.

Recommended Reading: Script send email to admin warning ssh login

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
use auto login ssh script
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.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating

You may also like

Notify of
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.



Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 – All rights reserved.

Please write sources “” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Would love your thoughts, please comment.x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.