Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中解析输出并在循环中使用它_Python_Paramiko - Fatal编程技术网

如何在Python中解析输出并在循环中使用它

如何在Python中解析输出并在循环中使用它,python,paramiko,Python,Paramiko,我需要从以下UPD.result输出中提取“xe-1/2/0”: interface xe-1/2/0.0; interface ge-1/2/1.0; interface lo0.0 { (这是从列表中的一个框中“显示配置协议ospf | match int”命令输出的。) 我正在使用netmiko库连接到网络设备,并找到一些参与OSPF协议的接口,然后我想检查这些接口上的错误计数 import getpass import sys import time import textfsm i

我需要从以下UPD.result输出中提取“xe-1/2/0”:

interface xe-1/2/0.0;
interface ge-1/2/1.0;
interface lo0.0 {
(这是从列表中的一个框中“显示配置协议ospf | match int”命令输出的。)


我正在使用netmiko库连接到网络设备,并找到一些参与OSPF协议的接口,然后我想检查这些接口上的错误计数

import getpass
import sys
import time
import textfsm
import os
from tabulate import tabulate
import re


from netmiko import ConnectHandler

USER = raw_input("Login:")
PASSWORD = getpass.getpass()
FILENAME = raw_input("Name of file with results:")
GETPATH= os.getcwd()
GETPATH2 = (GETPATH+'/IP-ADD.txt')
GETPATH3 = (GETPATH+'/template-desc.template')

BOXES_IP = [line.strip() for line in open(GETPATH2, 'r')]
print(BOXES_IP)

for IP in BOXES_IP:
    print('CONNECTION TO DEVICE {}'.format(IP))
    try:
       DEVICE_PARAMS = {'device_type': 'juniper_junos',
                        'ip': IP,
                        'username':USER,
                        'password':PASSWORD,
                        'verbose': True}
        with ConnectHandler(**DEVICE_PARAMS) as sss:
            sss.enable()

            result = sss.send_command('show configuration protocols ospf| match int')
            hostname = sss.send_command('show configuration system host-name')
            print(result)
    except:
        print('CONNECTION TO DEVICE FAILS {}'.format(IP))
        continue

    f = open(FILENAME+'.txt', 'a')
    for row in hostname:
        f.write(row)
    for row in result:
        f.write(row)
    f.close()

    regex = re.compile(r'^(xe.[0-9].[0-9].[0-9])')
    results_list = [] 

    b = open(FILENAME+'.txt', 'r')
    print b
    for line in b:
        match = regex.search(line)
        if not match: continue
        results_list.append(match.group())
    print results_list
我需要解析命令
'show configuration protocols ospf | match int'
的输出,并找到
xe
(例如
xe-0/0/1
)接口,然后输入
“show interface xe-***extensive”
命令并在文件中打印结果

我该怎么做

我试图用compile解析输出文件,但它不起作用

更新

print(result)
输出:

interface lo0.0 {
interface xe-1/3/1.0;
interface ge-1/2/0.0;
interface xe-1/3/0.0;
拆分结果

    for l in result.split("\n"):
    l = l.split(" ")
    print l

[u'']
[u'', u'', u'', u'', u'interface', u'lo0.0', u'{']
[u'', u'', u'', u'', u'interface', u'xe-1/3/1.0;']
[u'', u'', u'', u'', u'interface', u'ge-1/2/0.0;']
[u'', u'', u'', u'', u'interface', u'xe-1/3/0.0;']
[u'']

总之,对于这个任务来说,regexp是一个杀伤力过大且容易出错的工具。 我会这样做:

result ="""interface xe-1/2/0.0;
interface ge-1/2/1.0;
interface lo0.0 {
"""

for l in result.split("\n"):
    l = l.split(" ")
    if l[0] == "interface": # cause grep on "int"
        iface = l[1]
        if iface[0:3] == "xe-":
            arg=iface.split(".")[0]
            # do something with arg
            print(arg)
产生:

xe-1/2/0

如果您想使用
re
,应该这样做:

import re

output = '''interface xe-1/2/0.0;
interface ge-1/2/1.0;
interface lo0.0 {'''

print(re.findall('xe-\d/\d/\d', output))
#['xe-1/2/0']
要获得所需的输出,您可以使用:

for found in re.findall('xe-\d/\d/\d', output):
    print('show interface {} extensive'.format(found))

也许你可以把输入和输出的例子包括进来?@zipa更新了main post我们不需要看到或理解你的paramiko代码。我们需要看到的只是输入和期望的输出。我重写了你的问题,把它提到了最上面。为什么我们应该排除接口“lo0.0”,而包括“xe-1/2/0.0”和“ge-1/2/1.0”?合法网络接口名称的模式是什么?@smci我们需要的所有接口都以“ge-/*/”或“xe-/*/”开头lo0'是一个逻辑接口,在我们使用夹板(“\n”)后不需要进行操作。列表如下:a=[u'',u'接口ge-1/2/0.0;',u'接口ge-1/2/1.0;',u'接口lo0.0{',u']@也许是Goroshek,但这并没有改变答案:空行将被忽略。这对我现在不起作用,但我理解这个想法。thanks@Goroshek,如果您提供您的输入,我们可能会发现问题检查更新部分获得此[u'xe-1/3/1',u'xe-1/3/0']我真的不知道u的符号在哪里coming@Setop问题的第一行指出OP只想要
xe
@Goroshek
u'some_text'
只表示文本是unicode,您不必担心(如果它以后在代码中不会产生错误)。