This is an exercise I thought of on the drive home from work. Just a task to help give context to the concepts I have been learning.
This exercise came from wanting to VPN into my home network. The home network has a dynamic external IP address, which means I need a way to know if that IP has changed.
The script IPcheck.py helps solve that problem. It’;s been created to check the current external IP against a stored value and notify by email if the two do not match. It’;s intended to be run as a scheduled task. For example, I have a cron job setup on my Raspberry Pi.
The GitHub for this project is located here. At the time of writing this entry, there is still a little work to be done, such as adding doc strings.
The script was written for Python3 and requires the following modules:
- requests
- os.path
- smtplib
- email.mime.text
Before running the script, the file needs to have some values set to work.
The first three are required for emailing to work. For my testing, I didn’;t need to auth against the SMTP server, as such this is not in the script. If you require SMTP auth then it will need to be added. ipstorepath is set as a raw string to allow paths such as “C:” without the need for escape characters.
As a bit of a side note, when using cron, I needed to use a full path in ipstorepath, however when running manually, a filename is all that’;s needed.
|
|
The logic of the script is as follows:
- Get external IP address
- Check if IPStore file exists
- If it does, compare the stored value to the retrieve value
- If the values do not match, update the IPStore file and email the new IP
- If the values match, end
- If it does, compare the stored value to the retrieve value
- If the file does not exist, create the file with the retrieved IP
The code for this logic:
|
|
The following code is used to get the current external IP Address. This snippet will grab the external IP address from ipify as plain text.
|
|
The following function is used to write the retrieved IP address to the IPStore file.
|
|
Sending emails from Python requires a few core arguments. These have been set at the top of the script. In addition to those we need to past ipaddr to the function so it can be contained email. We use MIMEText to ensure the email is properly formatted.
|
|
That about covers this little script. I intend to create more scripts like this to help develop my skills and provide a breakdown explaining the logic and code segments.