How to Extract IP Addresses from ifconfig Output Using grep: A Practical Guide for Linux Networking


4 views

When working with Linux networking, the ifconfig command provides detailed information about network interfaces. The output typically follows this pattern for each interface:

interface_name Link encap:Ethernet
          inet addr:XXX.XXX.XXX.XXX  Bcast:XXX.XXX.XXX.XXX  Mask:XXX.XXX.XXX.XXX
          [other network statistics]

To extract just the IP addresses, we can use grep with a regular expression pattern:

ifconfig | grep -Eo 'inet addr:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'

This will output lines containing the IP addresses with the "inet addr:" prefix.

To get cleaner output showing just the IP addresses without the interface names or "inet addr:" prefix:

ifconfig | grep -Eo 'inet addr:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | cut -d: -f2

Often you'll want to exclude the loopback address (127.0.0.1):

ifconfig | grep -Eo 'inet addr:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -v '127.0.0.1' | cut -d: -f2

For a more complete solution showing both interface names and their corresponding IPs:

ifconfig | awk '/^[a-zA-Z0-9]/ {interface=$1} /inet addr:/ {split($2, a, ":"); print interface, a[2]}'

On newer Linux systems, ip is replacing ifconfig. Here's how to extract IPs with it:

ip addr | grep -oP 'inet \K[\d.]+'

For frequent use, create a bash script:

#!/bin/bash
# Script: get_ips.sh
# Description: Extract all non-loopback IP addresses

ifconfig | awk '/^[a-zA-Z0-9]/ {interface=$1} 
/inet addr:/ {split($2, a, ":"); 
if(a[2] != "127.0.0.1") print interface ":" a[2]}'

Some distributions format ifconfig output slightly differently. This more robust pattern handles most cases:

ifconfig | grep -Eo '(inet addr:|inet )([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{print $2}'

For IPv6 addresses, modify the pattern:

ifconfig | grep -Eo 'inet6 addr: ([a-fA-F0-9:]+)' | awk '{print $2}'

When working with network interfaces on Unix-like systems, the ifconfig command provides detailed information about each network interface. The key information we're interested in is the IPv4 address, which appears in lines containing inet addr: followed by the actual IP address.

The simplest way to extract IP addresses is by filtering for the "inet addr" pattern:

ifconfig | grep "inet addr"

To get cleaner output showing just the IP addresses without other information, we can chain additional commands:

ifconfig | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'

This pipeline:

  1. Finds all lines containing "inet addr"
  2. Uses awk to split at the colon (first awk)
  3. Prints just the IP portion (second awk)

Some newer systems might display IP information slightly differently. Here's a more robust version that handles various formats:

ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*'

To get the IP address for a specific interface (e.g., eth1):

ifconfig eth1 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'

On modern Linux systems, the ip command is preferred over ifconfig. Here's how to get IP addresses with it:

ip addr show | grep -Eo 'inet ([0-9]*\.){3}[0-9]*' | awk '{print $2}'

For frequent use, you might want to create a simple bash function:

get_ip() {
    ifconfig $1 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'
}
# Usage: get_ip eth0