Results 1 to 1 of 1
All,
I am new to expect and trying to create a script that ssh's into cisco routers to activate/ deactivate neighbors if certain conditions are met. I got the logic ...
- 08-23-2010 #1Just Joined!
- Join Date
- Aug 2010
- Posts
- 1
Need expect help, ssh script to automate cisco commands
All,
I am new to expect and trying to create a script that ssh's into cisco routers to activate/ deactivate neighbors if certain conditions are met. I got the logic working independently but my "if" "foreach" are not working as they should. Any help is appreciated. Below is my script
#!/usr/bin/expect
set password "testuser"
set chirtr {10.10.136.13 10.10.136.14 10.10.136.15}
set sfortr {10.20.87.13 10.20.87.14 10.20.87.15}
set testcustname [lindex $argv 2]
set testsite [lindex $argv 3]
set custIP [lindex $argv 4]
set custprefix [lindex $argv 5]
set custmode [lindex $argv 6]
send_user "Enter test Customer to activate\(eg.abcd\):"
sleep 5
expect_user {
-re "(.*)\n" {
set testcustname $expect_out(0,string)
}
}
send_user "Enter test scrubbing center\(eg. chi or sfo\):"
sleep 5
expect_user {
-re "(.*)\n" {
set testsite $expect_out(0,string)
}
}
send_user "Enter Customer IP under activate\(eg. 192.168.1.10):"
sleep 10
expect_user {
-re "-?(0|\[1-9]\[0-9]*)?\\.?\[0-9]*" {
set custIP $expect_out(0,string)
}
}
send_user "Enter customer prefix \(eg. 192.168.1.0)\:"
sleep 10
expect_user {
-re "-?(0|\[1-9]\[0-9]*)?\\.?\[0-9]*" {
set custprefix $expect_out(0,string)
}
}
send_user "activate or deactivate \(eg. activate)\:"
sleep 5
expect_user {
-re "(.*)\n" {
set custmode $expect_out(0,string)
}
}
## ACTIVATION ##
if { [$custmode == activate] && [$testsite==chi] }
foreach host $chirtr
{
puts $host
spawn ssh testadmin@$host
match_max 3
expect "*password:" {send "password \n}
expect "*#" {send "conf t\n"}
expect "*#"
send -- "router bgp xxx\r"
expect "*#"
sleep1
send -- "activate $custIP\r"
}
spawn ssh testadmin@10.10.136.16
match_max 1
expect "*password:" {send "$password \n"}
expect "*#" {send "conf t\n"}
expect "*#"
send -- "router bgp xxxx \r"
expect "*#"
sleep 1
send -- "network $custprefix 255.255.255.0\n"
expect "*#"
##DEACTIVATION##
if { [$custmode == deactivate] && [$testsite==chi] }
foreach host $chirtr {
puts $host
spawn ssh testadmin@$host
match_max 3
expect "*password:" {send "password \n}
expect "*#" {send "deactivate $testcust\n"}
sleep 1
expect "*#"
}
spawn ssh testadmin@10.20.87.16
match_max 1
expect "*password:" {send "$password \n"}
expect "*#" {send "deactivate $testcust\n"}
sleep 1
expect "*#"
################################################## ##
##SFO ACTIVATION##
if { [$custmode == activate] && [$testsite==sfo1] }
foreach entry $sfo1rtr {
puts $entry
spawn ssh testadmin@$entry
match_max 3
expect "*password:" {send "password \n}
expect "*#" {send "conf t\n"}
expect "*#"
send -- "router bgp yyyyy\r"
expect "*#"
sleep1
send -- "$custIP activate\r"
}
spawn ssh testadmin@10.20.87.16
match_max 1
expect "*password:" {send "$password \n"}
expect "*#" {send "conf t\n"}
expect "*#"
send -- "router bgp yyyyy\r"
expect "*#"
sleep 1
send -- "ip address $custprefix 255.255.255.0\n"
expect "*#"
##DEACTIVATION##
if { [$custmode == deactivate] && [$testsite==sfo1] }
foreach entry $sfo1rtr {
puts $entry
spawn ssh testadmin@$entry
match_max 3
expect "*password:" {send "password \n}
expect "*#" {send "deactivate $testcust\n"}
sleep 1
expect "*#"
}
spawn ssh testadmin@10.20.87.16
match_max 1
expect "*password:" {send "$password \n"}
expect "*#" {send "deactivate $testcust\n"}
sleep 1
expect "*#"
expect eofLast edited by blahblahsomeone; 08-23-2010 at 09:33 PM. Reason: Update


Reply With Quote