简体   繁体   English

pexpect setecho不工作

[英]pexpect setecho not working

I am trying to telnet to Cisco Router and give commands using pexpect. 我试图telnet到Cisco路由器并使用pexpect发出命令。 Its working, but the sendline() repeats in the output. 它工作,但sendline()在输出中重复。 even after using setecho to False. 甚至在使用setecho后使用False。 Code is: 代码是:

'''
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)

Output is: 输出是:

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

I had the same problem when interacting with network devices (not *nix terminals). 在与网络设备(不是* nix终端)交互时遇到了同样的问题。

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

Using the output example from the original poster above, here is what the output looks like for each logging method: 使用上面原始海报中的输出示例,以下是每种日志记录方法的输出结果:

1.) p.logfile() will log the network device's echo'd output AND it will log the text sent using send() & sendline() . 1.) p.logfile()将记录网络设备的echo'd输出,它将使用send()sendline()记录发送的文本。 This is what the original poster did NOT want to happen. 这就是原始海报不想发生的事情。

In script: 在脚本中:

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

Output: 输出:

# 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() will ONLY log the network device's echo'd output. 2.) p.logfile_read()将仅记录网络设备的echo'd输出。 It will not log p.sendline() characters. 它不会记录p.sendline()字符。 This is the desired result the original poster was looking for. 这是原始海报寻找的理想结果。

In script: 在脚本中:

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

Output: 输出:

# 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 will only send the p.sendline() characters to the log, which probably isn't very useful in most cases. 3.) p.logfile_send只会将p.sendline()字符发送到日志,这在大多数情况下可能不是很有用。 I'll skip the example, since everyone probably has the idea by now. 我会跳过这个例子,因为现在每个人都有这个想法。

So, using logfile_read() will fix the issue with passwords showing in the log output when interacting with network devices. 因此,使用logfile_read()将解决与网络设备交互时在日志输出中显示的密码的问题。 This will also fix the issue of pexpect showing double echo's in log output, which I've seen a few people ask questions about online as well. 这也将解决pexpect在日志输出中显示双重回声的问题,我看到一些人也在网上提问。

Regarding setecho(False) , per the pexpect docs , this sets the "terminal echo mode on or off". 关于setecho(False) ,根据pexpect docs ,这将设置“终端回显模式开启或关闭”。 That function isn't meant to surpress sendline() output as people were hoping (including myself). 该功能并不意味着sendline()输出,因为人们希望(包括我自己)。 And since I was dealing with a network device (cisco, juniper, mrv, etc), trying to turn off tty echo wasn't useful. 由于我正在处理网络设备(cisco,juniper,mrv等),试图关闭tty echo是没有用的。

Hope this helps someone out in the future. 希望这有助于将来的人。

So, what I have found is... 所以,我发现的是......

  • setecho just doesn't seem to work (in the pexpect.before output, the pexpect.sendline text is present). setecho似乎不起作用(在pexpect.before输出中,pexpect.sendline文本存在)。 Only solution is to do string stripping. 唯一的解决方案是进行字符串剥离。 Something like pseudo code pexpect.before.strip(pexpect.sendline) 像伪代码pexpect.before.strip(pexpect.sendline)
  • Also, while logging, logfile_read doesn't contain the echoes, but logfile contains (irrespective of setecho value) 此外,在记录时,logfile_read不包含回声,但是logfile包含(不考虑setecho值)

I used to have the same problem with echos, even when echo was turned off. 我曾经遇到过与回声相同的问题,即使关闭了回声。

But I might have found a solution: 但我可能找到了解决方案:

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

So basically, run you command in a shell using ' bash -c ' and then turn of echo in the bash. 所以基本上,使用' bash -c '在shell中运行命令,然后在bash中转换echo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM