【问题标题】:How to filter mac address如何过滤mac地址
【发布时间】:2020-03-27 23:21:03
【问题描述】:

谁能帮助我,我是编程初学者,我的问题是输出我只想过滤mac地址。我该怎么做。

from netmiko import ConnectHandler

cisco = {
        'device_type' : 'cisco_ios',
        'host' : '192.168.X.Y',
        'username' : 'foo',
        'password' : '123',
}

net_connect = ConnectHandler(**cisco)
net_connect.find_prompt()
show_mac = net_connect.send_command("show mac address-table vlan 100")
print(show_mac)

输出:

Vlan    Mac Address       Type        Ports
----    -----------       --------    -----
 100    264b.edf4.eba2    DYNAMIC     Gi0/3
 100    2680.f1ee.c0b1    DYNAMIC     Gi0/2
 100    3a60.2954.1ee2    DYNAMIC     Gi1/3
 100    4a60.05bd.27fc    DYNAMIC     Gi1/2
 100    7e02.eee8.0291    DYNAMIC     Gi0/1
 100    b689.d44e.afd1    DYNAMIC     Gi1/0
 100    d207.6258.5966    DYNAMIC     Gi1/1
Total Mac Addresses for this criterion: 7

【问题讨论】:

  • 过滤器是什么意思?你的意思是只打印mac地址与指定列表匹配的行吗?
  • 你能在打印时显示输出吗(type(show_mac))
  • 您可以检查 send_command 中的 'expect_string' 和 'changeto' 参数以获得帮助,也可以使用 help(net_connect.send_command)

标签: python cisco-ios netmiko


【解决方案1】:

你也可以通过 ttp 来解析你的数据。请参阅以下配置以从您的输出中获取 mac_addresses。

from ttp import ttp
import json

from netmiko import ConnectHandler

cisco = {
        'device_type' : 'cisco_ios',
        'host' : '192.168.X.Y',
        'username' : 'foo',
        'password' : '123',
}

net_connect = ConnectHandler(**cisco)
net_connect.find_prompt()
data_to_parse = net_connect.send_command("show mac address-table vlan 100")

ttp_template = '''
 {{vlan_id}}    {{mac_address}}    {{type}}     {{ports}}
'''

def mac_address_parser(data_to_parse): # "  Svc: 307006600 65035   483570    0 0148d14h  2/2/8 (IPv4)"'da parslıyor ekstradan. 
    
    parser = ttp(data=data_to_parse, template=ttp_template)
    parser.parse()

    # print result in JSON format
    results = parser.result(format='json')[0]
    #print(results)

    #converting str to json. 
    result = json.loads(results)

    return(result)

parsed_mac_address_parser = mac_address_parser(data_to_parse)

for mac_address in parsed_mac_address_parser[0]:
    print(mac_address['mac_address'])

请看上述代码运行后的输出:

【讨论】:

    【解决方案2】:

    您可以在此处使用regex。正则表达式允许您匹配某个文本结构。 举个例子:

    像“hello\d{1,3}”这样的正则表达式可以匹配像“hello12”或“hello432”这样的字符串。

    您可以匹配文字字符以及像 \d 这样的占位符来匹配任何数字值,包括重复次数。 {1,3} = 1 到 3 次重复。 在这种情况下,您需要字母数字值,可以与 python 中的 \w 匹配。

    ^ = 后面的字符必须在行首。

    $ = 前面的字符必须在行尾。

    . = 每个字符都匹配

    + = 匹配之前的字符/组至少一次或多次。这是一个贪婪的运算符,所以check your regex 在玩的时候。

    由于您只需要匹配行的一部分,因此可以使用捕获组。 捕获组使匹配中的子字符串可以单独访问。只需在希望的部分周围加上括号 ()。

    Python 有一个正则表达式模块,所以这里有一些示例代码:

    import re
    
    def your_func():
        mac_addresses = []
        for line in show_mac.split('\n'):  # split the above string in lines
            line = line.strip()  # remove leading and trailing spaces
            pattern = re.compile(r'^\d+\s+(\w{1,4}\.\w{1,4}\.\w{1,4})\s+\w+\s+.+$')
            # match a regex to a string from the beginning
            match = re.match(pattern, line)
            # python match object contains our capture group. first group is at index 1, index 0 is the whole match
            if match and match[1]:
                mac_addresses.append(match[1])
    
        print(mac_addresses)
    
    

    【讨论】:

      猜你喜欢
      • 2012-10-16
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2022-06-18
      • 2021-12-18
      • 1970-01-01
      相关资源
      最近更新 更多