Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Can any body 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 "#"

Hi,

I tried the below script its executing without errors, but i didnt get the output. Here is the script & the output when i executes. Where am i doing 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:~$
share|improve this question
add comment

3 Answers

up vote 3 down vote accepted
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}
    "#"
}

In Tcl (and, by extension, expect), curly braces act like the shell's single quotes: inhibit variable expansion.

The multi-pattern form of expect is useful for the case where you may not see a pattern. The exp_continue statement essentially "loops" within the expect so you can send the password and continue to expect the prompt. Since there is no action associated with the prompt pattern, control passes from the expect command to the next one.

I would recommend you save this as a separate script. First line should be

#!/usr/bin/expect -f

If you want to embed in a shell script:

#!/bin/sh
expect <<'END'
   # code as above
END

Note the quotes around the first "END" -- that has the effect of single quoting the entire here-document so you don't have to worry about the shell interpreting the Expect variables

share|improve this answer
    
Hi Glenn, the script is executing with no errors, but i'm unable to see the output of the scirpt. Am i doing any thing wrong in the script. If yes please let me know i save the output in separate file. here is the script that i'm executing 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:~$ –  sasuke Jun 14 '13 at 20:31
    
You can't post code in comments, please add it in your question –  glenn jackman Jun 14 '13 at 20:54
    
oh i'm sorry for that. Thanks i did it. Please help me out. –  sasuke Jun 15 '13 at 23:17
    
Don't know. Add exp_internal 1 to the top of the script and read the verbose output. –  glenn jackman Jun 16 '13 at 2:04
add comment

I would like to share my scirpt. I tried this on my system. this is working fine.

#!/usr/bin/expect
set username "myname"
set password "mypasswd"
set hosts "hostname"
foreach line [split $ip \n] {
spawn ssh -o StrictHostKeyChecking=no $username@$hosts
expect "$username@$hosts's password:"
send -- "$password\n"
expect "$"
send -- "sudo virsh list|awk '{print \$2}'|grep 'inmobi' >vm.list; for host in `cat vm.list`; do sudo virsh dominfo \$host >> vm.info; done\n"
expect "$"
send -- "$password\n"
expect "$"
send -- "exit\n"
share|improve this answer
add comment

I think you may need to backslash the quotes within the quotes. Otherwise expect thinks the command ends after "awk":

send "output=$(sudo virsh list --all | awk \"/running/{print \$2}\" | tail -2); sudo virsh dominfo $output"
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.