0

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 192.168.12.1 $wifi

To actually get it working. I happen to know that 192.168.12.1 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:

...
192.168.12.0    0.0.0.0         255.255.255.0   U     0      0        0 wlan3

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

Does anyone know a programmatic way I can get 192.168.12.1 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 192.168.12.1:

  fixed-address 192.168.12.120;
  option routers 192.168.12.1;
  option dhcp-server-identifier 192.168.12.1;
  option domain-name-servers 192.168.12.1;
  fixed-address 192.168.12.120;
  option routers 192.168.12.1;
  option domain-name-servers 192.168.12.1;
  option dhcp-server-identifier 192.168.12.1;

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?

8
  • Doesn't route -n give the IP address of the gateway with a line similar to: 0.0.0.0 192.168.12.1 0.0.0.0 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 192.168.12.0 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

2

You can use the following command to print the gatway:

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

or

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

or

netstat -n -r | grep 'UG\|*wlan3$' | awk '{print $2}'
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
1

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 192.168.25.1;

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

2
  • 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 .