Skip to content

Instantly share code, notes, and snippets.

@coffnix
Created November 6, 2023 12:33
Show Gist options
  • Save coffnix/4a6e318033face7d1fd68795f817c246 to your computer and use it in GitHub Desktop.
Save coffnix/4a6e318033face7d1fd68795f817c246 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check if -r flag is set
if [[ "$1" == "-r" || "$2" == "-r" ]]; then
netstat -nr | sed -e '1,3d;/Internet6:/,$d;/^$/d'
exit 0
fi
# Get interface names
network_info=$(networksetup -listnetworkserviceorder | grep -E '\(Hardware Port: .+, Device: .+\)')
# Loop through all interfaces found by ifconfig
for iface in $(ifconfig -l); do
inet_lines=$(ifconfig $iface | grep -E "inet [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
# Check if the interface has an IP, if not, continue the loop
[ -z "$inet_lines" ] && ip_main="not configured" || ip_main=$(echo "$inet_lines" | awk "NR==1 {print \$2}")
# Extract the interface name from network_info
iface_name=$(echo "$network_info" | grep "$iface" | awk -F', ' '{print $1}' | awk -F')' '{print $1}' | awk -F'(' '{print $2}')
# Assign "Local Loopback" to lo0 or 127.0.0.1
if [[ "$iface" == "lo0" || "$ip_main" == "127.0.0.1" ]]; then
iface_name="Local Loopback"
fi
# Assign "VPN" to unconfigured tun interfaces
if [[ -z "$iface_name" && "$iface" == utun* && "$ip_main" == "not configured" ]]; then
iface_name="VPN"
fi
# Check if the interface is disabled
iface_status=$(networksetup -getnetworkserviceenabled "$iface_name" 2>/dev/null)
[ "$iface_status" == "Disabled" ] && status="disabled" || status="enabled"
# Check if the -a flag is set
if [[ "$1" == "-a" || "$ip_main" != "not configured" ]]; then
# Print the information
echo "Interface: $iface"
echo "Interface name: $iface_name"
echo "Status: $status"
echo "IP principal: $ip_main"
echo ""
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment