pexpect - logging input AND output

Akira Li 4kir4.1i at gmail.com
Wed Aug 20 10:51:25 EDT 2014


sj.constantine at gmail.com writes:

> i have a script running a few commands on a network device. i can't
> seem to figure out how to log both the input and output of what the
> pexpect script initiates and responds to.
>
> child = pexpect.spawn ('telnet '+ ip)
> child.expect ('.*:*')
> child.sendline (user)
> child.expect ('.*:*')
> child.sendline (password)
> child.expect(['.*#*', '.*>*'])
> child.sendline ('enable')
> child.expect (['Password:', '.*#*'])
> child.sendline (password)
> child.expect ('.*#*')
> child.sendline ('conf t')
> child.expect ('.*#*')
> child.sendline ('line vty 0 4')
>
> i have tried both these logging commands:
>
> child.logfile = open('log.txt', 'w')
> child.logfile=sys.stdout
>
> all i get is the input i send with expect/sendline combinations, i
> don't get any of what the device sends, only what i send the device:
>
> user
> password
> enable
> password
> conf t
> line vty 0 4
>
> any ideas of what is the correct way to go about this? just can't get the output!

To be clear, expect() doesn't send anything to the device. expect()
matches as little as possible therefore '.*:*' matches *nothing*.

If it is Python 3 then use pexpect.spawnu(). Otherwise, assigning to
child.logfile should work as is.

There is a telnetlib module in stdlib. x/84 python telnet server might
contain a client too.


--
Akira




More information about the Python-list mailing list