Display gateway for an interface

Is there a way to show the gateway of an interface when you have multiple interfaces each with their own gateways. (I manually change the route now and then).

I have a wifi interface that is picking up an IP from the local AP but in order to make it the new route, I need to know its gw IP. In some cases, it's not .1.

I can use route to see the default route of the enabled route but how can I display the gw of the wlan0 interface since it only shows a * under Gateway.

I can use ifstatus wwan and grep nexthop for example but is that the only way?

I'm not sure what you mean. a gateway is not a property of an interface. a gateway is an entry in the forwarding information base. there is one per host, and it need not be the same on different hosts.

2 Likes

Maybe I'm not explaining my question correctly or using the right terms.
All I am trying to do is to see which gw the wlan0 interface is so I can manually switch routes.

Each interface gets a DHCP IP from the local router that it is connecting to but other than using 'ifstatus wwan' I wondered if there was any other way of knowing the gw for a network.

what happens when you type ip route show on a machine connected to both networks?

You should be able to have two default routes with different metrics, and switch back and forth between them by changing the metric.

NOTE: I just looked up info on this, and apparently it's not possible to change a route's metric in-place, you have to create a new one and delete the old one... sigh

2 Likes

ip route shows the routes in use at the time but that's not what I am asking about :).
I don't have multiple routes enabled at the same time, I just switch between routes manually.

I think I need to delete this question as it will not be useful to anyone who finds it.
Again, I just want to know what is the gateway of a network I might want to use.

In this case for example, I have wlan0 which got an IP from the local AP.
However, if I didn't know what the gateway was for that AP, how would I look it up from the command line.
I can use 'ifstatus wwan' and grep nexthop for example but is that the only way?

The issue is that there is no such thing as "the gateway for an interface". For example it's possible for an interface to be connected to a network that has say 6 different gateways to 6 different ISPs... Even in home networks we see plenty of people using MWAN3 to balance multiple ISPs, but they could just as well have two different routers. In an ipv6 network for example all those gateways could be advertising router advertisements and your device would find all of them.

But as far as I can tell you want to know which gateway did dhcpv4 tell your device about and I think ifstatus is probably the right way.

1 Like

To be clear, you cannot know the gateway of a given network unless:

  • that network runs DHCP, you've successfully connected and your host has been assigned a gateway via DHCP Option No. 3; or
  • the Network Administrator tells you the IP of the gateway so you can use Static IP Addressing

ip route show does exactly that.

Here's an example with at device that uses WLAN0 as it's WAN connection:

root@OpenWrt:~# ip route show | grep wlan0
default via 192.168.xxx.1 dev wlan0 proto static src 192.168.xxx.xxx 
192.168.xxx.0/24 dev wlan0 proto kernel scope link src 192.168.xxx.xxx

In this example, 192.168.xxx.1 is the gateway.

There are many more ways to receive default routes, such as Router Advertisement (NDP) for IPv6, and any dynamic routing protocol.

And ifstatus <interface> shows the gateways as "nexthop" in the "route" array. Inactive routes are available in the "inactive" array.

1 Like
  • My mistake, I did assume the OP was referring to IPv4 only.
  • Most routing protocols require you to establish the proper LAN/IP connection first. I assumed if the OP were using such a protocol, they'd already have the skills and knowledge requisite to see (or determine) the gateway.

I only considered a simple user scenario (i.e. DHCP-based LAN and Static LAN).

This is exactly on track. NON active route.

The question was simpler than how long it's gotten mainly it seems because I used the wrong term, interface :).

To again be clear, I'm saying that the wireless interface receives a DHCP IP from the AP but it is not the active route. The question is, other than grepping for 'nexthop', if I didn't know what the APs gw IP was, how could I switch that route. So, what other way could I find the APs gateway so I could manually switch to that route.

I had clarified in another comment that I used the wrong term, 'interface'.

Are you connecting to multiple access points, have multiple addresses, but a single gateway, and you want to change the gateway to switch the AP used to reach the internet?

you could take down the interface it's using currently as WAN, and restart the wifi interface used for your WWAN, and voila, you'd be using your alternative route.

Are you connecting to multiple access points, have multiple addresses, but a
single gateway, and you want to change the gateway to switch the AP used
to reach the internet?

There are two interfaces picking up a DHCP IP. Only the wired interface is default but the wireless interface also picks up a DHCP IP and is what I manually switch to when needed. No need for multiple routes, I'm just switching between routes as needed.

you could take down the interface it's using currently as WAN, and restart the wifi
interface used for your WWAN, and voila, you'd be using your alternative route.

What I cannot seem to find a clear answer to is how to set the default route for this router.

Let me try to explain again.

For example, eth0 is getting a DHCP IP from a wired connection and the DHCP server on that network while the wifi is getting a DHCP IP from the AP which is on a different network.

The default route is the wired interface.

Once they both have a DHCP IP, it's simple to swap routes but how do I manually set which I want to be default? For example, to go from the wired network which has a gateway of 192.168.1.1 to the wifi network which has a gateway of 192.168.0.1, I use the following.

ip route change default via 192.168.0.1 dev wlan0
To go back, I just change the gw IP and interface name.

Now, if I wanted to make the wireless the default network on bootup, how do I do that?
More importantly, what if the wifi becomes unavailable, would the router automatically pick the wired connection or would it just stop communicating.

Someone had suggested using set option metric 10 to assign a higher metric to one of the interfaces but I want to make sure I know what I'm doing before doing it.

This is what mwan3 is made for.
https://openwrt.org/docs/guide-user/network/wan/multiwan/mwan3

2 Likes

If you are not eager to deal with mwan3, a simpler solution is to assign different metrics on the interfaces. Set 10 for the wired and 20 for the wireless, wired will take precedence like this.
Create a script to change metric for the wired to 30 when you want to route from the wireless and call it manually or with some automation. Then create another script to reverse the action and call it accordingly.
You can alternatively do all these manually in Luci or uci.

Hi Trendy,

Do you mean something like this thread?

If so, might you have a link/lead on where to find a script that someone might have made available. I'm not a coder and in the post above, the person says 'cack-handed but works for me' which sounds like he says, works for him but may not be the best way.

If you are going into so much trouble to detect failed links, better configure mwan3.
Otherwise only the ip route change command alone will do also.
A simple script of what I suggested taking in account that wan1 has metric 10 and wan2 20 on boot, would be:

uci set network.wan1.metric=30
uci commit network
ifup wan1

then to restore the initial status:

uci set network.wan1.metric=10
uci commit network
ifup wan1

Wait, what I am wanting to do is not a lot of trouble or complicated so maybe my question is not being understood or I've not explained it well enough, yet.

I have two DHCP IPs being assigned to a router, one is wired, one is wireless.
The interfaces being used are br-wan and wlan0 in this case when using 'ifconfig' to look.

I use the following command to use the wireless route.
ip route change default via x.x.x.x dev wlan0

I put the above route change command in rc.local but that is not changing the route.
I simply want one of these routes to be the default until I manually change it again OR, if wireless becomes no longer available, I want the router to fall back to wired.

That's it, nothing more complex or complicated :).

It doesn't work in rc.local because that executes only once when booting, and rather early. If (and it almost certainly will) an interface comes up later than that, the DHCP client process will overwrite your action by installing the ISP-provided default route.

Setting option defaultroute 0 on the interface should prevent DHCP from installing a default route. In that case you have to set it by script-- which requires knowing the IP of the 'next hop' gateway on the ISP side of your ISP link.

Again though mwan3 is made for exactly this use case, including ping testing for ISP failure and switching over to the backup ISP.

2 Likes

It doesn't work in rc.local because that executes only once when booting, and rather early.

That's what I was wondering about. Thanks for confirming this.

Setting option defaultroute 0 on the interface should prevent DHCP from installing
a default route. In that case you have to set it by script-- which requires knowing
the IP of the 'next hop' gateway on the ISP side of your ISP link.

I realize there is a program to do this but I'm wanting to understand how this could be done manually.

The only part I don't understand is why would I care about the upstream provider? I only want to set the route to the local router/gateway.

I'm not sure that 'option defaultroute 0' would work since I do want the wired interface to always be available should the wireless become inaccessible or of course, if I manually change the route.

Maybe you mean combined with the weight options I need to also set on both interfaces in the network file?