I`m trying to login to a server as userA then switch to the root account and run some command. The steps should be like:
I implemented step 1 with the code below but don't know how to implement steps 2 and 3.
#!/usr/bin/expect -f
set timeout 12
set password_root 12345678
set password_A 12345678
spawn ssh -t sflow@10.0.0.1
expect -re ".*password:"
send "$password_sflow\r"
expect eof
#!/usr/bin/expect
set timeout 12
set password_root 12345678
set password_A 12345678
set prompt "#|>|\\\$ $"
spawn ssh -t user1@xxx.xxx.x.xx
expect {
timeout {puts TIMEOUT}
"yes/no" {send "yes\r";exp_continue}
"password:" {send "user1password\r";exp_continue}
-re $prompt
}
send "su - root\r"
expect "Password:"
send "rootpassword\r"
expect -re $prompt
send "whoami\r"
expect -re $prompt
# and to exit
send "exit\r" ;# exit su
expect -re $prompt
send "exit\r" ;# exit ssh
expect eof