为什么Pexpect代码没有获取我的需求数据?

时间:2019-06-25 15:10:17

标签: python pexpect

在路由器中,我使用以下命令过滤ae16 unit 200

jd@HK-MX-RE0# run show configuration | display set | match "ae16 unit 200"                      
set interfaces ae16 unit 200 vlan-id 200                                        
set interfaces ae16 unit 200 family inet address 13.57.111.246/29              
set interfaces ae16 unit 200 family inet address 13.215.8.246/29               

{master}[edit]                                                                  
jd@HK-MX-RE0#  

我想使用pexpect来打印

set interfaces ae16 unit 200 vlan-id 200                                        
set interfaces ae16 unit 200 family inet address 13.57.111.246/29              
set interfaces ae16 unit 200 family inet address 13.215.8.246/29               

这是我期望的代码:

        ...
        child = pexpect.spawn(cmd1)  # create the pexpect child 
        ...

        cmd2 = 'run show configuration | display set | match "ae16 unit 200"

        child.sendline(cmd1)
        child.expect('#')

        child.sendline(cmd2)


        expect_str = """jd@HK-MX-RE0# """

        child.expect(expect_str)
        exec_message += (cmd2 + '\n')

        exec_message += '\n'
        exec_message += 'query vlan as bellow:\n'
        exec_message += child.before.decode()
        print(exec_message)

仅打印:

query vlan as bellow:


{master}[edit]

因此,没有得到内容。

我也尝试过:

expect_str = """\n\n{master}[edit]\njd@HK-MX-RE0# """
expect_str = """\r\n\r\n{master}[edit]\r\njd@HK-MX-RE0# """
expect_str = """\r\n{master}[edit]\r\njd@HK-MX-RE0# """

所有人都将收到超时异常:

"Timeout exceeded.\n
<pexpect.pty_spawn.spawn object at 0x11120b128>\n
command: /usr/bin/ssh\nargs: ['/usr/bin/ssh', 'root@13.193.240.45']\n
buffer (last 100 chars): b'address 13.215.8.246/29\\r\\n\\r\\n
{master}[edit]\\r\\n
jd@HK-MX-RE0# \\r\\n\\r\\n
{master}[edit]\\r\\n
jd@HK-MX-RE0# '\n
before (last 100 chars): b'address 13.215.8.246/29\\r\\n\\r\\n
{master}[edit]\\r\\n
jd@HK-MX-RE0# \\r\\n\\r\\n
{master}[edit]\\r\\n
jd@HK-MX-RE0# '\n
after: <class 'pexpect.exceptions.TIMEOUT'>\n
match: None\n
match_index: None\n
exitstatus: None\n
flag_eof: False\n
pid: 8560\nchild_fd: 13\n
closed: False\ntimeout: 30\n
delimiter: <class 'pexpect.exceptions.EOF'>\n
logfile: None\n
logfile_read: None\n
logfile_send: None\n
maxread: 2000\n
ignorecase: False\n
searchwindowsize: None\n
delaybeforesend: 0.05\n
delayafterclose: 0.1\n
delayafterterminate: 0.1\n
searcher: searcher_re:\n    
0: re.compile(b'\\n
{master}[edit]\\n
jd@HK-MX-RE0#')"

0 个答案:

没有答案