Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Looking for a Shell Script to Modify DNS Records

Hi! I’m looking for a shell script that will allow me to edit DNS records. If anyone has something available or can point me in the right direction, I’d appreciate it.

Thanks!
Here's an example using dnsmasq.

Code
#!/bin/bash
[h1]dnsmasq-records.sh[/h1]
[h1]Script to add or update custom DNS records in dnsmasq[/h1]

[h1]CONFIGURATION[/h1]
DNSMASQ_DIR="/etc/dnsmasq.d"
CUSTOM_CONF="$DNSMASQ_DIR/custom-records.conf"
SERVICE_NAME="dnsmasq"

[h1]Ensure running as root[/h1]
if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root." >&2
    exit 1
fi

[h1]Validate arguments[/h1]
if [[ $# -ne 2 ]]; then
    echo "Usage: $0 <hostname> <ip-address>"
    echo "Example: $0 myapp.local 192.168.1.50"
    exit 1
fi

HOSTNAME="$1"
IP="$2"

[h1]Validate IP format[/h1]
if ! [[ $IP =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
    echo "Invalid IP address format: $IP" >&2
    exit 1
fi

[h1]Create dnsmasq.d directory if missing[/h1]
mkdir -p "$DNSMASQ_DIR"

[h1]Remove any existing entry for the hostname[/h1]
grep -v "address=/$HOSTNAME/" "$CUSTOM_CONF" 2>/dev/null > "$CUSTOM_CONF.tmp" || true

[h1]Add the new record[/h1]
echo "address=/$HOSTNAME/$IP" >> "$CUSTOM_CONF.tmp"

[h1]Replace the old config[/h1]
mv "$CUSTOM_CONF.tmp" "$CUSTOM_CONF"

[h1]Reload dnsmasq to apply changes[/h1]
if command -v systemctl >/dev/null; then
    systemctl reload "$SERVICE_NAME"
else
    service "$SERVICE_NAME" reload
fi

echo "DNS record updated: $HOSTNAME → $IP"

Run it with 'sudo ./dnsmasq-records.sh myapp.local 192.168.1.50'.


Do be aware if you're spoofing DNS records of TLS and HSTS.
(Dec 30, 2025 09:51 AM)DramaKing Wrote:  Here's an example using dnsmasq.

Code
#!/bin/bash
[h1]dnsmasq-records.sh[/h1]
[h1]Script to add or update custom DNS records in dnsmasq[/h1]

[h1]CONFIGURATION[/h1]
DNSMASQ_DIR="/etc/dnsmasq.d"
CUSTOM_CONF="$DNSMASQ_DIR/custom-records.conf"
SERVICE_NAME="dnsmasq"

[h1]Ensure running as root[/h1]
if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root." >&2
    exit 1
fi

[h1]Validate arguments[/h1]
if [[ $# -ne 2 ]]; then
    echo "Usage: $0 <hostname> <ip-address>"
    echo "Example: $0 myapp.local 192.168.1.50"
    exit 1
fi

HOSTNAME="$1"
IP="$2"

[h1]Validate IP format[/h1]
if ! [[ $IP =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
    echo "Invalid IP address format: $IP" >&2
    exit 1
fi

[h1]Create dnsmasq.d directory if missing[/h1]
mkdir -p "$DNSMASQ_DIR"

[h1]Remove any existing entry for the hostname[/h1]
grep -v "address=/$HOSTNAME/" "$CUSTOM_CONF" 2>/dev/null > "$CUSTOM_CONF.tmp" || true

[h1]Add the new record[/h1]
echo "address=/$HOSTNAME/$IP" >> "$CUSTOM_CONF.tmp"

[h1]Replace the old config[/h1]
mv "$CUSTOM_CONF.tmp" "$CUSTOM_CONF"

[h1]Reload dnsmasq to apply changes[/h1]
if command -v systemctl >/dev/null; then
    systemctl reload "$SERVICE_NAME"
else
    service "$SERVICE_NAME" reload
fi

echo "DNS record updated: $HOSTNAME → $IP"

Run it with 'sudo ./dnsmasq-records.sh myapp.local 192.168.1.50'.


Do be aware if you're spoofing DNS records of TLS and HSTS.

Hi mate, thank you very much — really appreciate it! I’ll give it a try


Possibly Related Threads...
Thread / Author Replies Views Last Post
by TrilliumSep 12, 2026 06:26 AM 10 355 Sep 12, 2026 06:26 AM
Last Post: Trillium
by SIM-5525056May 27, 2024 11:00 AM 0 189 May 27, 2024 11:00 AM
Last Post: SIM-5525056
by hackediaFeb 4, 2018 01:55 PM 5 460 Feb 4, 2018 01:55 PM
Last Post: CyberStrike