Advertisement
Lugico

one.com DDNS Python Script

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