Script detecting and resolving ngrok hostnames for updating noip A and srv dns records

56 views Asked by At

Hello I have tried to generate code with ChatGPT to make a script which get ngrok hostname and resolve it to ip and add dns A record and SRV record for noip when it run , but it only can add dns A record it can not add SRV record . Could you help me debug the code and give me the correct script . Heres is the chatgpt generated python code :

import requests
import socket
import subprocess

# Ngrok API URL
ngrok_api_url = "http://localhost:4040/api/tunnels"

# Get Ngrok information
try:
    response = requests.get(ngrok_api_url)
    response.raise_for_status()
    ngrok_data = response.json()
    print(response.text)  # Add this line to print the raw response from Ngrok
except requests.exceptions.RequestException as e:
    print(f"Error getting Ngrok information: {e}")
    exit()

# Extract public URL from Ngrok data
try:
    public_url = ngrok_data["tunnels"][0]["public_url"]
    ngrok_hostname = public_url.split(":")[1][2:].split('/')[0]  # Remove unwanted characters
    ngrok_port = public_url.split(":")[2]
except (KeyError, IndexError) as e:
    print(f"Error extracting Ngrok hostname and port: {e}")
    exit()

# Resolve Ngrok hostname to IP address
try:
    ngrok_ip = socket.gethostbyname(ngrok_hostname)
except socket.gaierror as e:
    print(f"Error resolving Ngrok hostname: {e}")
    exit()

# Update No-IP DNS A record
try:
    noip_hostname = ""  # Replace with your No-IP hostname
    noip_username = ""  # Replace with your No-IP username
    noip_password = ""  # Replace with your No-IP password

    # Update A record
    update_url_a = f"https://dynupdate.no-ip.com/nic/update?hostname={noip_hostname}&myip={ngrok_ip}"
    response_a = requests.get(update_url_a, auth=(noip_username, noip_password))
    response_a.raise_for_status()
    print(f"A record updated successfully.")

    # Update SRV record using subprocess
    subprocess.run(["C:\\Program Files (x86)\\No-IP\\DUC40.exe", "-U", noip_username, "-P", noip_password, "-d", noip_hostname, "SRV", ngrok_port])
    print(f"SRV record updated successfully.")
except requests.exceptions.RequestException as e:
    print(f"Error updating No-IP DNS records: {e}")
    exit()
except subprocess.CalledProcessError as e:
    print(f"Error updating SRV record: {e}")
    exit()

print(f"Ngrok IP: {ngrok_ip}")
print(f"Ngrok Port: {ngrok_port}")

I would like a script which when I run get ngrock hostname resolve it to ip after add that ip to my DNS A record and add same ip and port to DNS SRV record

0

There are 0 answers