automation, configuration, network components, scripting, Uncategorized

script on expect language to push batch changes to cisco and aruba switches / routers

Notes about the script:

– in such particular case script pushes new syslog server 10.10.10.21 to configuration and saves config file
– script works almost without changes on cisco and aruba (on cisco though syntax is “logging host <…>” on aruba it is “logging <…>”)
– script will terminate itself with error if it will be not able to establish ssh to any of mentioned switches/routers where it needs to do changes
– logging from aruba is a mambo-jumbo and not accepted as a solution (the same result I get if doing logging via tee command of Expect script and with Expect’s logging possibility by log_file command (in my script its log_file -a $Directory/session_$host.log) )
– script expects that directory /tmp/logs exists, hence either it should be created manually or by adjusting provided script
– “log_file” command in script closes logging for each host, otherwise I got complaints from Expect related to not closed logging
– script reads IP addresses of each aruba / cisco switch or router from a file, which is given as a parameter to a script:

case for cisco
./set_syslog_on_cisco_v1.0.ex /tmp/cisco.txt | tee /tmp/cisco_syslog.log

case for aruba
./set_syslog_on_aruba_v1.0.ex /tmp/aruba.txt | tee /tmp/aruba_syslog.log

below is example of script for aruba case, for cisco case one line should be changed:

compare

 

#!/usr/bin/expect -f

set timeout 20

set file [lindex $argv 0];

set f [open “$file”]

set hosts [split [read $f] “n”]

close $f

foreach host $hosts {

if {$host != “”} {

send “echo host is $hostr”

sleep 2

set Username “your_username”

set Password “your_password”

set Directory /tmp/logs

log_file -a $Directory/session_$host.log

send_log “### /START-SSH-SESSION/ IP: $host @ [exec date] ###r”

spawn ssh -o “StrictHostKeyChecking no” $Username@$host

expect “*assword: ”

send “$Passwordr”

expect “#”

send “conf tr”

expect “(config)#”

send “logging 10.10.10.21r”

expect “(config)#”

send “endr”

expect “#”

send “wr memr”

expect “#”
send “r”

send “logoutr”

sleep 2

send_log “r### /END-SSH-SESSION/ IP: $host @ [exec date] ###r”

log_file;
}
}