Browse Source

added send log to email

master
Rune Olsen 2 months ago
parent
commit
76475d0fad
  1. 1
      .gitignore
  2. 12
      config-template.ini
  3. 55
      py-dyndns.py

1
.gitignore

@ -1,3 +1,4 @@
config.ini
.idea/
py-getdomains.py
dyndns.log

12
config-template.ini

@ -11,10 +11,10 @@
[DYNDNS]
api_key =
baseuri = example.com
ip4 = http://ip4.iurl.no
ip6 = http://ip6.iurl.no
ip4 = https://ip4.iurl.no
ip6 = https://ip6.iurl.no
logfile = /var/log/dyndns.log
[SITE0]
subdomainid = 000000
[SITE1]
subdomainid = 000000
smtp_server = example.com
smtp_user = you@example.com
smtp_pass = [password for SMTP server]
smtp_from = you@example.com

55
py-dyndns.py

@ -7,20 +7,26 @@ import argparse
import requests
import os
import time
import smtplib
import ssl
import datetime
import email
from email.message import EmailMessage
from email import utils
# Config stuff
filepath = os.path.dirname(os.path.abspath(__file__))
config = configparser.ConfigParser()
config.read(filepath+'/config.ini')
config.read('/Users/rune/Seafile/Seafile/Live/Live/Documents/Code/Python/py-dyndns/config.ini')
config_file = '/home/rune/.config/dyndns/config.ini'
api = config['DYNDNS']['api_key']
ip4 = config['DYNDNS']['ip4']
ip6 = config['DYNDNS']['ip6']
domain_name = config['DYNDNS']['baseuri']
logging.basicConfig(filename=config['DYNDNS']['logfile'],level=logging.INFO)
local_ip4 = urllib.request.urlopen("http://ip4.iurl.no").read().decode('utf-8')
# The program
local_ip4 = urllib.request.urlopen("https://ip4.iurl.no").read().decode('utf-8')
# The program
def updateip(ip, domain):
data = {'data': ip}
@ -115,7 +121,7 @@ def listcurrent():
def addnewdomin(newdata):
currentlocalip = urllib.request.urlopen("http://ip4.iurl.no").read().decode('utf-8')
response = createnewdomain(newdata['add'][0], currentlocalip)
if response is not 'Fail':
if response != 'Fail':
logging.info('Success! Domain %s updated with ID: %s', newdata['add'][0], response['domain_record']['id'])
domainid = str(response['domain_record']['id'])
section = str(newdata['add'][0])
@ -123,12 +129,42 @@ def addnewdomin(newdata):
config[section] = {
'subdomainid': str(domainid)
}
with open('config.ini', 'w') as f:
with open(config_file, 'w') as f:
config.write(f)
print('Domain ' + str(section) + ' added with ID ' + str(domainid) + ' and config.ini updated!')
else:
print('ERROR! Domain not added!')
def sendmail(themail, title, newdata):
now = datetime.datetime.now()
today = str(now.strftime("%Y-%m-%d %H:%M"))
msg = EmailMessage()
msg['Subject'] = f'{title} ' + today
msg['To'] = newdata['send'][0]
msg['From'] = config['DYNDNS']['smtp_from']
msg['Date'] = email.utils.formatdate()
msg['Return-Path'] = config['DYNDNS']['smtp_from']
port = 587 # For starttls
smtp_server = config['DYNDNS']['smtp_server']
login_email = config['DYNDNS']['smtp_user']
password = config['DYNDNS']['smtp_pass']
msg.set_content(themail)
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(login_email, password)
server.send_message(msg)
def sendlog(newdata):
with open(config['DYNDNS']['logfile'], 'r') as log:
speedlog = log.read()
sendmail(speedlog, 'Log files for dynDNS', newdata)
# Commandline arguments
@ -137,8 +173,9 @@ parser.add_argument('-l', '--list', help='List the available sub-domains on the
'config.ini file.', required=False, action="store_true")
parser.add_argument('-c', '--current', help='List the current IP address for the sub-domains in the config.ini file',
required=False, action="store_true")
parser.add_argument('-a', '--add', help='Add a new domain to your DigitalOcean account and to the config.ini file',
required=False, nargs=1, action='append')
parser.add_argument('-a', '--add', help='Add a new domain to your DigitalOcean account and to the config.ini file', required=False, type=str, nargs=1, action='append')
parser.add_argument('-s', '--send', help='Add a new domain to your DigitalOcean account and to the config.ini file', required=False, type=str, nargs=1, action='append')
args = vars(parser.parse_args())
if args['list']:
listdomains()
@ -146,5 +183,7 @@ elif args['current']:
listcurrent()
elif args['add']:
addnewdomin(args)
elif args['send']:
sendlog(args)
else:
runupdate()

Loading…
Cancel
Save