【问题标题】:pexpect setecho not workingpexpect setecho 不工作
【发布时间】:2012-11-08 00:31:19
【问题描述】:

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

【问题讨论】:

    标签: python cisco pexpect


    【解决方案1】:

    我在与网络设备(不是 *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 并没有用。

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

    【讨论】:

    • 看来你明白了。你能举个例子吗?您是否明确致电logfile_read()?还是您只是将函数设置为某个东西,pexpect 会自己调用该方法?
    【解决方案2】:

    我曾经遇到过同样的回声问题,即使关闭了回声也是如此。

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

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

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

    【讨论】:

      【解决方案3】:

      所以,我发现的是……

      • setecho 似乎不起作用(在 pexpect.before 输出中,存在 pexpect.sendline 文本)。唯一的解决方案是进行字符串剥离。类似于伪代码pexpect.before.strip(pexpect.sendline)
      • 此外,在记录时,logfile_read 不包含回显,但日志文件包含(与 setecho 值无关)

      【讨论】:

        猜你喜欢
        • 2018-10-01
        • 2012-05-09
        • 1970-01-01
        • 2013-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-23
        • 2021-08-06
        相关资源
        最近更新 更多