Determine default Gateway obtained via DHCP under FreeBSD

By | November 1, 2019

So, lets have a FreBSD installation that got an IP address via DHCP using dhclient, and you want to know in a shell script the assigned default gateway for the specific interface.

You can use the following little script which prints the IP address of the default gateway to stdout.

#!/bin/sh
# read the default gateway from dhclient leases file for interface $1
DGW=`tail -r "/var/db/dhclient.leases.$1" | grep -m1 "router.*" | sed 's/;$//' | awk '{print \$3}'` > /dev/null
echo $DGW

Assuming you have the script in a file named getgw you can use it as follows to obtain the default gateway assigned to fxp0:

getgw fxp0

Leave a Reply

Your email address will not be published. Required fields are marked *