1

I've used pexpect on linux successfully to telnet/ssh into Cisco switches for CLI scrapping. I'm trying to convert this code over to Windows and having some issues since it doesn't support the pxpect.spawn() command.

I read some online documentation and it suggested to use the pexpect.popen_spawn.PopenSpawn command. Can someone please point to me what I'm doing wrong here? I removed all my exception handling to simplify the code. Thanks.

import pexpect
from pexpect.popen_spawn import PopenSpawn

child = pexpect.popen_spawn.PopenSpawn('C:/Windows/System32/telnet 192.168.1.1')
child.expect('Username:')
child.sendline('cisco')
child.expect('Password:')
child.sendline('cisco')
child.expect('>')
child.close()           

Error:

Traceback (most recent call last):
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\expect.py", line 98, in expect_loop
    incoming = spawn.read_nonblocking(spawn.maxread, timeout)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\popen_spawn.py", line 68, in read_nonblocking
    raise EOF('End Of File (EOF).')
pexpect.exceptions.EOF: End Of File (EOF).

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python\windows scripts\telnet\telnet.py", line 37, in <module>
    main()
  File "C:\Python\windows scripts\telnet\telnet.py", line 20, in main
    child.expect('Username:')
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\spawnbase.py", line 327, in expect
    timeout, searchwindowsize, async_)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\spawnbase.py", line 355, in expect_list
    return exp.expect_loop(timeout)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\expect.py", line 104, in expect_loop
    return self.eof(e)
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\expect.py", line 50, in eof
    raise EOF(msg)
pexpect.exceptions.EOF: End Of File (EOF).
<pexpect.popen_spawn.PopenSpawn object at 0x000000000328C550>
searcher: searcher_re:
    0: re.compile("b'Username:'")
eten
  • 49
  • 1
  • 2
  • Best guess is that even Windows Telnet does not consider PopenSpawn to be a terminal. Since it is not a terminal, it is emitting EOL to pexpect. You may need to emulate a terminal using some of the win32 tools then wrap pexpect around it, like at: https://gist.github.com/anthonyeden/8488763 – cowbert Nov 30 '17 at 22:11
  • This looks really complicated and I'm kind of new to Python. Outside of pexpect, what would you recommended to telnet and ssh into networking devices on Windows environment? I tried paramiko in linux and didn't really like it. – eten Nov 30 '17 at 22:42
  • the "official" version of [wexpect](https://pypi.python.org/pypi/winpexpect) maybe? – cowbert Nov 30 '17 at 23:06
  • This answer says, spawn `cmd` before calling a program: https://stackoverflow.com/questions/38976893/windows-alternative-to-pexpect – cowbert Nov 30 '17 at 23:08

1 Answers1

0

The solution is to use plink.exe, which is a part of putty installation. You can download it from https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

Enable TelnetClient on windows system and put plink.exe in the same folder from where you are running pexpect. Then you can telnet using pexpect as mentioned in the below example.

Also, use timeout flag with PopenSpawn to wait for the connection to establish. The above error is due to a timeout flag is not set.

p = pexpect.popen_spawn.PopenSpawn('plink.exe -telnet 192.168.0.1 -P 23', timeout=1)