hello,

i made a routine in expect that connects via ssh to a juniper firewall, makes a backup of the configuration and saves it into a text file, so far it's working fine, and since it was working so fine i decided to add it to cron in order to run it automatically every 14 days.

but now i have a problem, i use the standard output in order to save the text file, but when the script reads the text file that contains the ips of the firewalls its supposed to connect to, it saves the configuration of the last firewall it connected, i mean, i want it to save all the configurations of all the firewalls it connects.

heres the expect script

#!/usr/bin/expect -f
#
set arch [open ip.txt r]
while {[gets $arch LINEA] >= 0} {
 set IP       [lindex $LINEA 0]
 set USER  [lindex $LINEA 1]
 set PASSWORD [lindex $LINEA 2]
 set COM  [lindex $LINEA 3]
 set PROMPT   [lindex $LINEA 4]
}
set force_conservative 0  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .001}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
}
set timeout 2
puts "\n"
#
spawn $COM $IP
expect {
 -exact "conection refused" {puts "\n";exit}
 -exact "Host name lookup failure" {puts "\n";exit}
 -exact "Name (localhost:root):*"
}
send -- "$USER\r"
expect "password:"
send -- "$PASSWORD\r"
expect -exact "$PROMPT"
send -- "get config\r"
send -- "show test\r"
expect {
        -exact  "more" {send -- "\r";exp_continue}
        "$PROMPT" {}
        }
puts "\n"
exit

and here's how i get the standard output

expect firewall.exp > javier-$(date +%d%b%Y).txt

and here's the ip.txt in wich i store the ip's of the firewalls
192.168.127.1 javier javier telnet >
192.168.127.2 javier javier ssh >

i was kind of hoping anyone could help me to store all the configuration of all the firewalls instead of overwriting in the same text file....please!!!! :-|

Let's start here:

expect firewall.exp > javier-$(date +%d%b%Y).txt

should be:

expect firewall.exp >> javier-$(date +%d%b%Y).txt

I'm not clear about the ip thing at the moment, this part should work for you.

hello, thanx for answering to my prayers...

why should be >>?, i mean what's the difference? i get exactly the smae file as with > (or i think so), anyway the ip part is as follows:

i have, let's say, 3 firewalls, and i'm telling the expect script to look in the ip.txt for the ips, logins and passwords of the dispositives it must connect to.

but so far i've just acomplished that the script connects to all of the ips (using the correct user and password) but just saves the last configuration that it got, and i want it to save a text files for each ip it connects to. (i don't know how) :(

ps. sorry for my bad english

>> means append to an existing file, do not overwrite.

I think it may solve your ip issue as well. What I'm saying: your expect script looks okay, your problem seems to be how you are invoking it from the command line.

thankx again,

but in the case it appends to an existing file, would store the configuration of the last firewall it has connected, and i want it to store the configurations in different files, so each time it connects to a firewall i will get a different file...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.