Need help with Expect script
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Need help with Expect script

From: Daryl Rose <jzlvr1@yahoo.com>
Date: Wed May 25 2005 - 16:29:48 CEST

I have about 500 servers, a mixture of Solaris and Redhat that I need
to get user information off. I have to use scp to copy the data off
and onto a central location. Expect seems to be the only answer to
getting scp to work, however, I have never worked with Expect or TCL.
Could someone take a look at my script and tell me what I am doing
wrong?
******************************************
#!/usr/local/bin/expect -d
set password(0) "sample"
set password(1) "test"
set password(2) "pswd"
set password(3) "getsome"
set password(4) "yeah"
set num_passwd [array size password]
set remote_file "/user/info"
set local_file "/root/daryl/user_info"
set hosts_file "/root/daryl/files/test.txt"

set file [open $hosts_file r]

while {[gets $file hosts] != -1} {
set count 0
spawn scp $hosts:$remote_file $local_file/$hosts:userinfo
        while {$count<$num_passwd} {
             expect "*assword:" {
                        send $password($count)\r}
                incr count
        }
        expect eof
}

close $file

puts "\r\r All done \r"

************************************************************
I have all of the servers in an external file that I open to read.
There are five possible passwords. I've put those into an array and
loop through them until the correct password is used.

This works fine if with just a single server, but as soon as I try
looping through the servers, all falls a part. The first server works
fine. Files copy over, but when I read the next server from the file
the script fails.

DEBUG Output:
**************************************************************

expect version 5.31.5
argv[0] = /usr/local/bin/expect argv[1] = -d argv[2] = ./test.sh
set argc 0
set argv0 "./test.sh"
set argv ""
executing commands from command file ./test.sh
spawn scp SERVER1:/user/info /root/daryl/user_info/SERVER1:userinfo
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {8766}

expect: does "" (spawn_id exp5) match glob pattern "*assword:"? no
Password:
expect: does "Password: " (spawn_id exp5) match glob pattern
"*assword:"? yes
expect: set expect_out(0,string) "Password:"
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) "Password:"
send: sending "sample\r" to { exp5 }
expect: does " " (spawn_id exp5) match glob pattern "*assword:"? no
expect: does " \r\n" (spawn_id exp5) match glob pattern "*assword:"? no
passwd
0% 0 0.0KB/s --:-- ETA
expect: does " \r\n\rpasswd

0% 0 0.0KB/s --:-- ETA" (spawn_id exp5) match glob pattern
"*assword:"? no
asswd
100% 690 0.0KB/s 00:00
expect: does " \r\n\rpasswd

0% 0 0.0KB/s --:-- ETA\rpasswd

100% 690 0.0KB/s 00:00 \r\n" (spawn_id exp5) match glob
pattern "*assword:"? no
expect: read eof
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) " \r\n\rpasswd

0% 0 0.0KB/s --:-- ETA\rpasswd

100% 690 0.0KB/s 00:00 \r\n"
expect: spawn id exp5 not open
    while executing
"expect "*assword:" {
        send $password($count)\r}"
    ("while" body line 5)
    invoked from within
"while {[gets $file hosts] != -1} {
set count 0
spawn scp $hosts:$remote_file $local_file/$hosts:passwd
        while {$count<$num_passwd} {
             expect "*as..."
    (file "./test.sh" line 14)
******************************************************

Any help would be appreciated.

Thank you.

Daryl Rose
Received on Thu Sep 29 14:18:58 2005