From 23d826c5c9d273b81e13993800e9b4e748e0b86e Mon Sep 17 00:00:00 2001 From: Christopher Teutsch Date: Sun, 24 Mar 2019 16:16:11 +0100 Subject: [PATCH] Initial commit --- README.markdown | 12 ++++++++++ ipch.py | 27 ++++++++++++++++++++++ st.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ tables.sql | 15 +++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 README.markdown create mode 100755 ipch.py create mode 100755 st.py create mode 100644 tables.sql diff --git a/README.markdown b/README.markdown new file mode 100644 index 0000000..76e69be --- /dev/null +++ b/README.markdown @@ -0,0 +1,12 @@ +# Speed-test and IP change scripts + +## Requirements + +* Python 3 with modules `mysql-connector-python` and `speedtest-cli` +* A SQL database (MariaDB is recommended) + +## Usage + +1. Create tables in the database using `tables.sql` +2. Configure the scripts in the fields provided +3. Add cron-jobs for the scripts. I currently use an interval of 15 seconds diff --git a/ipch.py b/ipch.py new file mode 100755 index 0000000..ba2833e --- /dev/null +++ b/ipch.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +import requests +import mysql.connector +import datetime + +# Configuration +DYNDNS_AUTH = {'user':'','pass':''} +DYNDNS_URL = '' +DB_CONFIG = { + 'user': '', + 'password': '', + 'host': '', + 'db': '' + } +result = requests.get(DYNDNS_URL, auth=(DYNDNS_AUTH['user'], DYNDNS_AUTH['pass'])) +s = result.content.decode("unicode_escape").rstrip('\n').split(' ') +cx=mysql.connector.connect(**DB_CONFIG) +c=cx.cursor() +data = (datetime.datetime.now(),s[0],s[1]) +try: + c.execute("INSERT INTO ipch_he (time,status,ip) values (%s,%s,%s)",data) + cx.commit() +except: + cx.rollback() + raise + +c.close() diff --git a/st.py b/st.py new file mode 100755 index 0000000..29a88af --- /dev/null +++ b/st.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +import speedtest +import csv +import datetime +import mysql.connector +import os + +db_config = { + 'user': 'm_imp', + 'password': '123', + 'host': '127.0.0.1', + 'database': 'speedtests' + } + +try: + cx=mysql.connector.connect(**db_config) + c=cx.cursor() +except mysql.connector.Error as err: + exit('Could not open database') + +upload_statement = "INSERT INTO sts (server_id,sponsor,serv_name,time,distance,ping,download,upload) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)" + +try: + s=speedtest.Speedtest() + s.get_servers() + s.get_best_server() + s.download() + s.upload() + d=s.results.dict() + u=( + d['server']['id'], + d['server']['sponsor'], + d['server']['name'], + d['timestamp'], + d['server']['d'], # distance + d['server']['latency'], + d['download'], + d['upload'] + ) +except speedtest.SpeedtestException: + u=( + None, + str(datetime.datetime.utcnow().isoformat())+'Z', + None, + None, + None, + None, + None, + None + ) + +try: + c.execute(upload_statement, u) + cx.commit() +except: + cx.rollback() + raise +c.close() +cx.close() +#print("Uploaded" + str(u)) diff --git a/tables.sql b/tables.sql new file mode 100644 index 0000000..423e955 --- /dev/null +++ b/tables.sql @@ -0,0 +1,15 @@ +CREATE TABLE IF NOT EXISTS ipch ( + time datetime, + status text, + ip text +); +CREATE TABLE IF NOT EXISTS sts ( + test_no integer unsigned primary key auto_increment, + server_id tinytext, + sponsor tinytext, + time datetime, + distance float unsigned, + ping float unsigned, + download float unsigned, + upload float unsigned +);