Can anybody let me know how to execute sudo
commands with expect
? I tried the following but this is not working. Can anyone give me suggestion please?
set login "sasuke"
set addr "hostname"
set pw "mypasswd"
spawn ssh $login@$addr
expect "$login@$addr\'s password:"
send "$pw\r"
expect "#"
send "output=$(sudo virsh list --all | awk "/running/{print \$2}" | tail -2); sudo virsh dominfo $output"
expect "password:"
send "$pw\r"
expect "#"
When I tried the below script, it executed without errors, but I didn't get the output. Here is the script and the output when it executes. Where am I making a mistake here?
set login "sasuke"
set addr "hostname"
set pw "mypasswd"
spawn ssh $login@$addr
expect "$login@$addr's password:"
send "$pw\r"
expect "#"
send {output=$(sudo virsh list --all | awk '/running/{print $2}' | tail -2)}
expect {
password: {send "$pw\r"; exp_continue}
"#"
}
send {sudo virsh dominfo "$output"} ;# don't know if you need quotes there
expect {
password: {send "$pw\r"; exp_continue}
"#"
}
Execution
sasuke@njob:~$ ./hypr.sh
spawn ssh sasuke@hostname
sasuke@hostname's password:
sasuke@hostname:~$ output=$(sudo virsh list --all | awk '/running/{print $2}' | tail -10)sudo virsh dominfo '$output' sasuke@njob:~$