OPNsense Forum

Archive => 18.1 Legacy Series => Topic started by: privateer on March 26, 2018, 10:55:01 am

Title: cron to check wan ip?
Post by: privateer on March 26, 2018, 10:55:01 am
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
Title: Re: cron to check wan ip?
Post by: elektroinside on March 26, 2018, 02:22:16 pm
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.
Title: Re: cron to check wan ip?
Post by: privateer on March 30, 2018, 03:00:41 pm
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:

Code: [Select]
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).
Title: Re: cron to check wan ip?
Post by: elektroinside on March 30, 2018, 04:04:04 pm
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...
Title: Re: cron to check wan ip?
Post by: 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. :)

Code: [Select]
#!/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
Title: Re: cron to check wan ip?
Post by: elektroinside on March 31, 2018, 08:11:57 am
Just a small typo:

interface="igb0"

Thank you for writing the script :-)
Title: Re: cron to check wan ip?
Post by: marjohn56 on March 31, 2018, 08:14:13 am
all my code has at least one typo. :)
Title: Re: cron to check wan ip?
Post by: elektroinside on March 31, 2018, 08:19:07 am
Got it :) good practice  ;D
Title: Re: cron to check wan ip?
Post by: privateer on April 01, 2018, 04:31:22 pm
thanks a lot, i'll try it soon!


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. :)

Code: [Select]
#!/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