Expect script to save configuration from a router

Hy guys,

My name is Alex, i am new here and I hope to find some answers. I am trying to run a expect script to telnet to a mikrotik router, run a command (export), and save the output of that commant to a file (outputfile.txt). The problem is that only part of the output is saved to outputfile.txt.

Here is my code:

#!/usr/bin/expect -f
exp_internal 0
log_user 0
match_max -d 1000000

set timeout 30
spawn telnet 192.168.255.100
expect "Login: "
send "mktbk\r"
expect "Password: "
send "password\r"
expect "> "
send "export\r"
expect eof
puts [open outputfile.txt w] $expect_out(buffer)
expect "> "
send "quit\r"

I guess it's a buffer problem ..... got any ideas?

Thanks,
Alex

Is the saved output (in the file), always the same or does the content vary?

Hy,

I got that figured out, right now I am trying to do a foreach loop, I want this script to read a file that have some hosts in it and save the config for each one:

Here is the code that I am trying:

#!/usr/bin/tclsh
package require Expect
log_user 0
match_max -d 10000000
set timeout 30

set tdate [clock format [clock seconds] -format %Y%m%d]

#set host 192.168.255.100
set user mktbk\r
set pass password\r


foreach {host} {argv0} {





set name $tdate-$host
spawn telnet $host

expect "Login: "
exp_send $user

expect "Password: "
exp_send $pass

expect "> "
exp_send "export\r"

expect "> "
exp_send "quit\r"
expect eof

set fd [ open $name w ]
puts $fd $expect_out(buffer)
close $fd

}

The error that i get is:

[alex@samba scripts]$ ./script.sh host.txt 
send: spawn id exp4 not open
    while executing
"exp_send $user"
    ("foreach" body line 11)
    invoked from within
"foreach {host} {argv0} {





set name $tdate-$host     
spawn telnet $host

expect "Login: " 
exp_send $user

expect "Password: "
exp_send $pass

exp..."
    (file "./script.sh" line 14)
[alex@samba scripts]$ 

Got any ideas?

Thanks