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 # Script by: Danie Pham # Website: https://www.writebash.com # Script date: 26-12-2017 # Script ver: 1.0 set user root set password abc123@@ set ipaddr 192.168.56.4 spawn ssh $user@$ipaddr expect "*?assword:*" send -- "$password\r" expect "$" interact
You replace the information below with your system:
root: replace with your username.
abc123@@: replace with your password.
192.168.56.4: replace with your IP of server.
In the above example, I will create the first sub script and name it
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.
#!/bin/bash # Script by: Danie Pham # Website: https://www.writebash.com # Script date: 26-12-2017 # Script ver: 1.0 echo; echo "Select the server to ssh" echo "---------------------" echo "1) server-01" echo "2) server-02" echo "---------------------" read NUM case $NUM in 1) cd /home/scripts ./server-01 ;; 2) cd /home/scripts ./server-02 ;; *) echo "Select again" esac
In my example, I put the sub scripts in the
/home/scripts directory. I have
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/ssh_main.sh 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 install expect tcl -y
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 appreciate what we share in this blog, you can support us by:
- Stay connected to: Facebook | Twitter | Google Plus | YouTube
- Subscribe email to recieve new posts from us: Sign up now.
- Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
- Become a Supporter - Make a contribution via PayPal.
- Support us by purchasing Ribbon Lite Child theme being using on this website.
We are thankful for your support.