Skip to content

Instantly share code, notes, and snippets.

@jonathanhle
Created May 16, 2023 18:27
Show Gist options
  • Save jonathanhle/473341571d85c9073c3a09134dae11ec to your computer and use it in GitHub Desktop.
Save jonathanhle/473341571d85c9073c3a09134dae11ec to your computer and use it in GitHub Desktop.
nextdns spec code
# This is some horrible stuff, but just quickly jotting stuff down to show it's possible to automate the nextdns API with LinkedIP setup, including profile creation
import requests
from nextdnsapi.api import account, settings
from shell import shell
from ifconfigparser import IfconfigParser
header = account.login("[email protected]", "some-pass-word")
def create_profile(header, profile_name):
data = {"name": f"{profile_name}"}
put = requests.post(f"https://api.nextdns.io/profiles",
headers=header, json=data)
return put.text
print(create_profile(header, "jle-m01"))
config_id = None
for profile in account.list(header):
if "jle-m01" in profile.values():
config_id = profile["id"]
def setup(config, header):
setup = requests.get(f"https://api.nextdns.io/profiles/{config}/setup", headers=header)
print(setup.text)
if setup.text.__contains__("notFound"):
raise ConfigNotFound(config)
else:
setup = setup.json()
return setup
# update_token = setup(config_id, header)["data"]["linkedIp"]["updateToken"]
dns_servers = (setup(config_id, header)["data"]["linkedIp"]["servers"])
dns_servers = " ".join(dns_servers)
print(dns_servers)
def updatelinkedip(config, header):
r = setup(config, header)
updatetoken = r["data"]["linkedIp"]["updateToken"]
updateip = requests.get(f"https://link-ip.nextdns.io/{config}/{updatetoken}")
print(updateip.text)
return updateip.text
updatelinkedip(config_id, header)
# -----------------------------------------------------------
# Set DNS Servers
# -----------------------------------------------------------
InstalledHardware = shell("networksetup -listallhardwareports")
ActiveInterfaces = shell("ifconfig")
hello = InstalledHardware.output()
def create_dicts_devices(input_list):
# Initialize the list of dictionaries
dicts = []
# Iterate over the input_list in steps of 3
for i in range(0, len(input_list), 3):
# Check if the current index is not beyond the end of the list
if i+2 < len(input_list):
# Create a new dictionary and add it to the list
dicts.append({
"Hardware Port": input_list[i].split(": ", 1)[-1],
"Device": input_list[i+1].split(": ", 1)[-1],
"Ethernet Address": input_list[i+2].split(": ", 1)[-1]
})
return dicts
hardware_named_ports = create_dicts_devices(hello)
interfaces = IfconfigParser(ActiveInterfaces.output())
interfaces_dict = dict(interfaces.get_interfaces())
live_devices = []
for en in interfaces_dict.values():
if en.ipv4_addr and en.ipv4_addr != "127.0.0.1":
live_devices.append(en.name)
for name in live_devices:
for named_port in hardware_named_ports:
if named_port["Device"] == name:
# set the dns_servers on the live devices
status = shell(f'networksetup -setdnsservers "{named_port["Hardware Port"]}" {dns_servers}')
print(status.code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment