How to get IP address for network interface in Python without any libraries

The following example code will get the IPv4 address for the local network interface eth0:

import subprocess
import re

def get_interface_ip(interface):
    """Get IP address for a given network interface"""
    command = f"ip addr show {interface}"
    output = subprocess.check_output(command, shell=True).decode()

    ip_pattern = r'inet\s+(\d+\.\d+\.\d+\.\d+)'
    match =, output)
    if match:
        ip_address =
        return ip_address
        return None

interface_name = "eth0"
ip_address = get_interface_ip(interface_name)

if ip_address:
    print(f"IP address of {interface_name}: {ip_address}")
    print(f"No IP address found for {interface_name}")

Example output:

IP address of eth0: