cron to check wan ip?

Started by privateer, March 26, 2018, 10:55:01 AM

Previous topic - Next topic
Hi,
my opnsense uses pppoe to connect to the internet. sometimes my isp assigns me a private ip (100.xxx.xxx.xxx) which doesn't allow mu to VPN home using dyndns, there's a way to check wan ip address and force reconnect if is a private one?

Andrea
OPNsense 18.1.5 | PPPoE: Eolo Italy | Down: Few Mbit/s | Up: Even Less Mbit/s

According to IANA (https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml), only 100.64.0.0/10 (100.64.0.0 –100.127.255.255) is considered as special purpose address block (https://tools.ietf.org/html/rfc6598).

Is this the case? It is important not to act on any other address blocks.
OPNsense v18 | HW: Gigabyte Z370N-WIFI, i3-8100, 8GB RAM, 60GB SSD, | Controllers: 82575GB-quad, 82574, I221, I219-V | PPPoE: RDS Romania | Down: 980Mbit/s | Up: 500Mbit/s

Team Rebellion Member

That's correct, it' s a private class used by the ISP due to a lack of public ip  . Extracted from the your second link:

Quote[...]It is anticipated that Service Providers
   will use this Shared Address Space to number the interfaces that
   connect CGN devices to Customer Premises Equipment (CPE).[...]

here you can see my connection log:

Mar 26 08:08:16 opnsense: /usr/local/etc/rc.newwanip: On (IP address: 100.115.X.X) (interface: EOLO[wan]) (real interface: pppoe0).
Mar 26 06:29:23 opnsense: /usr/local/etc/rc.newwanip: On (IP address: 78.134.X.X) (interface: EOLO[wan]) (real interface: pppoe0).
Mar 26 04:28:04 opnsense: /usr/local/etc/rc.newwanip: On (IP address: 78.134.X.X) (interface: EOLO[wan]) (real interface: pppoe0).
Mar 22 16:27:39 opnsense: /usr/local/etc/rc.newwanip: On (IP address: 78.134.X.X) (interface: EOLO[wan]) (real interface: pppoe0).
Mar 20 19:06:21 opnsense: /usr/local/etc/rc.newwanip: On (IP address: 78.134.X.X) (interface: EOLO[wan]) (real interface: pppoe0).
Mar 20 19:04:16 opnsense: /usr/local/etc/rc.newwanip: On (IP address: 100.119.X.X) (interface: EOLO[wan]) (real interface: pppoe0).
OPNsense 18.1.5 | PPPoE: Eolo Italy | Down: Few Mbit/s | Up: Even Less Mbit/s

Ok then.
I can tell you how it's done, but you have to do the rest, unfortunately I'm way to busy to try writing one. Can you handle sh scripts?
You need to write a script which collects the ip of your PPPoE interface and add execute permissions to it. If the IP begins with 100.xxx you could bring down the interface, wait for a few secs the bring it back up.
Schedule this with cron, say every 5 minutes. You can do that from the GUI.

How to schedule your custom script (an example) from the GUI:
https://forum.opnsense.org/index.php?topic=7316

But this might not be such a good idea if your ISP assigns, let's say, 15 IPs consecutively, from this private class. You will get disconnected 15 times in a very short period...
OPNsense v18 | HW: Gigabyte Z370N-WIFI, i3-8100, 8GB RAM, 60GB SSD, | Controllers: 82575GB-quad, 82574, I221, I219-V | PPPoE: RDS Romania | Down: 980Mbit/s | Up: 500Mbit/s

Team Rebellion Member

March 30, 2018, 11:37:23 PM #4 Last Edit: March 30, 2018, 11:44:00 PM by marjohn56
This is basically the script you need to run, you'll need to set up the cron event to run it

It's very simple, it uses ifconfig to look for the ipv4 IP address, if it starts wih 100. then it will take down the WAN interface, wait 5 seconds and bring it back up.

You'll need to set the parent interface name to match yours... and as an afterthought change it to pppoe0. :)

#!/bin/sh
# Testing for invalid wan IP

inteface="igb0"

test_string="net 100."
result=$(ifconfig pppoe1 | grep "inet ")

if [ "$result" != "${result%"$test_string"*}" ]; then
ifconfig $interface down
sleep 5
ifconfig $inteface up
fi

OPNsense 24.7 - Qotom Q355G4 - ISP - Squirrel 1Gbps.

Team Rebellion Member

Just a small typo:

interface="igb0"

Thank you for writing the script :-)
OPNsense v18 | HW: Gigabyte Z370N-WIFI, i3-8100, 8GB RAM, 60GB SSD, | Controllers: 82575GB-quad, 82574, I221, I219-V | PPPoE: RDS Romania | Down: 980Mbit/s | Up: 500Mbit/s

Team Rebellion Member

all my code has at least one typo. :)
OPNsense 24.7 - Qotom Q355G4 - ISP - Squirrel 1Gbps.

Team Rebellion Member

Got it :) good practice  ;D
OPNsense v18 | HW: Gigabyte Z370N-WIFI, i3-8100, 8GB RAM, 60GB SSD, | Controllers: 82575GB-quad, 82574, I221, I219-V | PPPoE: RDS Romania | Down: 980Mbit/s | Up: 500Mbit/s

Team Rebellion Member

thanks a lot, i'll try it soon!


Quote from: marjohn56 on March 30, 2018, 11:37:23 PM
This is basically the script you need to run, you'll need to set up the cron event to run it

It's very simple, it uses ifconfig to look for the ipv4 IP address, if it starts wih 100. then it will take down the WAN interface, wait 5 seconds and bring it back up.

You'll need to set the parent interface name to match yours... and as an afterthought change it to pppoe0. :)

#!/bin/sh
# Testing for invalid wan IP

inteface="igb0"

test_string="net 100."
result=$(ifconfig pppoe1 | grep "inet ")

if [ "$result" != "${result%"$test_string"*}" ]; then
ifconfig $interface down
sleep 5
ifconfig $inteface up
fi

OPNsense 18.1.5 | PPPoE: Eolo Italy | Down: Few Mbit/s | Up: Even Less Mbit/s