【发布时间】:2017-05-29 16:24:42
【问题描述】:
我正在学习 Python,如果我的问题很幼稚,敬请原谅。
我在我的脚本中使用了pexpectinteract(),它正在使用python2.7。但是当我使用 python3 或 python3.5 时,出现以下错误:
Traceback (most recent call last):
File "ap_access.py", line 73, in <module>
child.interact()
File "/usr/local/lib/python3.5/dist-packages/pexpect/pty_spawn.py", line 745, in interact
self.__interact_copy(escape_character, input_filter, output_filter)
File "/usr/local/lib/python3.5/dist-packages/pexpect/pty_spawn.py", line 784, in __interact_copy
self._log(data, 'read')
File "/usr/local/lib/python3.5/dist-packages/pexpect/spawnbase.py", line 121, in _log
self.logfile.write(s)
TypeError: write() argument must be str, not bytes
在 Google 上搜索了这个问题,但实际上没有与 interact() 相关,但没有得到太多信息。
# Script to log into the device and execute the required shell commands.
log_file = open('logfile.txt', 'w')
for ip in list_of_IP_addresses:
ip1=str(ip)
child = pexpect.spawnu('ssh '+username+'@'+ip1, log_file)
code = child.expect(['Are you sure you want to continue connecting (yes/no)?', 'Please login: ', 'password :', 'No route to host', pexpect.EOF, pexpect.TIMEOUT])
if code==0:
child.sendline('yes')
code = child.expect(['Are you sure you want to continue connecting (yes/no)?', 'Please login: ', 'password :', 'No route to host', pexpect.EOF, pexpect.TIMEOUT])
if code==1:
print("Entering the Username")
child.sendline(username)
code = child.expect(['Are you sure you want to continue connecting (yes/no)?', 'Please login: ', 'password :', 'No route to host', pexpect.EOF, pexpect.TIMEOUT])
if code==2:
print("Entering the credentials")
child.sendline(password)
if code==3:
print ("Please check for reachability for ip", ip1)
sys.exit()
if code==4:
print ("Looks like there is an error")
sys.exit(0)
if code==5:
print ("Timeout in accessing or logging into ", ip1)
sys.exit(0)
child.interact()
如果能获得有关如何让 pexpect interact() 与 python3.x 一起工作的帮助,我将不胜感激
谢谢。
更新我面临的新问题是,在我正在捕获输出的日志文件中看不到命令的输出,例如 ps、具有较大输出的文件的 cat。我尝试使用“setwinsize”设置 winsize,将 maxreadsize 设置为更大的值,但没有任何改变。
def log_separation():
sep_file = 'Device_LOG_{}.log'.format(date.today())
child.logfile = open(sep_file, "a")
with open(sep_file, 'a+') as log_sep:
log_sep.write('\n'+'\n')
def mem_cpu():
mem_file = 'Device_LOG_{}.log'.format(date.today())
with open(mem_file, 'a+') as cpu:
cpu.write(str(datetime.datetime.now())+'\n') cpu.write('########################################################'+'\n')
child.sendline('top -n 1')
child.expect('#')
log_separation()
child.sendline('cat /proc/meminfo')
log_separation()
child.expect('#')
child.sendline('cat /proc/slabinfo')
log_separation()
child.expect('#')
child.sendline('cat /proc/tasklets')
log_separation
child.expect('#')
child.sendline('free')
log_separation()
child.expect('#')
child.sendline('ps axww')
log_separation()
child.expect('#')
child.sendline('exit')
# Gets the set of all IP addresses in the given range
list_of_IP_addresses = get_IP_address(ip_addr_lower, ip_addr_upper)
#Opening a file to write the error logs to
log_file = open('logfile.txt', 'w')
with open('Device_Access_log.log', 'w+') as f:
# Script to log into the devices and excute the required shell commands.
for ip in list_of_IP_addresses:
ip1=str(ip)
child = pexpect.spawn('ssh '+username+'@'+ip1, logfile=log_file)
child.logfile_read = sys.stdout
code = child.expect(['Are you sure you want to continue connecting (yes/no)?', 'Please login: ', 'password :', 'No route to host', pexpect.EOF, pexpect.TIMEOUT])
if code==0:
child.sendline('yes')
code = child.expect(['Are you sure you want to continue connecting (yes/no)?', 'Please login: ', 'password :', 'No route to host', pexpect.EOF, pexpect.TIMEOUT])
if code==1:
print('Entering the Username')
child.sendline(username)
code = child.expect(['Are you sure you want to continue connecting (yes/no)?', 'Please login: ', 'password :', 'No route to host', pexpect.EOF, pexpect.TIMEOUT])
if code==2:
print('Entering the credentials')
child.sendline(password)
mem_cpu()
stats_file = 'Device_LOG_{}.log'.format(date.today())
with open(stats_file, 'a+') as eol:
eol.write('############################### End of log for Device '+ip1+' ###############################################'+'\n')
eol.write('########################################################################################################'+'\n')
if code==3:
print ('Ip address error', ip1)
f.write('No route to host '+ip1+'\n')
if code==4:
print ('Error connecting to ', ip1)
f.write('Error connecting to '+ip1+'\n')
if code==5:
print ('Timeout in accessing or logging into ', ip1)
f.write('Timeout in accessing or logging into '+ip1+'\n')
我可以看到输出直到“空闲”,在日志文件中看不到 ps 命令的输出。
【问题讨论】:
-
您能否给出一些最小的代码示例,您在哪里使用所述功能,以便我们可以尝试重现您的错误消息?
-
亲爱的 Thomas,我已经添加了代码。我正在尝试获取一系列 IP 地址以一次登录一个并执行一些命令然后退出。
-
发帖者似乎在同时提出两个问题。第一个,
interact的问题似乎是一个开放的错误。见github.com/pexpect/pexpect/issues/506。看起来 Pexpect 不是一个非常活跃的项目,因此尚不清楚何时会修复此问题。问题本身来自 2018 年。
标签: python python-2.7 python-3.x pexpect