You are not logged in.

#1 2013-09-20 02:14:41

fukawi2
Forum Moderator
From: .vic.au
Registered: 2007-09-28
Posts: 5,268
Website

[SOLVED] expect steals my loop data

I'm working on a script to automatically backup my switch configurations via scp (keys are not an option here) so I need to use 'expect'. It appears expect is stealing the stdin data from my loop though.

Here's sample code that demonstrates the issue, note that 'expect' has the debug flag:

#!/bin/bash 
set -e
set -u

cat test.conf | while read name addr pw ; do
  expect -dc "
    set timeout 30
    spawn scp admin@$addr:/cfg/startup-config ${name}_startup-config
    expect \"password: \"
    send \"$pw\r\"
    interact
  "
done

And test.conf looks like this:

line1   address1  password1
line2   address2  password2
line3   address3  password3
line4   address4  password4
line5   address5  password5

Now when I run the script, after expect connects to the host and sends the password, it then 'steals' the rest of the data from the `while read` loop on the "spawn id" line of the debug output (eg, line2, address2 and so on) so my loop never gets to it's second (or third, forth etc) iteration:

expect version 5.45
spawn scp admin@address1:/cfg/startup-config line1_startup-config
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {14182}

expect: does "" (spawn_id exp6) match glob pattern "password: "? no
admin@address1's password: 
expect: does "admin@address1's password: " (spawn_id exp6) match glob pattern "password: "? yes
expect: set expect_out(0,string) "password: "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "admin@address1's password: "
send: sending "password1\r" to { exp6 }
spawn id exp0 sent <line2   address2  password2\nline3   address3  password3\n#line4   address4  password4\nline5   address5  password5\n>
interact: received eof from spawn_id exp0
argv[0] = expect  argv[1] = -dc  argv[2] = 
    set timeout 30
    spawn scp admin@address1:/cfg/startup-config line1_startup-config
    expect "password: "
    send "password1\r"
    interact
    
set argc 0
set argv0 "expect"
set argv ""

Any idea how I can stop 'expect' stealing my stdin? sad

Last edited by fukawi2 (2013-09-20 03:38:07)

Offline

#2 2013-09-20 03:37:58

fukawi2
Forum Moderator
From: .vic.au
Registered: 2007-09-28
Posts: 5,268
Website

Re: [SOLVED] expect steals my loop data

Ah-ha! Changing 'interact' to 'expect eof' in the expect commands makes this work as expected (no pun intended) big_smile

Offline

Board footer

Powered by FluxBB