2017-09-18 108 views
-1

我有一个Cisco路由器,我需要知道哪个接口用于LAN。这是显示接口描述输出:过滤pexpect输出

R1#sho int desc 
Interface      Status   Protocol Description 
Em0/0       admin down  down 
Gi0/0       up    up  LAN 
Gi0/1       up    up  WAN 
Gi0/2       up    up  Crosslink 
Gi0/2.100      up    up  Crosslink 

我设法与Pexpect的登录并获得以上输出到一个变量,但我不知道如何对其进行过滤:

execute.send('term len 0\n') 
execute.expect(device['name'] + '#') 

execute.send('sho int desc\n') 
execute.expect(device['name'] + '#') 
output = execute.before 

我想结果是“Gi0/0”。

你能给我一些想法吗? 谢谢!

回答

0

我用下面的代码:

execute.send('sho int desc\n') 
    execute.expect(device['name'] + '#') 
    output = execute.before 

    for line in output.splitlines(): 
     if re.match('.*LAN.*', line): 
      interfaceName = re.findall(r'[^\s]+' ,line)[0] 

必须有一个更好的解决方案。如果您有任何想法,请分享。