[TriLUG] expect question
jonc at nc.rr.com
jonc at nc.rr.com
Mon Mar 9 18:29:54 EDT 2009
Python!
#! /usr/bin/env python
#
import telnetlib
#
#
HOST = myrouter.network.com
LOGIN = "mypassword"
ENABLE = "myenablepw"
#
# tn = telnetlib.Telnet(HOST)
try:
tn = telnetlib.Telnet(HOST)
except Exception,e:
print " Problem accessing " + HOST + " (" + i[0] + "): Exception #" + str(e[0]) + " - " + e[1]
SKIP = 1
if ( SKIP == 0 ):
tn.read_until("Password:",10)
tn.write(LOGIN + "\n")
# Get rid of those nasty "-- more --" lines when looking at text
tn.write("terminal length 0 \n")
tn.write("enable \n")
tn.read_until("Password:",5)
tn.write(ENABLE + "\n")
tn.write("show config \n")
tn.write("exit\n")
tn.write("exit\n")
# print tn.read_all()
h = open("myrouterconf.txt", "w")
h.write(tn.read_all())
h.close()
tn.close()
#
---- Robert Dale <robdale at gmail.com> wrote:
> On Mon, Mar 9, 2009 at 2:36 PM, Greg Brown <gwbrown1 at gmail.com> wrote:
> So I've got a simple script. So far it logs into a router and goes to
> enable mode.
>
> I'd like to issue the command 'show run' then capture that output and write
> it to a log file. So far the expect script opens the log file but for the
> life of me I can't seem to capture the 'show run' output write that to the
> file.
>
> Can anyway share an example of how to do this?
#!/usr/bin/expect --
set log_file [open "foo.txt" "w"]
spawn w
expect eof {
puts $log_file $expect_out(buffer)
}
--
Robert Dale
--
TriLUG mailing list : http://www.trilug.org/mailman/listinfo/trilug
TriLUG FAQ : http://www.trilug.org/wiki/Frequently_Asked_Questions
More information about the TriLUG
mailing list