Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76284 > unrolled thread

pexpect - logging input AND output

Started bysj.constantine@gmail.com
First post2014-08-14 01:59 -0700
Last post2014-08-20 18:51 +0400
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  pexpect - logging input AND output sj.constantine@gmail.com - 2014-08-14 01:59 -0700
    Re: pexpect - logging input AND output Chris Angelico <rosuav@gmail.com> - 2014-08-14 19:14 +1000
    Re: pexpect - logging input AND output Akira Li <4kir4.1i@gmail.com> - 2014-08-20 18:51 +0400

#76284 — pexpect - logging input AND output

Fromsj.constantine@gmail.com
Date2014-08-14 01:59 -0700
Subjectpexpect - logging input AND output
Message-ID<f6f7175a-37cc-44d0-9e62-b237f51d6ac8@googlegroups.com>
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!

[toc] | [next] | [standalone]


#76286

FromChris Angelico <rosuav@gmail.com>
Date2014-08-14 19:14 +1000
Message-ID<mailman.12986.1408007694.18130.python-list@python.org>
In reply to#76284
On Thu, Aug 14, 2014 at 6:59 PM,  <sj.constantine@gmail.com> wrote:
> 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)

If that's not working for you, would it be easier instead to simply
open a socket connection to port 23 on that IP address? Then you'd
just write to the socket (and log what you write) and read from it
(and log that). It's possible you'll see some TELNET or ANSI codes
coming back, but I expect you won't have to send any of them.

ChrisA

[toc] | [prev] | [next] | [standalone]


#76668

FromAkira Li <4kir4.1i@gmail.com>
Date2014-08-20 18:51 +0400
Message-ID<mailman.13212.1408546304.18130.python-list@python.org>
In reply to#76284
sj.constantine@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web