1

How can I get the command output from the buffer? Current'y it's just saying "(buffer).

Here is my script and debug output:

#!/bin/bash -x
while read p; do
echo $p
{
    /usr/bin/expect -d << EOF
    match_max 10000
    spawn ssh anthony@$p
    expect "password:"
    send "MyPasswordHere\r"
    expect " "
    send "ifconfig |grep -i eth0\r"
    expect " "
    puts "The output is $expect_out(buffer) "
    send "exit\r"
EOF
}
done <List.txt


+ read p
+ echo localhost
localhost
+ /usr/bin/expect -d
expect version 5.45
argv[0] = /usr/bin/expect  argv[1] = -d  
set argc 0
set argv0 "/usr/bin/expect"
set argv ""
executing commands from command file
spawn ssh anthony@localhost
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {2032}

expect: does "" (spawn_id exp15) match glob pattern "password:"? no
anthony@localhost's password: 
expect: does "anthony@localhost's password: " (spawn_id exp15) match glob pattern "password:"? yes
expect: set expect_out(0,string) "password:"
expect: set expect_out(spawn_id) "exp15"
expect: set expect_out(buffer) "anthony@localhost's password:"
send: sending "M00nsh0t@\r" to { exp15 }

expect: does " " (spawn_id exp15) match glob pattern " "? yes
expect: set expect_out(0,string) " "
expect: set expect_out(spawn_id) "exp15"
expect: set expect_out(buffer) " "
send: sending "ifconfig |grep -i eth0\r" to { exp15 }

expect: does "" (spawn_id exp15) match glob pattern " "? no


expect: does "\r\n" (spawn_id exp15) match glob pattern " "? no
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-40-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

Last login: Sun Dec 21 09:23:38 2014 from localhost

expect: does "\r\nWelcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-40-generic x86_64)\r\n\r\n * Documentation:  https://help.ubuntu.com/\r\n\r\nLast login: Sun Dec 21 09:23:38 2014 from localhost\r\r\n" (spawn_id exp15) match glob pattern " "? yes
expect: set expect_out(0,string) " "
expect: set expect_out(spawn_id) "exp15"
expect: set expect_out(buffer) "\r\nWelcome "
The output is (buffer) 
send: sending "exit\r" to { exp15 }
+ read p
3

Since your expect script is in an unquoted heredoc, your expect variable is being handled as a shell variable. Change

puts "The output is $expect_out(buffer) "

to

puts "The output is \$expect_out(buffer) "

The output you see, (The output is (buffer)) is exactly what I'd expect: the shell is expanding $expect_out to nothing before handing the script to expect.

  • That makes complete sense, thanks. My output is still not as expected though: expect: does "" (spawn_id exp15) match glob pattern " "? no ifconfig |grep -i eth0' expect: does "\r\nifconfig |grep -i eth0'\r\n" (spawn_id exp15) match glob pattern " "? yes expect: set expect_out(0,string) " " expect: set expect_out(spawn_id) "exp15" expect: set expect_out(buffer) "\r\nifconfig " The output is ifconfig send: sending "exit\r" to { exp15 } + read p – Anthony52 Dec 22 '14 at 4:23
  • The output is just "ifconfig" now. – Anthony52 Dec 22 '14 at 4:25
  • You need to make your expect " " more specific to the prompt. The command gets echoed by expect, and you are matching the space in the command ifconfig |grep ... – glenn jackman Dec 22 '14 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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