expect script to automate shell logins and sudo to oracle

This posting describes a way to solve a problem of how to open an ssh session and login automatically (without using a private key). This is not a good idea, but can be useful and demonstrates the usefulness of expect scripting.

Note that I would recommend against doing this in most cases because the password is clearly documented in the script. Having said that, this is the situation at many Fortune 500 companies and Global 1000 companies.

My direct experience is that these companies have thousands of scripts in production that have passwords directly embedded into them or passwords set to the account ID.

Given that we cannot fix everything all the time, and need to work fast when we are tired, this script can be useful. Also, there are other applications of expect scripting that are benign. Hopefully this example will lead the reader to see other ways to use expect scripting.

Script to perform an automated login and sudo to oracle account

#!/usr/bin/expect 
# this script opens a ssh session and then logs is using a password containing a $
# then sudo over to the oracle account 

spawn ssh $argv 
expect "password:" 
send "Jan\$6rath\r" 

#sudo to oracle
send "sudo su - oracle\r" 
expect "password:" 
send "Jan\$6rath\r"
expect "$" 

#cd to your home location -- different depending on your OFA
send "cd /ora/admin\r"
expect "$" 
send "ls -l\r"

# return console to user
interact