top of page

import psutil
import socket

def display_network_info():
    # Get network interface addresses
    interfaces = psutil.net_if_addrs()
    
    # Loop through each network interface
    for iface_name, iface_addresses in interfaces.items():
        mac_address = None
        ipv4_address = None
        
        # Extract MAC and IPv4 addresses
        for address in iface_addresses:
            if address.family == socket.AF_LINK:  # MAC address
                mac_address = address.address
            elif address.family == socket.AF_INET:  # IPv4 address
                ipv4_address = address.address
        
        # Print details if MAC or IPv4 address is found
        if mac_address or ipv4_address:
            print(f"Interface: {iface_name}")
            print(f"  MAC Address: {mac_address or 'N/A'}")
            print(f"  IPv4 Address: {ipv4_address or 'N/A'}")
            print()

if __name__ == "__main__":
    display_network_info()
 

PSUTIL
System Wide Methods

  • CPU:

    • psutil.cpu_count(logical=True) - Number of logical CPUs.

    • psutil.cpu_percent(interval=1, percpu=True) - CPU utilization as a percentage.

    • psutil.cpu_times(percpu=True) - CPU times spent in user/system/idle mode.

    • psutil.cpu_stats() - CPU statistics like context switches, interrupts, etc.

    • psutil.cpu_freq() - CPU frequency.

  • Memory:

    • psutil.virtual_memory() - System virtual memory statistics.

    • psutil.swap_memory() - Swap memory statistics.

  • Disks:

    • psutil.disk_partitions(all=False) - Disk partitions.

    • psutil.disk_usage(path) - Disk usage statistics for a given path.

    • psutil.disk_io_counters(perdisk=False) - Disk I/O statistics.

  • Network:

    • psutil.net_io_counters(pernic=True) - Network I/O statistics per interface.

    • psutil.net_connections(kind='inet') - List of network connections.

    • psutil.net_if_addrs() - Network interface addresses.

    • psutil.net_if_stats() - Network interface stats.

  • System Boot:

    • psutil.boot_time() - System boot time as a timestamp.

Example net_if_addrs()
bottom of page