Bash script – How to pass the password using expect in Linux

Posted: March 22, 2018 in password pass via expect, Shell Script

#!/bin/bash
#!/usr/bin/expect
IP=192.168.10.100
PASSWORD=root@123

echo -e "\n------------------------------------------------------------------------------------------"
echo -e "\t RHEL version"
echo "------------------------------------------------------------------------------------------"

expect -c "spawn ssh -qo StrictHostKeyChecking=no root@${IP}
 expect {
 \"(yes/no)?\"
 {
 send \"yes\"
 expect \"password: \"
 send \"${PASSWD}\r\"
 expect \"\\\\#\"
 send \"uname -r\r\"
 expect \"\\\\#\"
 send \"lsb_release -r\r\"
 expect \"\\\\#\"
 send \"logout\"
 }
 \"password: \"
 {
 send \"${PASSWD}\r\"
 expect \"\\\\#\"
 send \"uname -r\r\"
 expect \"lsb_release -r\\\\#\"
 send \"\r\"
 expect \"\\\\#\"
 send \"logout\"
 }
 \"# \"
 {
 expect \"\\\\#\"
 send \"uname -r\r\"
 expect \"\\\\#\"
 send \"lsb_release -r\r\"
 expect \"\\\\#\"
 send \"logout\"
 }
 }
 "
echo -e "\n ------------------------------------------------------------------------------------------\n"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s