When writing network automation scripts in Linux, one common task is identifying the default gateway for connectivity checks. The traditional netstat -rn
approach often requires parsing multiple lines of routing table output.
Let's examine a typical routing table output:
$ netstat -rn
Destination Gateway Genmask Flags MSS Window irtt Iface
10.9.9.17 0.0.0.0 255.255.255.255 UH 0 0 0 tun0
133.88.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 133.88.31.70 0.0.0.0 UG 0 0 0 eth0
The default gateway is marked by destination 0.0.0.0
and the G
flag in the Flags column (UG here).
Here are three robust approaches to extract only the default gateway:
# Method 1: Using ip route
$ ip route show default | awk '/default/ {print $3}'
133.88.31.70
# Method 2: Advanced netstat parsing
$ netstat -rn | awk '$1=="0.0.0.0" && $2!="0.0.0.0" {print $2}'
133.88.31.70
# Method 3: Using route command
$ route -n | awk '$1=="0.0.0.0" {print $2}'
133.88.31.70
Here's a complete bash script to check network connectivity:
#!/bin/bash
# Get default gateway
GATEWAY=$(ip route show default | awk '/default/ {print $3}')
if [ -z "$GATEWAY" ]; then
echo "Error: No default gateway found"
exit 1
fi
# Ping test
if ping -c 1 $GATEWAY &> /dev/null; then
echo "Network UP - Gateway $GATEWAY reachable"
else
echo "Network DOWN - Gateway $GATEWAY unreachable"
fi
For systems with multiple interfaces, you might want to specify which interface's gateway to check:
# Get gateway for specific interface
$ ip route show default dev eth0 | awk '{print $3}'
133.88.31.70
The ip route
method is generally preferred in modern systems as it's more reliable and produces cleaner output for parsing. The script example shows how to integrate this into automated network monitoring solutions.
When checking Linux network connectivity in scripts, the default gateway is critical. The traditional netstat -rn
approach with grep 0.0.0.0
returns multiple entries including:
- Host routes (UH flag)
- Network routes (U flag)
- Actual default gateway (UG flag)
Here are three reliable methods to isolate the default gateway:
# Method 1: Using awk to filter UG flag
ip route show default | awk '/default/ {print $3}'
# Method 2: Advanced grep pattern
netstat -rn | grep '^0.0.0.0' | grep 'UG' | awk '{print $2}'
# Method 3: Modern iproute2 alternative
ip route | awk '/default/ {print $3}'
For robust network monitoring scripts:
#!/bin/bash
get_default_gateway() {
local gw=$(ip route show default 2>/dev/null | awk '/default/ {print $3}')
if [[ -z "$gw" ]]; then
echo "No default gateway detected" >&2
return 1
fi
echo "$gw"
}
ping_gateway() {
local gateway=$(get_default_gateway) || exit 1
if ping -c 1 -W 2 "$gateway" &>/dev/null; then
echo "Gateway $gateway is reachable"
return 0
else
echo "Gateway $gateway is unreachable" >&2
return 1
fi
}
# Example usage
if ping_gateway; then
echo "Network connectivity confirmed"
else
echo "Network issues detected"
fi
Consider these scenarios in production scripts:
- Multiple default routes (ECMP)
- Tunnel interfaces (like OpenVPN)
- IPv6 compatibility
For complex environments:
# Get primary IPv4 gateway
ip -4 route show default | head -n1 | awk '{print $3}'
# Get primary IPv6 gateway
ip -6 route show default | head -n1 | awk '{print $3}'
For systems without iproute2:
# Using route command (legacy systems)
route -n | awk '$1 == "0.0.0.0" && $2 != "0.0.0.0" {print $2}'
# Using nmcli (NetworkManager systems)
nmcli -g ip4.gateway connection show "$(nmcli -g name,device con show --active | grep -v ':')"