pexpect setecho不工作

时间:2012-11-20 00:20:00

标签: 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)

输出是:

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

3 个答案:

答案 0 :(得分:9)

在与网络设备(不是* 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()仅记录网络设备的echo'd输出。它不会记录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),根据pexpect docs,这会设置“终端回显模式开启或关闭”。这个功能并不意味着压迫人们希望(包括我自己)的sendline()输出。由于我正在处理网络设备(cisco,juniper,mrv等),试图关闭tty echo是没有用的。

希望将来可以帮助某人。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

我曾经遇到过与回声相同的问题,即使关闭了回声。

但我可能找到了解决方案:

commandToRun = 'bash -c "less /readfile | tail -4"'
yourConnection.sendLine("stty -echo")
commandResult = yourConnection.sendLine(commandToRun)
self.sendLine("stty echo")

所以基本上,使用“bash -c”在shell中运行命令,然后在bash中转换echo