pexpect setecho不工作

aba*_*rik 6 python cisco pexpect

我试图telnet到Cisco路由器并使用pexpect发出命令.它工作,但sendline()在输出中重复.甚至在使用setecho后使用False.代码是:

'''
Created on Nov 19, 2012

@author: Amit Barik
'''

import pexpect

hostname = 'hostname'
login_cmd = 'telnet ' + hostname + '.net'
username = 'username'
password = 'pwd'
prompt = hostname + '#'

p = pexpect.spawn(login_cmd)
p.setecho(False)
p.logfile = open('Log.log', 'w+')

p.expect('Username:')
print '1',repr(p.before)

p.sendline(username)
p.expect('Password:')
print '2',repr(p.before)

p.sendline(password)
p.expect(prompt)
print '3',repr(p.before)

cmd = 'show clock'
p.sendline(cmd)
p.expect(prompt)
print 'Output for {0}'.format(cmd), repr(p.before)
Run Code Online (Sandbox Code Playgroud)

输出是:

# On Python Console
Output for show clock 'show clock\r\n00:16:40.692 UTC Tue Nov 20 2012\r\n'

# On Log File
Username: username
username
Password: pwd

My Cisco Banner

hostname#show clock
show clock
00:16:40.692 UTC Tue Nov 20 2012
hostname#
Run Code Online (Sandbox Code Playgroud)

小智 10

在与网络设备(不是*nix终端)交互时遇到了同样的问题.

Pexpect有3种记录方法(1. logfile_send(),2.3 logfile_read(). logfile()).

使用上面原始海报中的输出示例,以下是每种日志记录方法的输出结果:

1.)p.logfile()将记录网络设备的echo'd输出,它将记录使用send()&发送的文本sendline().这就是原始海报不想发生的事情.

在脚本中:

p.logfile = open('Log.log', 'w+')
Run Code Online (Sandbox Code Playgroud)

输出:

# On Python Console
Output for show clock 'show clock\r\n00:16:40.692 UTC Tue Nov 20 2012\r\n'

# On Log File
Username: username   #This is the `sendline()` output
username             #This is echo from the network device
Password: pwd        #This is `sendline()` output
                     #Notice, pwd only echo's once.  There is no echo from the network device since it doesn't echo passwords

My Cisco Banner

hostname#show clock  #This is the `sendline()` output
show clock           #This is echo from the network device
00:16:40.692 UTC Tue Nov 20 2012
hostname#
Run Code Online (Sandbox Code Playgroud)

2.)p.logfile_read()仅记录网络设备的echo'd输出.它不会记录p.sendline()字符.这是原始海报寻找的理想结果.

在脚本中:

p.logfile_read = open('Log.log', 'w+')
Run Code Online (Sandbox Code Playgroud)

输出:

# On Python Console
Output for show clock 'show clock\r\n00:16:40.692 UTC Tue Nov 20 2012\r\n'

# On Log File
Username: username   #This is echo from the network device
Password:            #There is no echo from the network device since it doesn't echo passwords

My Cisco Banner

hostname#show clock  #This is echo from the network device
00:16:40.692 UTC Tue Nov 20 2012
hostname#
Run Code Online (Sandbox Code Playgroud)

3.)p.logfile_send只会将p.sendline()字符发送到日志,这在大多数情况下可能不是很有用.我会跳过这个例子,因为现在每个人都有这个想法.

因此,使用logfile_read()将解决与网络设备交互时在日志输出中显示的密码的问题.这也将解决pexpect在日志输出中显示双重回声的问题,我看到一些人也在网上提问.

关于setecho(False),根据pexpect docs,这将设置"终端回显模式开启或关闭".这个功能并不意味着压抑sendline()输出,因为人们希望(包括我自己).由于我正在处理网络设备(cisco,juniper,mrv等),试图关闭tty echo是没有用的.

希望这有助于将来的人.


aba*_*rik 0

所以,我发现的是......

  • setecho 似乎不起作用(在 pexpect.before 输出中,存在 pexpect.sendline 文本)。唯一的解决方案是进行字符串剥离。类似伪代码的东西pexpect.before.strip(pexpect.sendline)
  • 另外,在记录时, logfile_read 不包含回声,但 logfile 包含(无论 setecho 值如何)