Python Tools


Description/Use

The purpose of the tools is to collect interface, container, bridge, and network configuration data; Combine them together using IP addresses and MAC addresses, and display what is connected to what.


Commands used to generate the initial (raw) data

a. Capture the ifconfig output.

ifconfig > ifconfig.txt

Use the tool "ifconfig_convert_to_csv.py" to convert it to CSV data

b. Capture the full configuration of a container, including the MAC address assigned to its network interface, which is typically specified using lxc.network.hwaddr.

lxc config show <container_name> --expanded > containers.txt lxc config show <container_name> --expanded >> containers.txt

or

Capture just the stuff you want.

lxc list -f csv -c n46,volatile.eth0.hwaddr > containers.csv

or

Also capture parent information.

lxc list -f csv -c n46,volatile.eth0.hwaddr,devices:eth0.parent:ETHP > containers.csv

documentation.ubuntu.com/lxd/latest/reference/manpages/lxc/list/


tools

ifconfig_convert_to_csv.py

#!/usr/bin/python3 # ================================================================= # process ifconfig data and output to CSV data # ----------------------------------------------------------------- # Note: eventually this code will be modified to output # the CSV data to a file for later use. # ----------------------------------------------------------------- # command-line used: ifconfig > ifconfig.txt # ================================================================= import re import os # ----------------------------------------------------------------- # ---- process ifconfig output # ----------------------------------------------------------------- def process_ifconfig_file(infile:str,outfile:str=None) -> bool: ''' Process ifconfig data and output CSV data. ''' lcount = 0 # input line count csv_data = [] # accumulated CSV data #--- input file exists and readable? if not os.path.exists(infile): print() print(f'OOPS! {infile} does not exist') return False if not os.access(infile,os.R_OK): print() print(f'OOPS! {infile} is not readable') return False # ---- process ifconfig input "text" file (one line at a time) with open(infile,'r') as f: interface = None # no interface found for line in f: lcount += 1 # increment line read count line = line.strip() ##print(f'[{lcount:02}] {line}') # ---- blank line? if not line: # start a new interface? interface = None continue # ---- split line on spaces # ---- are there at least two elements? lst = line.split() if len(lst) < 2: print() print(f'bad line ({lcount})') print(f'line: "{line}"') return False # ---- is this line the start of an interface? if re.search('^.*:',lst[0],re.IGNORECASE): interface = lst[0].strip() interface = interface[:len(interface)-1] # ---- from here on there should be an interface if interface is None: print() print(f'OOPS! read {lcount} lines from input file') print(f' no interface found') return False # ---- just in case! lst[0] = lst[0].strip() # IP version lst[1] = lst[1].strip() # IP address # ---- IPv4 address? if re.search('^inet',lst[0],re.IGNORECASE): if len(lst[1]) < 1: print(f'{interface} {lst[0]} has no IPv4 address') return False csv_data.append(f'{interface},{lst[0]},{lst[1]}') continue # ---- IPv6 address? if re.search('^inet6',lst[0],re.IGNORECASE): if len(lst[1]) < 1: print(f'{interface} {lst[0]} has no IPv6 address') return False csv_data.append(f'{interface},{lst[0]},{lst[1]}') continue # ---- ether (MAC) address? if re.search('^ether',lst[0],re.IGNORECASE): if len(lst[1]) < 1: print(f'{interface} {lst[0]} has no ether address') return False csv_data.append(f'{interface},{lst[0]},{lst[1]}') continue # ---- display line count read from input file print() print(f'{lcount} lines read from file {infile}') # ---- output CSV data print() print('---- CSV data ----------------------------------------') for csv in csv_data: print(csv) return True # ----------------------------------------------------------------- # ---- main # ----------------------------------------------------------------- if __name__ == '__main__': print() cwd = os.getcwd() print(f'current working directory: {cwd}') if not process_ifconfig_file('ifconfig.txt','ifconfig.csv'): print() print('ifconfig data conversion to csv failed') print()