Running Hassio on a Raspberry Pi. Within my network I have a few other R-Pis all serving other various roles. What I’m trying to solve for is a way to monitor the IP address of a network device and detect / kill the connection if the IP changes.
Specifically, one of my boards (a Pine64 running Ubuntu) is serving as a torrent box. This sits behind a VPN hosted by Private Internet Access and run on the board through OpenVPN. The public IP on this board is different from my normal network’s. What I’m hoping to do is monitor this public IP of this device through hassio and if/when the VPN service drops (resulting in a change in the public IP) it kills the service, or at the very least notifies me with a push notification that the IP has changed.
I’ve tried various methods of setting up a killswitch on the device itself, but haven’t had much luck. If this is possible with Hassio I thought it may improve the result since I could get be easily notified.
Does the device with the public IP have some sort of dynamic DNS? If so, you could use the DNS IP to monitor the hostname. As soon as it resolves to something different the value of the sensor changes to the new IP, which you can base your automation on.
This is where my knowledge gap will come in to play. I think this will work, but I’m not sure on the dynamic DNS question.
The device has a static internal IP on the network. I have specified the hostname for it so I can ssh into it using either [email protected] or username@hostname.
The public IP is set by the VPN (when it’s enabled). So if I’m following you correctly, DNS IP would look at the public IP if I supplied it with the hostname of the device? Then, if that changes to match my public IP I could get notified?
I think it might be unclear what public addresses are. By public addresses I mean the address, that your ISP provides to you, and under which you are reachable from the public internet. From what you say this is a bit unclear to me. Am I correct, that the machine connected via OpenVPN is at a remote location, and because of that has a different public IP than the machine Home Assistant is running on?
What you internally use as a hostname is not so important. The sensor I have mentioned is used to resolve hostnames to IP addresses that are reachable from wherever an internet connection is available. The internal IP in that case only is necessary at your router to forward traffic from the outside to the machine within the network (which is called NAT).
What dynamic dns providers do is allowing you to publish your changing public IP to them whenever it changes, and the hostname you have registered with them always points to the updated IP address. This is what the mentioned sensor would check periodically.
To clarify based on this - the device does not have a hostname or dynamic DNS. It sits inside my house, but when running the VPN it appears as though it’s located somewhere else.
When I run the command ‘curl ipinfo.io’ it returns a location outside of my home network. However, when I run the same command on a second device (not behind a VPN) it returns the correct location.
Sorry for the confusion, this stuff is slightly above my level of understanding.
Ok, I think I understood now what your setup is:
The one machine you are referring to uses the OpenVPN to connect to some external VPN service to provide privacy when accessing the internet. Or to work around regional restrictions.
In that case there’s no builtin method to monitor this. You would need some kind of workaround for that. The easiest method that come to my mind would be to set up a cron-job on the OpenVPN machine. This cron-job periodically checks the public IP. In bash you could easily fetch the IP like this:
The script should save the result (stored in the IP variable) in a local file. But before saving the IP, compare it to the value that originally was in the file.
If the value has changed, then you can to use the RESTful API to trigger an event or execute an automation. Whatever you like best to handle the event.
I do similar on my windows machine using utorrent and ProtonVPN.
I monitor the ProtonVPN log file, when the last line says “…exit-event…” or "…IP-change… I use Eventghost to shutdown utorrent and send HA a message via MQTT to send me a notification
I know you wanted a HASSIO solution but thought you could try monitoring of the log file somehow and get a notification at least ?
I run Glances on my Xigmanas machine and use the following sensor to get the Public IP:
- platform: rest
resource: http://X.X.X.X:61208/api/3/ip/public_address
name: Public IP
value_template: '{{ value_json.public_address }}'"
Since I not have been able to figure out how to start Glances on boot up on my xigmanas machine I use the SSH & Web Terminal add-on in Hassio to run it.
Probably not the sexiest setup, but it works and my girlfriend would not be happy if I spend more time on it.