After manually connecting my wifi interface to an access point with:

iwconfig $wifi essid "$1"
dhclient $wifi
# ... $wifi = wlan3

I'm finding that I need to run:

route add default gw $wifi

To actually get it working. I happen to know that is the IP of the access point here, but I need a programmatic way of determining that once connected.

The closest I've found is running route -n which includes the line:

...   U     0      0        0 wlan3

I've been told that given here I can't automatically assume, that the last number could possibly be different.

Does anyone know a programmatic way I can get here?

Update: I found a file called /var/lib/dhcp/dhclient.wlan3.leases. When I grep the access point IP I see it listed several times. I'm still not sure what option would be the best to parse.

Results of cat /var/lib/dhcp/dhclient.wlan3.leases | grep

  option routers;
  option dhcp-server-identifier;
  option domain-name-servers;
  option routers;
  option domain-name-servers;
  option dhcp-server-identifier;

Update #2: Using another device where I have never run route add ... but have run the first command listed. I find that the dhclient lease file doesn't exist. Maybe it was the route command that created this file?

  • Doesn't route -n give the IP address of the gateway with a line similar to: UG 0 0 0 eth0 as you want to route the traffic to your gateway
    – fcbsd
    Nov 14, 2017 at 18:22
  • @fcbsd route -n shows the subnet but it doesn't give the last number in the IP needed. Nov 14, 2017 at 18:24
  • 1
    I was just about to suggest dhclient - you want the option routers
    – fcbsd
    Nov 14, 2017 at 18:26
  • @fcbsd cool feel free to post as a complete answer Nov 14, 2017 at 18:28
  • Did you need to print only the gatway from the command line?
    – GAD3R
    Nov 14, 2017 at 18:31

2 Answers 2


You can use the following command to print the gatway:

ip route show | awk '/default/ { print $3 }'


netstat -n -r | awk '/UG/ { print $2 }'


netstat -n -r | grep 'UG\|*wlan3$' | awk '{print $2}'
  • thanks this is almost right except as is it returns the gateway for my Ethernet which is different. Though I do see the IP I need when I run ip route show pastebin.com/UFV4n3qY Nov 14, 2017 at 18:41
  • 1
    Here is the command I have working based on your answer. Hopefully the parsing will work consistently ip route show | grep $wifi | awk '{ print $9 }' Nov 14, 2017 at 18:50

Something like:

grep routers /var/lib/dhclient/dhclient-eth0.leases |sort -u |cut -d ' ' -f 5 |sed -e 's/;//'

will generate the needed IP.

sort -u gives a list of unique lines like option routers;

cut -d ' ' -f 5 splits the line by spaces and gives you field 5 which is and then the sed -e 's/;//' removes the trailing semi-colon.

  • Sorry I spoke too soon. I updated my question with a second update. Finding that the file mentioned doesn't seem to exist on devices before the route command is run. Nov 14, 2017 at 18:36
  • if you run /sbin/dhclient it should do the networking magic for you
    – fcbsd
    Nov 14, 2017 at 18:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .