winpexpec: spawning telnet/ssh

2,041 views
Skip to first unread message

attilio

unread,
Mar 28, 2011, 12:13:22 PM3/28/11
to winpexpect
I 'm just started a open source project based on pexpect (http://
code.google.com/p/pyco/) and in an attempt to try winpexpect on
windows I encountered a problem.

On Microsoft Windows Xp the winpexpect package, with python 2.7, fails
to spawn the child process.

It is a known bugs?

See Below the details ...

C:\Documents and Settings\so000112\Desktop\pyco\docs>python
Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from winpexpect import winspawn
>>> import StringIO
>>> logfile = StringIO.StringIO()
>>> import sys
>>> sys.platform
'win32'
>>> c = winspawn('telnet kabul.sodalia.it', logfile=logfile)
>>> c.expect('.*')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\opt\Python27\lib\site-packages\winpexpect-1.5-py2.7.egg
\pexpect.py", line 1311, in expect
return self.expect_list(compiled_pattern_list, timeout,
searchwindowsize)
File "C:\opt\Python27\lib\site-packages\winpexpect-1.5-py2.7.egg
\pexpect.py", line 1325, in expect_list
return self.expect_loop(searcher_re(pattern_list), timeout,
searchwindowsize)
File "C:\opt\Python27\lib\site-packages\winpexpect-1.5-py2.7.egg
\pexpect.py", line 1396, in expect_loop
raise EOF (str(e) + '\n' + str(self))
pexpect.EOF: End of file in read_nonblocking().
<winpexpect.winspawn object at 0x011AF670>
version: 2.3 ($Revision: 399 $)
command: telnet
args: ['telnet', 'kabul.sodalia.it']
searcher: searcher_re:
0: re.compile(".*")
buffer (last 100 chars):
before (last 100 chars):
after: <class 'pexpect.EOF'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 712
child_fd: 4
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: <StringIO.StringIO instance at 0x011A9328>
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
>>> logfile.getvalue()
''
>>>

Jason Haury

unread,
Mar 29, 2011, 6:45:19 PM3/29/11
to winpe...@googlegroups.com
i think the problem is that expect() takes an array as an argument, not just a string.  it then returns an integer describing which element was matched from the array you passed.  

Try modifying your line from:
c.expect('.*') 
To:
c.expect(['.*'])  

If things are timing out or you're getting an unexpected EOF, it will also error out like what happened to you.  I suggest adding even more code like this:

index = c.expect(['.*', pexpect.TIMEOUT, pexpect.EOF]) 
print index 
 
This will let you know exactly what is happening (ie if you get 0 back, then it found some text correctly, 1 means you timed out and 2 means you got an EOF)

Jason
 
 

attilio

unread,
Mar 30, 2011, 3:47:12 PM3/30/11
to winpexpect
Hi Jason,

In my first post I semplified the problem to signal the issue, indeed
the real code is something like:

import sys
import StringIO #@UnresolvedImport
if sys.platform != 'win32':
from pexpect import spawn, TIMEOUT, EOF #@UnresolvedImport
else:
from winpexpect import winspawn as spawn, TIMEOUT, EOF
#@UnresolvedImport


... (omitted) ...

self.pipe = spawn(cmd, logfile=self.logfile)

where cmd is something like "telnet hostname 23"

Now on windows the spawn command blocks the process.

To reproduce the problem I have published a first version of pyco:
http://code.google.com/p/pyco/downloads/list

just download the package, extract and run:

python setup.py install

On python shell:

>> from netcube.device import device
>> host = device('telnet://a:b@localhost')
>> host.login()

The login may be slow because of internal timeouts, so it is not
immediate, but 5 seconds is the wait time to expect before the prompt
is eventually discovered and the login() returns.

this, in my environment (python 2.7 on win xp) blocks the shell,
instead on linux with pexpect it works as expected.

nice to hear from you ...
greetings
attilio

ps. if you try pyco a pyco.log file with DEBUG level is written on /
tmp, so you can see if the last command written is spawn.

purna pushkala

unread,
Jan 24, 2014, 2:32:02 AM1/24/14
to winpe...@googlegroups.com
Hi,
   I have installed winpexpect  in windows and tried below

import pexpect
import winpexpect
import os,sys
from winpexpect import *
p=winspawn('telnet 192.168.9.82')
p.expect([":",winpexpect.EOF,winpexpect.TIMEOUT],3)
p.sendline('admin\n')
p.expect([":",winpexpect.EOF,winpexpect.TIMEOUT],3)
p.sendline('private\n')
p.expect([">",winpexpect.EOF,winpexpect.TIMEOUT],3)
p.sendline('enable\n')
print p.before
print p.after
p.expect([">",winpexpect.EOF,winpexpect.TIMEOUT],3)
p.sendline('logout\n')
p.expect(["\)",winpexpect.EOF,winpexpect.TIMEOUT],3)
p.sendline('n\n')

output:

p.before=""
p.after="class <pexpect.EOF">



In this, I enabled telnet service. But I did'nt get prompt or interact. I need to print this. I dont know whether session is opened or not. Please help to solve this.


Reply all
Reply to author
Forward
0 new messages