Wednesday, January 23, 2019

Shell Script to SSH into servers

This is a simple script that can ssh into a server by typing the password for you.

Filename: login
#!/usr/bin/expect
eval spawn ssh username@servername
expect "assword:"
send "server_password\r"
interact

# to run the script
# ./login
In this below script you can pass arguments to the shell script
Filename: login
#!/usr/bin/expect
set num [lindex $argv 0];
eval spawn ssh username@servername-$num
expect "assword:"
send "server_password\r"
interact

# make sure to give executable permission to script.

chmod +x login

# to run the script and login to server-01.

./login 01

Now add the script path to your .bashrc file as alias, so that you can access this script from any path.


# vi ~/.bashrc

alias login='/path/to/script/file/login'


After updating the .bashrc file, you will have to source.
source ~/.bashrc


Now call the script from anywhere in your shell.
login 01

Enjoy!!!