Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Test ipwatch

on: [push]
jobs:
install_and_test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.9", "3.10", "3.11", "3.12"] # , "pypy3.9", "pypy3.10"]

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Upgrade pip
run: |
python -m pip install --upgrade pip

- name: Install package
run: python -m pip install .

- name: Test scripts
run: |
ipget
ipwatch test_config.txt
ipwatch test_config.txt
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
config.txt
serverCache.json
oldip.txt
saved_ip.txt
__pycache__
.venv
51 changes: 36 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,42 @@ https://github.com/begleysm/ipwatch
## Description
This program gets your external & internal IP addresses, checks them against your "saved" IP addresses and, if a difference is found, emails you the new IP's. This is useful for servers at residential locations whose IP address may change periodically due to actions by the ISP.

## Usage Examples
[config] = path to an IPWatch configuration file
## Installation

1. `python3 ipwatch.py [config]`
2. `./ipwatch.py [config]`
3. `python3 ipwatch.py config.txt`
4. `./ipwatch.py config.txt`
5. `python3 /path/to/dir/ipwatch.py /path/to/dir/config.txt`
6. `./path/to/dir/ipwatch.py /path/to/dir/config/txt`
### Install python3, git and optionally nano

On Debian-based system (e.g. Ubuntu) you can do:

## Installation
### Debian based Linux systems
Install python3, git, & nano by running
```bash
sudo apt install python3 git nano
```

## Create a Python virtual environment (venv)

```bash
sudo python3 -m venv /opt/ipwatch
```

## Install the ipwatch package in this venv

Clone the ipwatch repo by running

```bash
sudo git clone https://github.com/begleysm/ipwatch /opt/ipwatch
git clone https://github.com/begleysm/ipwatch
```

Copy `example_config.txt` to `config.txt` by running
Install the package in the venv

```bash
sudo cp /opt/ipwatch/example_config.txt /opt/ipwatch/config.txt
. /opt/ipwatch/bin/activate
cd ipwatch
pip install
```

Copy `example_config.txt` to `config.txt` by running
```bash
sudo cp example_config.txt /opt/ipwatch/config.txt
```
Since `config.txt` will contain an email password, make it viewable & editable by `root` only by running
```bash
sudo chmod 600 /opt/ipwatch/config.txt
Expand All @@ -50,10 +59,21 @@ sudo nano /opt/ipwatch/config.txt

You can test the setup by running
```bash
sudo python3 /opt/ipwatch/ipwatch.py /opt/ipwatch/config.txt
sudo /opt/ipwatch/bin/ipwatch /opt/ipwatch/config.txt
```

Check out the **Cronjob** section below to make this utility run on its own so that you may be quickly alerted to any IP changes on your system.

## Usage

[config] = path to an IPWatch configuration file

```bash
. /opt/ipwatch/bin/activate
ipwatch [config]

```

## Config File
ipwatch uses a config file to define how to send an email. An example and description is below. A similar config file is in the repo as example_config.txt. You should copy it by running something like `sudo cp example_config.txt config.txt` and then modify `config.txt`. It is recommended that you adjust the permissions of your config file so that no one but you and/or root can read it since it will contain the sender email password.

Expand All @@ -70,6 +90,7 @@ smtp_addr=smtp.gmail.com:587 #this is the SMTP address for the send
save_ip_path=/opt/ipwatch/oldip.txt #this is the location where the saved ip address will be stored
try_count=10 #this defines how many times the system will try to find the current IP before exiting
ip_blacklist=192.168.0.255,192.168.0.1,192.168.1.255,192.168.1.1 #this is a list of IP address to ignore if received
dry_run=0 # do not send email when dry_run=1
```

## Cronjob
Expand Down
1 change: 1 addition & 0 deletions example_config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ smtp_addr=smtp.gmail.com:587
save_ip_path=/user/bob/ipwatch/oldip.txt
try_count=10
ip_blacklist=192.168.0.255,192.168.0.1,192.168.1.255,192.168.1.1
dry_run=0

34 changes: 34 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "ipwatch"
dynamic = ["version"]
description = """This program gets your external & internal IP addresses, checks them against
your "saved" IP addresses and, if a difference is found, emails you the new
IP's. This is useful for servers at residential locations whose IP address may
change periodically due to actions by the ISP."""
readme = "README.md"
requires-python = ">=3.8"
license = "MIT"
keywords = []
authors = [
{ name = "Sean Begley", email = "begleysm@gmail.com" },
{ name = "Tom Vander Aa", email = "tom.vanderaa@gmail.com" },
]

[project.urls]
Documentation = "https://github.com/begleysm/ipwatch#readme"
Issues = "https://github.com/begleysm/ipwatch/issues"
Source = "https://github.com/begleysm/ipwatch"

[project.scripts]
ipwatch = "ipwatch:main"
ipget = "ipwatch.ipgetter:main"

[template.plugins.default]
src-layout = true

[tool.hatch.version]
path = "src/ipwatch/__about__.py"
4 changes: 4 additions & 0 deletions src/ipwatch/__about__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2024-present Tom Vander Aa <tom.vanderaa@gmail.com>
#
# SPDX-License-Identifier: MIT
__version__ = "0.0.1"
6 changes: 6 additions & 0 deletions src/ipwatch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-FileCopyrightText: 2024-present Tom Vander Aa <tom.vanderaa@gmail.com>
#
# SPDX-License-Identifier: MIT

from .ipwatch import main
from . import ipgetter
21 changes: 12 additions & 9 deletions ipgetter.py → src/ipwatch/ipgetter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import socket
import ssl
import json
import os
import os
from datetime import datetime, timedelta

from sys import version_info
Expand Down Expand Up @@ -76,7 +76,7 @@ def __init__(self):
theList = json.load (infile)
except:
pass

if (theList is None
or "expiry" not in theList
or "expiryDisplay" not in theList
Expand Down Expand Up @@ -106,7 +106,7 @@ def __init__(self):
self.server_list = theList["servers"]
theList = None


def get_externalip(self):
'''
This function gets your IP from a random server
Expand All @@ -119,8 +119,8 @@ def get_externalip(self):
if myip != '':
break
return myip,server


def get_local_ip(self):
# From https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Expand All @@ -133,13 +133,13 @@ def get_local_ip(self):
finally:
s.close()
return IP


def get_ips(self):
local_ip = self.get_local_ip()
external_ip, server = self.get_externalip()
return external_ip, local_ip, server


def fetch(self, server):
'''
Expand Down Expand Up @@ -197,6 +197,9 @@ def test(self):
print('\n')
print(resultdict)

if __name__ == '__main__':
def main():
print(myip())

if __name__ == '__main__':
main()

Loading