python - 预计 setecho 不工作

标签 python cisco pexpect

我正在尝试远程登录到 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)

输出是:

# 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#

最佳答案

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

Pexpect 有 3 种日志记录方法(1. logfile_send(),2. logfile_read() 3. logfile())。

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

1.) p.logfile() 将记录网络设备的回显输出,并将记录使用 send()sendline 发送的文本()。这是原始发布者不希望发生的事情。

在脚本中:

p.logfile = open('Log.log', 'w+')

输出:

# 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#

2.) p.logfile_read() 将只记录网络设备的回显输出。它不会记录 p.sendline() 字符。这是原始发帖人想要的结果。

在脚本中:

p.logfile_read = open('Log.log', 'w+')

输出:

# 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#

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

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

关于 setecho(False),根据预期 docs ,这会设置“打开或关闭终端回显模式”。该函数并不像人们(包括我自己)所希望的那样抑制 sendline() 输出。由于我正在处理网络设备(cisco、juniper、mrv 等),因此尝试关闭 tty echo 没有用。

希望这对将来的人有所帮助。

关于python - 预计 setecho 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13464759/

相关文章:

python - 使用 pexpect 和 pxssh 时的 EOF

python - celery 即服务 - PermissionError : [Errno 13] Permission denied: '/var/run/celery'

python - 使用 scikit-learn 的一次性编码

python - 如何在django模板中查找字典值

linux - freeradius根据AD组确定cisco shell权限

Python pexpect 脚本运行无误,但输出文件中没有输出

python - 使用 dtype 读取文件处理 pandas 中的缺失值

linux - 如何打包 Go 程序使其自给自足?

java - 如何向 Cisco Contact Center Express Identity Service 进行身份验证?

python ,预计: sendline() adding unexpected whitespace in sending text