简体   繁体   中英

How to filter mac address

can someone help me, i am beginner in programming, my problem is in output i would just like only to filter the mac address. how can i do it.

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)

output:

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

You can use a regex here. Regex allows you to match a certain text structure. As an example:

A regex like 'hello\\d{1,3}' could match strings like 'hello12' or 'hello432'.

You can match literal chars as well as placeholders like \\d for any digit value, including recurrences. {1,3} = between one and three recurrences. In this case, you want alphanumerical values, which can be matched with \\w in python.

^ = following char must be at the beginning of the line.

$ = foregoing char must be at the end of the line.

. = Every char is matched

+ = Match previous char/group at least one or more times. This is a greedy operator, so check your regex when playing around.

Since you want only one part of the matched line, you can use a capture group. Capture groups make substrings in your match accessible separately. Just wrap a parenthesis () around the wished part.

Python has a regex module for this, so here's some sample code:

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)

You can also you ttp to parse you data. Please see following config to get mac_addresses from your output.

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

Please kindly see the output after the above code runs:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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