Sunday 15 July 2012

cisco ios - Expect Script - Starting logging from a certain point, and stop at a certain point -



cisco ios - Expect Script - Starting logging from a certain point, and stop at a certain point -

hope you're well!

i've begun putting little utility few our of network engineers use. it's function retrieve circuit info parsed , outputted in nice format. of course, part of involves ssh connection box required , running command generate desired output.

here's script i've got. works fine, runs desired command, sends few blank spaces prompt scheme display more circuits, , finishes off exiting. of beingness logged file name identical box's hostname. great.

however issue apparent when read file produced , see includes ton of data, including command ran , unnecessary stats provided on connection. i'm looking logging begin when issue command, , cutting off afterwards. i'm not familiar expect, i'd appreciate guidance.

ps. apologies if i've done stupid; i'm pretty new language , back upwards out there isn't great.

thanks.

set ip [lindex $argv 0] set hostname [lindex $argv 1] set timeout 10 set user "" set password "" # spawning ssh session spawn ssh $ip -l $user # awaiting prompt password expect "$user@$ip's password:" sleep 1; # awaiting prompt send "$password\n" sleep 2 log_file -noappend $hostname; send "terminal length 0\n" sleep 1 send "show int desc\n" sleep 5 send "exit\n" interact exit

you can command output means of placing log_file in desired place.

when want start logging, can add together line (which have in code)

log_file my_log_file.log

whatever printed in console logged after these command execution in file named my_log_file.log. default, if file nowadays in location, appended. if add together flag -noappend, overwrite existing file.

log_file -noappend fresh.log

if want stop logging, give log_file without arguments such

log_file

from point, whatever output generated not saved.

for example, logging switch , giving credentials , executing commands can follows,

spawn ssh some_ip expect some_thing #login code here log_file output.log; # start logging #some actions here log_file; # stopping logging here. # more actions # end

you sending spaces multiple times. instead, can set terminal length 0 follows,

send "term len 0" expect "prompt"

this avoid overhead of sending multiple times. also, in case, spaces sent switch fast, since there nil expect. if still interested without 'term len 0' , @ to the lowest degree can set code in loop, follows,

for { set 0 } { $i < 10 } { incr } { send " " expect "something" }

but, way of doing not advisable, since there possibility of need send more 10 spaces, fail.

expect cisco-ios

No comments:

Post a Comment