1

I have a Centos 7 laptop connected to my device on the LAN port. Trying to login to my device and execute some commands for my automation tasks. I have python 3.6.4 installed on Centos 7. Below is the .py script I have on the laptop..

#!/usr/local/lib python3.6
import pexpect
import sys
import re

def dologin(child):
    # Enter User Name
    child.expect ('login:')
    child.sendline ('admin')

    # Enter Password
    child.expect ('Password:')
    child.sendline ('123')
    return

def doprepcommands(child):
    # Enter config prompt
    child.expect ('NOS/27179080475072>')
    child.sendline ('config')

    # Issue command
    child.expect ('NOS/27179080475072/DEBUG/Config>')
    child.sendline ('show vlan')
    child.expect ('NOS/27179080475072/DEBUG/Config>')
    child.sendline ('exit')
    child.expect ('NOS/27179080475072/DEBUG>')
    child.sendline ('exit')
    child.expect ('NOS/27179080475072')
    child.sendline ('exit')
    return

# Spawn the telnet session
child = pexpect.spawn ('telnet 192.168.1.254')

 # Display progress on screen
child.logfile = sys.stdout
dologin(child)
doprepcommands(child)

I am observing the below mentioned error on my IDLE screen when I run the script.Going by the details found in different blogs, I partially understand that 'sys.stdout' is wrongly used in my script(needs to be used differently for python 2.7 and python 3.6). I am also trying to find out if the functions 'dologin(child)', 'doprepcommands(child)' I have called uses the right syntax for Python 3.

I am new to Python. Can someone please help me with this error?

================= RESTART: /home/aricent/Siva/test_debug.py        =================
Traceback (most recent call last):
 File "/home/Siva/test_debug.py", line 37, in <module>
 dologin(child)
 File "/home/Siva/test_debug.py", line 8, in dologin
   child.expect ('login:')
 File "/usr/local/lib/python3.6/site-packages/pexpect/spawnbase.py",           line 341, in expect
 timeout, searchwindowsize, async_)
 File "/usr/local/lib/python3.6/site-packages/pexpect/spawnbase.py",     line 369, in expect_list
 return exp.expect_loop(timeout)
 File "/usr/local/lib/python3.6/site-packages/pexpect/expect.py",    line 111, in expect_loop
 incoming = spawn.read_nonblocking(spawn.maxread, timeout)
 File "/usr/local/lib/python3.6/site-packages/pexpect/pty_spawn.py", line 485, in read_nonblocking
 return super(spawn, self).read_nonblocking(size)
 File "/usr/local/lib/python3.6/site-packages/pexpect/spawnbase.py", line 179, in read_nonblocking
 self._log(s, 'read')
 File "/usr/local/lib/python3.6/site-packages/pexpect/spawnbase.py", line 126, in _log
 self.logfile.write(s)
 TypeError: must be str, not bytes 

1 Answers1

0

I got this fixed by going through some available discussion in different blogs. I have replaced 'spawn' with 'spawnu' and now I am able to telnet to my device and perform the operations as needed. child = pexpect.spawn ('telnet 192.168.1.254') child.logfile = sys.stdout

child = pexpect.spawnu ('telnet 192.168.1.254', logfile=sys.stdout, timeout = None)