pexpect setecho不工作

2024-05-15 14:43:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试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#

Tags: cmdshowusernamepromptcisconovhostnametelnet
3条回答

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

Pexpect有3种日志记录方法(1。logfile_send(),2。logfile_read()三。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显示double echo的日志输出的问题,我也看到一些人在网上问一些问题。

对于setecho(False),根据pexpect docs,这将设置“终端回音模式开或关”。这个函数并不像人们(包括我自己)希望的那样支持sendline()输出。由于我正在处理一个网络设备(cisco、juniper、mrv等),试图关闭tty echo也没用。

希望这能帮助将来的人。

我以前对回声也有同样的问题,即使回声被关掉了。

但我可能找到了解决办法:

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

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

所以,我发现。。。

  • setecho似乎不起作用(在输出之前的pexpect.before中,存在pexpect.sendline文本)。唯一的解决办法是剥离管柱。类似伪代码pexpect.before.strip(pexpect.sendline)
  • 此外,在日志记录时,logfile_read不包含回声,但logfile包含(与setecho值无关)

相关问题 更多 >