How to execute sudo commands with Expect & send commands in bash script?

Tags:

Answer: 1

26 hours ago

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 [email protected]$addr
expect "[email protected]$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 [email protected]$addr
expect "[email protected]$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

[email protected]:~$ ./hypr.sh 
spawn ssh [email protected] 
[email protected]'s password: 
[email protected]:~$ output=$(sudo virsh list --all | awk '/running/{print $2}' | tail -10)sudo virsh dominfo '$output' [email protected]:~$
Added by: Javonte Rippin

Answer: 2

25 hours ago

set login "sasuke"
set addr "hostname"
set pw "mypasswd"
spawn ssh [email protected]$addr
expect "[email protected]$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

Added by: Alda Graham

Answer: 3

3 hours ago

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 [email protected]$hosts
expect "[email protected]$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"

Answer: 4

27 hours ago

Try to end it with the "interact" command it should work.

spawn ssh -l $username $ip -p $sshport
sleep 5
expect "password:"
send "$pass\r"
interact
Added by: Paige Bartoletti

Answer: 5

20 hours ago

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"

Popular Search

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9