ios - Unexpected behaviours with TCL/Expect and Cisco -
i'm trying log cisco switch , run list of commands. using next code, i'm able log device, enable, , configure terminal:
# connect single host, enable, , configure proc connect {host payload username password enablepassword} { send_user "connecting to: $host $payload $username $password $enablepassword\n" spawn ssh -o "stricthostkeychecking no" -l $username $host # # pardon rudeness; switches upper case, lower case expect "assword:" send "$password\r" # switch enable mode expect ">" send "en\r" expect "assword:" send "$enablepassword\r" expect "*#" send -- "conf t\r" expect "config*#" } however, using next code, output below. ($payload contains file has 1 ios command per line)
proc drop_payload {payload} { set f [open "$payload"] set payload [split [read $f] "\n"] close $f foreach pld $payload { send -- "$pld\r" expect "config*#" sleep 2 } } my expectation loop iterate on each line in file, however, expect debug (from exp_internal 1) follows:
host-0001# expect: " \r\host-0001#" (spawn_id exp7) match glob pattern "*#"? yes expect: set expect_out(0,string) " \r\nhost-0001#" expect: set expect_out(spawn_id) "exp7" expect: set expect_out(buffer) " \r\nhost-0001#" send: sending "conf t\r" { exp7 } expect: "" (spawn_id exp7) match glob pattern "config*#"? no c expect: "c" (spawn_id exp7) match glob pattern "config*#"? no o expect: "co" (spawn_id exp7) match glob pattern "config*#"? no n expect: "con" (spawn_id exp7) match glob pattern "config*#"? no f expect: "conf" (spawn_id exp7) match glob pattern "config*#"? no expect: "conf " (spawn_id exp7) match glob pattern "config*#"? no t expect: "conf t" (spawn_id exp7) match glob pattern "config*#"? no expect: "conf t\r\n" (spawn_id exp7) match glob pattern "config*#"? no come in configuration commands, 1 per line. end cntl/z. host-0001(config)# expect: "conf t\r\nenter configuration commands, 1 per line. end cntl/z.\r\nhost-0001(config)#" (spawn_id exp7) match glob pattern "config*#"? yes expect: set expect_out(0,string) "configuration commands, 1 per line. end cntl/z.\r\nhost-0001(config)#" expect: set expect_out(spawn_id) "exp7" expect: set expect_out(buffer) "conf t\r\nenter configuration commands, 1 per line. end cntl/z.\r\nhost-0001(config)#" }end: sending "no logging 172.x.x.20\r" { exp0 no logging 172.x.x.20 expect: "" (spawn_id exp0) match glob pattern "config*#"? no expect: timed out }end: sending "no logging 172.x.x.210\r" { exp0 no logging 172.x.x.210 expect: "" (spawn_id exp0) match glob pattern "config*#"? no expect: timed out }end: sending "no logging 172.x.x.9\r" { exp0 no logging 172.x.x.9 expect: "" (spawn_id exp0) match glob pattern "config*#"? no expect: timed out }end: sending "no logging 172.x.x.210\r" { exp0 no logging 172.x.x.210 expect: "" (spawn_id exp0) match glob pattern "config*#"? no expect: timed out }end: sending "no logging 172.x.x.20\r" { exp0 no logging 172.x.x.20 expect: "" (spawn_id exp0) match glob pattern "config*#"? no expect: timed out }end: sending "logging 172.x.x.50\r" { exp0 logging 172.x.x.50 expect: "" (spawn_id exp0) match glob pattern "config*#"? no expect: timed out i'm confused why it's trying expect "conf t" beingness sent host; not received. i'm confused why of commands end after conf t applied don't nail switch, , time out instead.
you can seek sending configurations spwan_id
spawn ssh -o "stricthostkeychecking no" -l $username $host #after process creation process id saved in #standard expect variable'spawn_id' #copying variable 'id' set id $spawn_id now variable 'id' holding reference ssh process. can utilize send , expect spawn id.
#now setting spawn id our ssh process create sure #we sending commands right process #you can pass variable 'id' arg in 'drop_payload' set spawn_id $id foreach pld $payload { send -- "$pld\r" expect "config*#" sleep 2 } or other way around follows,
foreach pld $payload { #this way useful, when u want send , expect multiple process #simultaneously. send -i $id "$pld\r" expect -i $id "config*#" sleep 2 } ios tcl expect cisco
No comments:
Post a Comment