Skip to content

Instantly share code, notes, and snippets.

@kayrus
Last active March 4, 2016 14:13
Show Gist options
  • Save kayrus/4cb418bac54d1b63e653 to your computer and use it in GitHub Desktop.
Save kayrus/4cb418bac54d1b63e653 to your computer and use it in GitHub Desktop.
Configure dnsmasq nameservers through DBUS

Run example:

./dnsmasq.sh 8.8.8.8

Each time you run this script it adds only one DNS server.

dnsmasq.sh:

#!/bin/bash

IP_INT=0

if [[ $1 =~ ^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$ ]]; then
  for OCTET in {1..4}; do
    IP_INT=$(($IP_INT+${BASH_REMATCH[($OCTET-4-1)*-1]}*256**($OCTET-1)))
  done
else
  echo "Invalid IP address"
  exit 1
fi

DOMAIN=""
if [ -n "$2" ]; then
  DOMAIN="string:'$2'"
fi

sudo dbus-send --system --dest=org.freedesktop.NetworkManager.dnsmasq /uk/org/thekelleys/dnsmasq uk.org.thekelleys.SetServers uint32:$IP_INT $DOMAIN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment