Initial commit

This commit is contained in:
Christopher Teutsch 2019-03-24 16:16:11 +01:00
commit 23d826c5c9
Signed by: iwonder
GPG Key ID: 0EE33D788D50130D
4 changed files with 114 additions and 0 deletions

12
README.markdown Normal file
View File

@ -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

27
ipch.py Executable file
View File

@ -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()

60
st.py Executable file
View File

@ -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))

15
tables.sql Normal file
View File

@ -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
);