How to get gateway of network interface?

8,722

Solution 1

When configuring the network interface using dhclientit is possible to set the verbose flag which will yield something like

~ $  dhclient -v vpn_se
Internet Systems Consortium DHCP Client 4.2.4
Copyright 2004-2012 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/vpn_se/00:ac:2b:d3:d9:e8
Sending on   LPF/vpn_se/00:ac:2b:d3:d9:e8
Sending on   Socket/fallback
DHCPDISCOVER on vpn_se to 255.255.255.255 port 67 interval 3 (xid=0xa0efb173)
DHCPREQUEST of 10.211.1.22 on vpn_se to 255.255.255.255 port 67 (xid=0x73b1efa0)
DHCPOFFER of 10.211.1.22 from 10.211.254.254
DHCPACK of 10.211.1.22 from 10.211.254.254
bound to 10.211.1.22 -- renewal in 1403 seconds.

where the lines starting with DHCPOFFER and DHCPACK contain the clients local IP and the VPN gateway.

Solution 2

  • Try ip r. It will show info in the form

    default via aa.bb.cc.dd ...
    
  • or netstat -r -

    Kernel IP routing table
    Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
    
  • traceroute goo.gl or tracepath goo.gl will show all routers (VPN-gateway at the top)

Solution 3

To be honest , as you are on a private network, the easiest way is to ask your network administrator.

The next easiest way is to connect with a working connection, perhaps without VPN

you then run ifconfig and ip r, or similar, and you can get useful information about your network including netmask.

inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.255

ip r default via 10.0.0.1 dev enp0s3 proto static metric 100 tracepath google.com

If you can not obtain your network architecture that way, it can be difficult to impossible.

Start with typical guesses such as x.x.x.1 or x.x.x.254

If that does not work it can be difficult to impossible, it is going to depend on the network architecture, the gateway can be firewalled or may only respond to certain features such as mac or ipaddress, or 802.1x networks. Some networks will not allow you to connect even if you know the gateway.

You would have to start with a packet sniffer, such as tcpdump or wireshark, capture network traffic, and make a guess at the gateway.

Or you can try to discover hosts using nmap

nmap -p 22 --open x.x.x.0/24

The nmap command assumes the gateway is running ssh on the standard port, 22

If that does not work, you have to be more liberal with nmap and guess the gateway

Note: Even if you discover or guess the gateway, you may not be able to connect.

Share:
8,722

Related videos on Youtube

Suppenkasper
Author by

Suppenkasper

Updated on September 18, 2022

Comments

  • Suppenkasper
    Suppenkasper over 1 year

    Playing around with VPN connections on SoftEther, I created a virtual network interface for the VPN. Now in order to set iptables to route via this network interace, I need the gateway of the VPN server (local ip of the host). Is there an easy way to get it? I tried ip neigh but this does not list my virtual network interface.

  • Suppenkasper
    Suppenkasper over 6 years
    The problem is that the gateway is not yet listed in iptables. For my VPN connection to work I want to add it as default but first I have to know the local ip address of the router that I am connected to via VPN
  • N0rbert
    N0rbert over 6 years
    Normal PPTP and OpenVPN gateways are shown in the output of these commands. Does ifconfig -a list you virtual interface?
  • N0rbert
    N0rbert over 6 years
    Try traceroute or tracepath.
  • Suppenkasper
    Suppenkasper over 6 years
    That would require sending packages via VPN which is not possible because the routing is not done yet.
  • Panther
    Panther over 6 years
    @N0rbert - your answer is correct, as long as one is already connected to the network / gateway. If you are not already connected, and do not know the gateway, it can be difficult to impossible to discover or access can be blocked even if you discover the correct ip, part of wireless security or otherwise.