有人可以让我知道如何执行sudo
命令expect
吗?我尝试了以下操作,但这不起作用。有人可以给我建议吗?
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 "#"
当我尝试下面的脚本时,它没有错误地执行,但是我没有得到输出。这是脚本及其执行时的输出。我在哪里犯错?
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}
"#"
}
执行
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:~$