SSH Automation

Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network.[1] The best-known example application is for remote login to computer systems by users.

SSH provides a secure channel over an unsecured network in a client-server architecture, connecting an SSH client application with an SSH server.[2] Common applications include remote command-line login and remote command execution, but any network service can be secured with SSH. The protocol specification distinguishes between two major versions, referred to as SSH-1 and SSH-2.

Since it’s one of the safest way and common access to the Linux server’s, it’s daily used by admins. In some cases, in order to harden the server, we will disable direct root logins and add wheel user for the same. So in order to log in to the server, you have to provide to two passwords. So in order to automate, you can use the below-pasted script. This script will store the password in a local Linux machine and will help to login without providing the password.

Please refer the below snippet for the script to enter a server having wheel user then root using password authentication:




#!/usr/bin/expect
set timeout 60
set IPaddress ""
set Username ""
set Password ""
set Rtpass ""

 

spawn ssh -o "StrictHostKeyChecking no" $Username@$IPaddress -p port
expect "*assword: "
send "$Password\r"
expect "${Username}@"
send -- "su -\r"

expect "*assword: "
send "$Rtpass\r"
interact
exit

 

Please refer the below script to do ssh from serverĀ  A to B then to C. All by running a script from A.



spawn ssh username@IP
expect "password: "
send "xxpasswordxx\r"
expect "$ "
spawn ssh username@IP
expect "*assword: "
send "xxpasswordxx\r"
expect "$ "
interact
exit


If you need any further assistance contact me @ [email protected].. prefer hangout ?

Leave a Reply

Your email address will not be published. Required fields are marked *

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