Display router IP?

macmanmk
Contributor

We use static IP's in our environment and have a need to verify default router IP's from time to time. Can anyone recommend an extension attribute or script that would display the default router/gateway info?

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

You can try the following

#!/bin/sh

activeDev=$(netstat -rn | awk '/default/{print $NF}')

if [ ! -z "$activeDev" ]; then
    activePort=$(networksetup -listallhardwareports | grep -B1 "$activeDev" | awk -F': ' '/Hardware Port:/{print $NF}')
    routerIP=$(networksetup -getinfo "$activePort" | awk '/Router:/{print $NF}')

    echo "<result>$routerIP</result>"
else
    echo "<result>No active network</result>"
fi

View solution in original post

2 REPLIES 2

mm2270
Legendary Contributor III

You can try the following

#!/bin/sh

activeDev=$(netstat -rn | awk '/default/{print $NF}')

if [ ! -z "$activeDev" ]; then
    activePort=$(networksetup -listallhardwareports | grep -B1 "$activeDev" | awk -F': ' '/Hardware Port:/{print $NF}')
    routerIP=$(networksetup -getinfo "$activePort" | awk '/Router:/{print $NF}')

    echo "<result>$routerIP</result>"
else
    echo "<result>No active network</result>"
fi

macmanmk
Contributor

Worked like a charm. Thanks much.