Automate password based ssh login using expect

 Install the expect package

#yum list expect

//this package is available in my redhat repo

#yum install expect

//given yes

//whenever we used expect in script make sure the shebang should be the expect like below

this is not /bin/bash script it an separate bash so we have to specify in #! like below

#!/usr/bin/expect -f

as well the script should not save as .sh 

it need to be create a file name without .sh

#vi examplescript

//and the exectution mention 

expect examplescript

or

#./examplescript


to find the location

# which expect

/bin/expect


//script
#!/usr/bin/expect -f
spawn ssh root@10.10.10.10 hostname
expect "password"
send "Welcome@1234\r"
interact

//exectuion
# expect stat
spawn ssh root@10.10.10.10 hostname
root@10.10.10.10's password:
ansiblehost

or

#chmod +x stat
#./stat

---------------------------

execute shell script insise expect

#vi stat
#!/usr/bin/expect -f
spawn /bin/bash /home/ec2-user/stat.sh
#exec /home/ec2-user/stat.sh
expect "password"
send "Welcome@1234\r"
interact



Post a Comment

0 Comments