Extract Mac address from output

I m trying to extract ONLY the Mac address from the "sh mac address-table interface gi1/0/3" command and save it in a file but struggling to get the right result . I have come up with the following script in trying to seek and display the MAC address but not getting the expected outcome , it may over elaborated . Any help would be appreciated . Thanks in advance

from netmiko import ConnectHandler
from getpass import getpass
from netmiko.ssh_exception import NetMikoTimeoutException
from netmiko.ssh_exception import NetMikoAuthenticationException
from paramiko.ssh_exception import SSHException

print('\n****Please provide Login Details****')
username = input('\nEnter your username: ')
password = getpass('\nEnter password:')


with open('devices.txt') as f:
    devices = f.read().splitlines()

for item in devices[1:]:
    ip = item.split(',')[0]

    cisco_device = {
        'device_type': 'cisco_ios',
        'host': ip,
        'username': username,
        'password': password,
    }
    print(f'\n Connecting to {cisco_device["host"]}\n')

    connection = ConnectHandler(**cisco_device)

    try:
        net_connect = ConnectHandler(**cisco_device)
    except NetMikoTimeoutException:
        print('Device not reachable')
        continue

    except NetMikoAuthenticationException:
        print('Authentication Failure')
        continue

    except SSHException:
        print('Make sure SSH is enabled')
        continue

    command1 = [f'do sh mac address-table interface gi1/0/3']

    output1 = connection.send_config_set(command1)

    print(output1)

    with open('myfile.txt', 'a') as f:
        f.write(output1)
    f = open('myfile.txt', 'r')
    f.seek(305)
    print(f.read(17))


    print(f'\n Disconnecting from {cisco_device["host"]}\n')
    connection.disconnect()

    print('#' * 60)


  


Read more here: https://stackoverflow.com/questions/64959187/extract-mac-address-from-output

Content Attribution

This content was originally published by Peaceandlove at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: