angristan / wireguard-install

WireGuard VPN installer for Linux servers
https://stanislas.blog/2019/01/how-to-setup-vpn-server-wireguard-nat-ipv6/
MIT License
7.96k stars 1.3k forks source link

Better IPv4 detection #278

Closed Ravinou closed 2 years ago

Ravinou commented 2 years ago

On some systems like Hetzner VM cloud i have a Point-to-Point interface so i have a peer address on the same line as my public IPv4 (look at peer here : https://linux.die.net/man/8/ip )

An example of ip a with peer is :

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 96:00:00:a2:88:c2 brd ff:ff:ff:ff:ff:ff
    altname enp0s3
    inet XX.XX.XX.XX peer XX.XX.XX.XX/32 brd XX.XX.XX.XX scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::9400:ff:fea2:88c2/64 scope link 
       valid_lft forever preferred_lft forever

With a peer, the output of the command line 74 is : XX.XX.XX.XX peer XX.XX.XX.XX

I just modify this line with awk to print only the first field which is always the IPv4. I think it's correct and it's work like a charm when there is a peer or not now. But tell me if it's not good for you :)

Thanks for your work !

pasdenv commented 1 year ago

Hi guys, my external address was incorrectly detected on one of the VPS, my solution is: :~# ip -4 addr | grep "$(ip r | grep default | grep -Po '(?<=dev )(\S+)' | head -1)$" | grep -Po '(?<=inet )(\S+)' | cut -d'/' -f1

my /etc/network/interfaces:

...
# The primary network interface
allow-hotplug eth0
iface eth0 inet static
        address 5.1##.2##.#2#/24
        gateway 5.1##.2##.1
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 8.8.8.8 8.8.4.4
        pre-up iptables-restore < /etc/iptable_rules
auto eth0:0
iface eth0:0 inet static
        address 192.168.13.1
        netmask 255.255.255.0
...

for example:

cat <<EOF | grep "$(ip r | grep default | grep -Po '(?<=dev )(\S+)' | head -1)$" | grep -Po '(?<=inet )(\S+)' | cut -d'/' -f1
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:c0:d7:44 brd ff:ff:ff:ff:ff:ff
    inet 192.168.13.1/24 brd 192.168.13.255 scope global eth0:0
       valid_lft forever preferred_lft forever
    inet 5.1##.2##.#2#/24 brd 5.1##.2##.2## scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe##::5###:ff:##c0:d###/64 scope link
       valid_lft forever preferred_lft forever
EOF
5.1##.2##.#2#

what do you say to that?