Pexpect and telnet not communicating properly


D

David Anderson

I am trying to automate the following session - to talk to my router:

=======================
telnet speedtouch
Trying 192.168.1.254...
Connected to speedtouch.
Escape character is '^]'.
Username : Administrator
Password :
------------------------------------------------------------------------

______ SpeedTouch 780
___/_____/\
/ /\ 6.1.7.2
_____/__ / \
_/ /\_____/___ \ Copyright (c) 1999-2006, THOMSON
// / \ /\ \
_______//_______/ \ / _\/______
/ / \ \ / / / /\
__/ / \ \ / / / / _\__
/ / / \_______\/ / / / / /\
/_/______/___________________/ /________/ /___/ \
\ \ \ ___________ \ \ \ \ \ /
\_\ \ / /\ \ \ \ \___\/
\ \/ / \ \ \ \ /
\_____/ / \ \ \________\/
/__________/ \ \ /
\ _____ \ /_____\/
\ / /\ \ /___\/
/____/ \ \ /
\ \ /___\/
\____\/

------------------------------------------------------------------------
_{Administrator}=>?
Following commands are available :

help : Displays this help information
menu : Displays menu
? : Displays this help information
exit : Exits this shell.
... : Exits group selection.
saveall : Saves current configuration.
ping : Send ICMP ECHO_REQUEST packets.
traceroute : Send ICMP/UDP packets to trace the ip path.

Following command groups are available :

firewall service autopvc connection cwmp
dhcp dns dsd dyndns eth
adsl atm config debug env
expr grp hostmgr ids igmp
interface ip ipqos label language
mbus memm mlp nat ppp
pptp script snmp sntp software
system systemlog upgrade upnp user
voice wireless

{Administrator}=>exit

========================

I am using the following code:

#!/usr/bin/env python

import pexpect
import sys

child = pexpect.spawn('telnet 192.168.1.254')
fout = file('mylog.txt','w')
child.logfile = fout

child.expect('sername : ')
child.sendline('Administrator')
child.expect('assword : ')
child.sendline('')


child.expect('_{Administrator}=>')
child.sendline('?')
child.expect('_{Administrator}=>')
child.expect('exit')

========================

This times out after child.sendline('Administrator')

mylog.txt contains:
Trying 192.168.1.254...

Connected to 192.168.1.254.

Escape character is '^]'.

Username : Administrator
Administrator
========================

Can anyone help me in finding out what I am doing wrong?

Regards
David
 
Ad

Advertisements

G

George Trojan

David said:
I am trying to automate the following session - to talk to my router:

=======================
telnet speedtouch
Trying 192.168.1.254...
Connected to speedtouch.
Escape character is '^]'.
Username : Administrator
Password :
------------------------------------------------------------------------

______ SpeedTouch 780
___/_____/\
/ /\ 6.1.7.2
_____/__ / \
_/ /\_____/___ \ Copyright (c) 1999-2006, THOMSON
// / \ /\ \
_______//_______/ \ / _\/______
/ / \ \ / / / /\
__/ / \ \ / / / / _\__
/ / / \_______\/ / / / / /\
/_/______/___________________/ /________/ /___/ \
\ \ \ ___________ \ \ \ \ \ /
\_\ \ / /\ \ \ \ \___\/
\ \/ / \ \ \ \ /
\_____/ / \ \ \________\/
/__________/ \ \ /
\ _____ \ /_____\/
\ / /\ \ /___\/
/____/ \ \ /
\ \ /___\/
\____\/

------------------------------------------------------------------------
_{Administrator}=>?
Following commands are available :

help : Displays this help information
menu : Displays menu
? : Displays this help information
exit : Exits this shell.
.. : Exits group selection.
saveall : Saves current configuration.
ping : Send ICMP ECHO_REQUEST packets.
traceroute : Send ICMP/UDP packets to trace the ip path.

Following command groups are available :

firewall service autopvc connection cwmp
dhcp dns dsd dyndns eth
adsl atm config debug env
expr grp hostmgr ids igmp
interface ip ipqos label language
mbus memm mlp nat ppp
pptp script snmp sntp software
system systemlog upgrade upnp user
voice wireless

{Administrator}=>exit

========================

I am using the following code:

#!/usr/bin/env python

import pexpect
import sys

child = pexpect.spawn('telnet 192.168.1.254')
fout = file('mylog.txt','w')
child.logfile = fout

child.expect('sername : ')
child.sendline('Administrator')
child.expect('assword : ')
child.sendline('')


child.expect('_{Administrator}=>')
child.sendline('?')
child.expect('_{Administrator}=>')
child.expect('exit')

========================

This times out after child.sendline('Administrator')

mylog.txt contains:
Trying 192.168.1.254...

Connected to 192.168.1.254.

Escape character is '^]'.

Username : Administrator
Administrator
========================

Can anyone help me in finding out what I am doing wrong?

Regards
David

To debug, add lines

print self.before
print self.after

after each child.expect(). Also, add timeout=2 to the argument list of
child.expect().

I wrote a thin wrapper that serves me well, at least till now.


class Connection(object):
'''Establishes connection to Cisco modem server.
A wrapper around fdexpect spawn.
'''
def __init__(self, host, port, user, passwd, **kwds):
self.pipe = None
self.socket = None
self.host = host
self.port = port
self.user = user
self.passwd = passwd
self.logger = kwds.get('logger')
self._last = ''

def __getattr__(self, name):
if name not in ['open', 'close', 'send', 'sendline', 'expect']:
return getattr(self.pipe, name)

def open(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((self.host, self.port))
self.pipe = pexpect.fdspawn(self.socket)
self.expect('Username:', timeout=2)
self.sendline(self.user)
self.expect('Password:', timeout=1)
self.sendline(self.passwd)
self.send('ATZ\r')
self.expect('OK', timeout=1)

def send(self, s):
self._last = s
return self.pipe.send(s)

def sendcr(self, s):
self._last = s
return self.pipe.send(s+'\r')

def sendline(self, s):
self._last = s
return self.pipe.sendline(s)

def expect(self, pattern, **kwds):
rc = self.pipe.expect(pattern, **kwds)
if self.logger:
self.logger.debug('sent "%s", received\n\t1. before "%s"\n\t' \
'2. match "%s"\n\t3. after "%s"\n', self._last,
self.before, self.match.group(0), self.after)
return rc

def close(self):
self.pipe.close()
self.pipe = None
self.socket.close()
self.socket = None

George
 
Ad

Advertisements

M

Mark Wooding

David Anderson said:
I am trying to automate the following session - to talk to my router:

telnet speedtouch [...]
I am using the following code: [...]
child.expect('sername : ')
child.sendline('Administrator')

I've scripted communications with my Speedtouch 510 using pexpect: I use
it to send the router's logs to my server. The obvious difference
between my script and yours is that instead of sendline, I used send and
an explicit carriage return.

kid.expect('Username :')
kid.send('admin\r\n')

I think this is a result of a similar problem, but I don't remember any
more.

Anyway, I've put my (very simple) script up at

http://www.distorted.org.uk/~mdw/example/guvnor-maintenance

in case you're interested. (No, I wasn't daft enough to put the
password in the script!)

-- [mdw]
 

Top