Advertisement
Lugico

one.com DDNS Python Script

Jul 15th, 2021 (edited)
2,847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.00 KB | None | 0 0
  1. '''
  2.  
  3.    ###############################
  4.    ##                           ##
  5.    ##    one.com DDNS Script    ##
  6.    ##                           ##
  7.    ###############################
  8.    | Author       | Lugico       |
  9.    +--------------+--------------+
  10.    | Email        | me@lugico.de |
  11.    +--------------+--------------+
  12.    | Discord      | Lugico#4592  |
  13.    +--------------+--------------+
  14.    | Version      | 2.1          |
  15.    +--------------+--------------+
  16.    | Last Updated | 2021-07-09   |
  17.    +--------------+--------------+
  18.  
  19.  
  20.    Note:
  21.        This script is not very fail proof.
  22.        something as little as a missing internet connection or a
  23.        slight amount of packet loss can cause the script to crash
  24.        or behave unexpectedly.
  25.  
  26.        I'm open to suggestions and will be glad to help if you
  27.        have trouble getting the script to work.
  28.  
  29. '''
  30.  
  31.  
  32.  
  33. # YOUR ONE.COM LOGIN
  34. USERNAME="email.address@example.com"
  35. PASSWORD="Your Beautiful Password"
  36.  
  37. # YOUR DOMAIN ( NOT www.example.com, only example.com )"
  38. DOMAIN="example.com"
  39.  
  40. # LIST OF SUBDOMAINS YOU WANT POINTING TO YOUR IP
  41. SUBDOMAINS = ["myddns"]
  42. # SUBDOMAINS = ["mutiple", "subdomains"]
  43.  
  44.  
  45. # YOUR IP ADDRESS.
  46. IP='AUTO'
  47. # '127.0.0.1' -> IP  Address
  48. # 'AUTO'      -> Automatically detect using ipify.org
  49. # 'ARG'       -> Read from commandline argument ($ python3 ddns.py 127.0.0.1)
  50.  
  51.  
  52. # CHECK IF IP ADDRESS HAS CHANGED SINCE LAST SCRIPT EXECUTION?
  53. CHECK_IP_CHANGE = True
  54. # True = only continue when IP has changed
  55. # False = always continue
  56.  
  57. # PATH WHERE THE LAST IP SHOULD BE SAVED INBETWEEN SCRIPT EXECUTIONS
  58. # not needed CHECK_IP_CHANGE is false
  59. LAST_IP_FILE = "lastip.txt"
  60.  
  61.  
  62. import requests
  63. import json
  64. import os
  65. import sys
  66.  
  67. if IP == 'AUTO':
  68.     IP = requests.get("https://api.ipify.org/").text
  69.     print(f"Detected IP: {IP}")
  70. elif IP == 'ARG':
  71.     if (len(sys.argv) < 2):
  72.         raise Exception('No IP Adress provided in commandline parameters')
  73.         exit()
  74.     else:
  75.         IP = sys.argv[1]
  76.  
  77. if CHECK_IP_CHANGE:
  78.     try:
  79.         # try to read file
  80.         with open(LAST_IP_FILE,"r") as f:
  81.             if (IP == f.read()):
  82.                 # abort if ip in file is same as current
  83.                 print("IP Address hasn't changed. Aborting")
  84.                 exit()
  85.     except IOError:
  86.         pass
  87.  
  88.     # write current ip to file
  89.     with open(LAST_IP_FILE,"w") as f:
  90.         f.write(IP);
  91.  
  92.  
  93. def findBetween(haystack, needle1, needle2):
  94.     index1 = haystack.find(needle1) + len(needle1)
  95.     index2 = haystack.find(needle2, index1 + 1)
  96.     return haystack[index1 : index2]
  97.  
  98.  
  99. # will create a requests session and log you into your one.com account in that session
  100. def loginSession(USERNAME,  PASSWORD):
  101.     print("Logging in...")
  102.  
  103.     # create requests session
  104.     session = requests.session();
  105.  
  106.     # get admin panel to be redirected to login page
  107.     redirectmeurl = "https://www.one.com/admin/"
  108.     r = session.get(redirectmeurl)
  109.  
  110.     # find url to post login credentials to from form action attribute
  111.     substrstart = '<form id="kc-form-login" class="Login-form login autofill" onsubmit="login.disabled = true; return true;" action="'
  112.     substrend = '"'
  113.     posturl = findBetween(r.text, substrstart, substrend).replace('&amp;','&')
  114.  
  115.     # post login data
  116.     logindata = {'username': USERNAME, 'password': PASSWORD, 'credentialId' : ''}
  117.     session.post(posturl, data=logindata)
  118.  
  119.     print("Sent Login Data")
  120.  
  121.     return session
  122.  
  123.  
  124. # gets all DNS records on your domain.
  125. def getCustomRecords(session, DOMAIN):
  126.     print("Getting Records")
  127.     getres = session.get("https://www.one.com/admin/api/domains/" + DOMAIN + "/dns/custom_records").text
  128.     #print(getres)
  129.     return json.loads(getres)["result"]["data"];
  130.  
  131.  
  132. # finds the record id of a record from it's subdomain
  133. def findIdBySubdomain(records, subdomain):
  134.     print("searching domain '" + subdomain + "'")
  135.     for obj in records:
  136.         if obj["attributes"]["prefix"] == subdomain:
  137.             print("Found Domain '" + subdomain + "': " + obj["id"])
  138.             return obj["id"]
  139.     return ""
  140.  
  141.  
  142. # changes the IP Address of a TYPE A record. Default TTL=3800
  143. def changeIP(session, ID, DOMAIN, SUBDOMAIN, IP, TTL=3600):
  144.     print("Changing IP on subdomain '" + SUBDOMAIN + "' - ID '" + ID + "' TO NEW IP '" + IP + "'")
  145.  
  146.     tosend = {"type":"dns_service_records","id":ID,"attributes":{"type":"A","prefix":SUBDOMAIN,"content":IP,"ttl":TTL}}
  147.  
  148.     dnsurl="https://www.one.com/admin/api/domains/" + DOMAIN + "/dns/custom_records/" + ID
  149.  
  150.     sendheaders={'Content-Type': 'application/json'}
  151.  
  152.     session.patch(dnsurl, data=json.dumps(tosend), headers=sendheaders)
  153.  
  154.     print("Sent Change IP Request")
  155.  
  156.  
  157.  
  158. # Create login session
  159. s = loginSession(USERNAME, PASSWORD)
  160.  
  161. # get dns records
  162. records = getCustomRecords(s, DOMAIN)
  163. #print(records)
  164.  
  165. # loop through list of subdomains
  166. for subdomain in SUBDOMAINS:
  167.     #change ip address
  168.     changeIP(s, findIdBySubdomain(records, subdomain), DOMAIN, subdomain, IP, 600)
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement