Jump to content

Raspberry Pi - IP Check

rustikles

I'd like to add a script to crontab for my Pi that:

  1. checks the current external IP of the device
  2. issue a 'sudo reboot' if the IP returned is within a certain range e.g. '70.x.x.x --> 75.x.x.x'

This may seem like overkill, but I'm just going to assume that if the VPN is no longer connected, there was some kind of network disruption that may have affected other parts of the system (e.g. drive mappings) as well. Hence, the reboot.

 

Specifically, my Pi is running as an VPN client, and I'm trying to check to make sure traffic is being actually routed through the VPN. I'd just as soon make some kind of firewall rule to only allow VPN traffic, but that won't automatically refresh the network connection if traffic isn't being routed as desired.

 

Would anymore mind throwing together a few lines of code to give me an example of how it could work? 

 

EDIT: If it saves some time, this should return the current external IP:

wget http://ipecho.net/plain -O - -q ; echo

CPU: i5 4670k • Cooler: Corsair H100i • Motherboard: MSI Z87 MPOWER • RAM: Crucial Ballistix Elite 2x 8GB • Storage: Samsung 840 250GB SSD, 2x WD Red 3TB • GPU: EVGA GTX 780 3GB • PSU: Corsair RM750W • Case: Corsair 750D • Mouse: Razer Naga 2012 (I actually use the thing for productivity/media buttons) • Keyboard: Ducky Shine 3 w/ Browns - Green LED • Monitor: Asus PB278Q 27" 2560 x 1440, ASUS PB238Q 23" 1920x1080 • Lighting: 2m NZXT Sleeved Blue LED Strip • pcpartpicker.com/p/3cHfZ

 

Link to comment
Share on other sites

Link to post
Share on other sites

This API will show your public IP: https://api.ipify.org/. You could run a cronjob to power-cycle the network interface card? Is your pi connecting to the network through WiFi or Ethernet? (Wifi connections on the Pi do not stay active, they always drop after an inactivity period due to kernel-based power saving features)

Speedtests

WiFi - 7ms, 22Mb down, 10Mb up

Ethernet - 6ms, 47.5Mb down, 9.7Mb up

 

Rigs

Spoiler

 Type            Desktop

 OS              Windows 10 Pro

 CPU             i5-4430S

 RAM             8GB CORSAIR XMS3 (2x4gb)

 Cooler          LC Power LC-CC-97 65W

 Motherboard     ASUS H81M-PLUS

 GPU             GeForce GTX 1060

 Storage         120GB Sandisk SSD (boot), 750GB Seagate 2.5" (storage), 500GB Seagate 2.5" SSHD (cache)

 

Spoiler

Type            Server

OS              Ubuntu 14.04 LTS

CPU             Core 2 Duo E6320

RAM             2GB Non-ECC

Motherboard     ASUS P5VD2-MX SE

Storage         RAID 1: 250GB WD Blue and Seagate Barracuda

Uses            Webserver, NAS, Mediaserver, Database Server

 

Quotes of Fame

On 8/27/2015 at 10:09 AM, Drixen said:

Linus is light years ahead a lot of other YouTubers, he isn't just an average YouTuber.. he's legitimately, legit.

On 10/11/2015 at 11:36 AM, Geralt said:

When something is worth doing, it's worth overdoing.

On 6/22/2016 at 10:05 AM, trag1c said:

It's completely blown out of proportion. Also if you're the least bit worried about data gathering then you should go live in a cave a 1000Km from the nearest establishment simply because every device and every entity gathers information these days. In the current era privacy is just fallacy and nothing more.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×