Skip to main content
Solved

Display router IP?

  • March 22, 2017
  • 2 replies
  • 10 views

Forum|alt.badge.img+12

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?

Best answer by mm2270

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

2 replies

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • Answer
  • March 22, 2017

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

Forum|alt.badge.img+12
  • Author
  • Contributor
  • March 22, 2017

Worked like a charm. Thanks much.