OPNsense
  • Home
  • Help
  • Search
  • Login
  • Register

  • OPNsense Forum »
  • Archive »
  • 18.1 Legacy Series »
  • cron to check wan ip?
« previous next »
  • Print
Pages: [1]

Author Topic: cron to check wan ip?  (Read 4361 times)

privateer

  • Newbie
  • *
  • Posts: 21
  • Karma: 2
    • View Profile
cron to check wan ip?
« 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
Logged
OPNsense 18.1.5 | PPPoE: Eolo Italy | Down: Few Mbit/s | Up: Even Less Mbit/s

elektroinside

  • Hero Member
  • *****
  • Posts: 574
  • Karma: 51
    • View Profile
Re: cron to check wan ip?
« Reply #1 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.
Logged
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

privateer

  • Newbie
  • *
  • Posts: 21
  • Karma: 2
    • View Profile
Re: cron to check wan ip?
« Reply #2 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).
Logged
OPNsense 18.1.5 | PPPoE: Eolo Italy | Down: Few Mbit/s | Up: Even Less Mbit/s

elektroinside

  • Hero Member
  • *****
  • Posts: 574
  • Karma: 51
    • View Profile
Re: cron to check wan ip?
« Reply #3 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...
Logged
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

marjohn56

  • Hero Member
  • *****
  • Posts: 1677
  • Karma: 171
    • View Profile
Re: cron to check wan ip?
« Reply #4 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
« Last Edit: March 30, 2018, 11:44:00 pm by marjohn56 »
Logged
OPNsense 21.7 - Qotom Q355G4 - ISP - Community Fibre 1Gbps.

Team Rebellion Member - If we've helped you remember to applaud

elektroinside

  • Hero Member
  • *****
  • Posts: 574
  • Karma: 51
    • View Profile
Re: cron to check wan ip?
« Reply #5 on: March 31, 2018, 08:11:57 am »
Just a small typo:

interface="igb0"

Thank you for writing the script :-)
Logged
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

marjohn56

  • Hero Member
  • *****
  • Posts: 1677
  • Karma: 171
    • View Profile
Re: cron to check wan ip?
« Reply #6 on: March 31, 2018, 08:14:13 am »
all my code has at least one typo. :)
Logged
OPNsense 21.7 - Qotom Q355G4 - ISP - Community Fibre 1Gbps.

Team Rebellion Member - If we've helped you remember to applaud

elektroinside

  • Hero Member
  • *****
  • Posts: 574
  • Karma: 51
    • View Profile
Re: cron to check wan ip?
« Reply #7 on: March 31, 2018, 08:19:07 am »
Got it :) good practice  ;D
Logged
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

privateer

  • Newbie
  • *
  • Posts: 21
  • Karma: 2
    • View Profile
Re: cron to check wan ip?
« Reply #8 on: April 01, 2018, 04:31:22 pm »
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. :)

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
Logged
OPNsense 18.1.5 | PPPoE: Eolo Italy | Down: Few Mbit/s | Up: Even Less Mbit/s

  • Print
Pages: [1]
« previous next »
  • OPNsense Forum »
  • Archive »
  • 18.1 Legacy Series »
  • cron to check wan ip?
 

OPNsense is an OSS project © Deciso B.V. 2015 - 2023 All rights reserved
  • SMF 2.0.19 | SMF © 2021, Simple Machines
    Privacy Policy
    | XHTML | RSS | WAP2