2021-05-28 01:59:11 +00:00
import json
import requests
import re
import sys
def getRecords ( domain ) : #grab all the records so we know which ones to delete to make room for our record. Also checks to make sure we've got the right domain
2021-09-22 23:33:58 +00:00
allRecords = json . loads ( requests . post ( apiConfig [ " endpoint " ] + ' /dns/retrieve/ ' + domain , data = json . dumps ( apiConfig ) ) . text )
2021-05-28 01:59:11 +00:00
if allRecords [ " status " ] == " ERROR " :
print ( ' Error getting domain. Check to make sure you specified the correct domain, and that API access has been switched on for this domain. ' ) ;
sys . exit ( ) ;
return ( allRecords )
def getMyIP ( ) :
ping = json . loads ( requests . post ( apiConfig [ " endpoint " ] + ' /ping/ ' , data = json . dumps ( apiConfig ) ) . text )
return ( ping [ " yourIp " ] )
def deleteRecord ( ) :
for i in getRecords ( rootDomain ) [ " records " ] :
2021-09-22 23:33:58 +00:00
if i [ " name " ] == fqdn and ( i [ " type " ] == ' A ' or i [ " type " ] == ' ALIAS ' or i [ " type " ] == ' CNAME ' ) :
2021-05-28 01:59:11 +00:00
print ( " Deleting existing " + i [ " type " ] + " Record " )
deleteRecord = json . loads ( requests . post ( apiConfig [ " endpoint " ] + ' /dns/delete/ ' + rootDomain + ' / ' + i [ " id " ] , data = json . dumps ( apiConfig ) ) . text )
def createRecord ( ) :
createObj = apiConfig . copy ( )
2021-09-22 23:33:58 +00:00
createObj . update ( { ' name ' : subDomain , ' type ' : ' A ' , ' content ' : myIP , ' ttl ' : 300 } )
2021-05-28 01:59:11 +00:00
endpoint = apiConfig [ " endpoint " ] + ' /dns/create/ ' + rootDomain
print ( " Creating record: " + fqdn + " with answer of " + myIP )
create = json . loads ( requests . post ( apiConfig [ " endpoint " ] + ' /dns/create/ ' + rootDomain , data = json . dumps ( createObj ) ) . text )
return ( create )
if len ( sys . argv ) > 2 : #at least the config and root domain is specified
apiConfig = json . load ( open ( sys . argv [ 1 ] ) ) #load the config file into a variable
2022-06-17 23:13:40 +00:00
rootDomain = sys . argv [ 2 ] . lower ( )
2021-05-28 01:59:11 +00:00
if len ( sys . argv ) > 3 and sys . argv [ 3 ] != ' -i ' : #check if a subdomain was specified as the third argument
2022-06-17 23:13:40 +00:00
subDomain = sys . argv [ 3 ] . lower ( )
2021-05-28 01:59:11 +00:00
fqdn = subDomain + " . " + rootDomain
else :
subDomain = ' '
fqdn = rootDomain
if len ( sys . argv ) > 4 and sys . argv [ 3 ] == ' -i ' : #check if IP is manually specified. There's probably a more-elegant way to do this
myIP = sys . argv [ 4 ]
elif len ( sys . argv ) > 5 and sys . argv [ 4 ] == ' -i ' :
myIP = sys . argv [ 5 ]
else :
myIP = getMyIP ( ) #otherwise use the detected exterior IP address
2021-09-22 23:33:58 +00:00
deleteRecord ( )
print ( createRecord ( ) [ " status " ] )
2021-05-28 01:59:11 +00:00
else :
print ( " Porkbun Dynamic DNS client, Python Edition \n \n Error: not enough arguments. Examples: \n python porkbun-ddns.py /path/to/config.json example.com \n python porkbun-ddns.py /path/to/config.json example.com www \n python porkbun-ddns.py /path/to/config.json example.com ' * ' \n python porkbun-ddns.py /path/to/config.json example.com -i 10.0.0.1 \n " )