Skip to content

Instantly share code, notes, and snippets.

@KoffeinKaio
Created November 28, 2018 20:09
Show Gist options
  • Save KoffeinKaio/038a4f1bf8f1a6d1e6b00a05be934897 to your computer and use it in GitHub Desktop.
Save KoffeinKaio/038a4f1bf8f1a6d1e6b00a05be934897 to your computer and use it in GitHub Desktop.
sync fb host entrys to piholes list
#!/bin/bash
FB_IP=192.168.100.1
PIHOLE_LIST=/opt/pihole/etc/fb-sync.list
#0 if pihole is standalone, 1if its running in docker
DOCKER_ACTIVE=1
DOCKER_IP=192.168.100.10
DOCKER_PORT=2376
DOCKER_PIHOLE_NAME='pi-hole'
numHostsXML=$(curl -s -d @- -H "Content-Type: text/xml" -H "SOAPAction: urn:dslforum-org:service:Hosts:1#GetHostNumberOfEntries" http://$FB_IP:49000/upnp/control/hosts <<EOF
<Envelope>
<Body>
<GetHostNumberOfEntries>
<NewHostNumberOfEntries>0</NewHostNumberOfEntries>
</GetHostNumberOfEntries>
</Body>
</Envelope>
EOF
)
numHosts=$(grep -oPm1 "(?<=<NewHostNumberOfEntries>)[^<]+" <<< "$numHostsXML")
numHosts=$((numHosts-1))
truncate --size 0 $PIHOLE_LIST
for i in `seq 0 $numHosts`;
do
hostXML=$(curl -s -d @- -H "Content-Type: text/xml" -H "SOAPAction: urn:dslforum-org:service:Hosts:1#GetGenericHostEntry" http://$FB_IP:49000/upnp/control/hosts <<EOF
<Envelope>
<Body>
<GetGenericHostEntry>
<NewIndex>$i</NewIndex>
</GetGenericHostEntry>
</Body>
</Envelope>
EOF
)
ip=$(grep -oPm1 "(?<=<NewIPAddress>)[^<]+" <<< "$hostXML")
hostname=$(grep -oPm1 "(?<=<NewHostName>)[^<]+" <<< "$hostXML")
isActive=$(grep -oPm1 "(?<=<NewActive>)[^<]+" <<< "$hostXML")
if [[ -z $ip ]]; then
continue
fi
echo "$ip $hostname" >> $PIHOLE_LIST
#echo "IP: $ip Hostname: $hostname Active: $isActive"
done
if [ $DOCKER_ACTIVE -eq 1 ]; then
CONTAINER_ID=`curl --silent -G -XGET "http://$DOCKER_IP:$DOCKER_PORT/v1.37/containers/json" -d 'all=1' --data-urlencode 'filters={"name":["$DOCKER_PIHOLE_NAME"]}' | jq .[].Id | sed 's/"//g'`
id=`curl --silent "http://$DOCKER_IP:$DOCKER_PORT/v1.37/containers/$CONTAINER_ID/exec" -XPOST -H "Content-Type: application/json" -d '{
"AttachStdout": true,
"Tty": true,
"Cmd": [ "pihole", "-g"]
}' | jq .Id | sed 's/"//g'`
curl --silent "http://$DOCKER_IP:$DOCKER_PORT/v1.37/exec/$id/start" -XPOST \
-H "Content-Type: application/json" \
-d '{
"Detach": false,
"Tty": true
}' > /dev/nul
id=`curl --silent "http://192.168.100.10:2376/v1.37/containers/$CONTAINER_ID/exec" -XPOST -H "Content-Type: application/json" -d '{
"AttachStdout": true,
"Tty": true,
"Cmd": [ "pihole", "restartdns"]
}' | jq .Id | sed 's/"//g'`
curl --silent "http://$DOCKER_IP:$DOCKER_PORT/v1.37/exec/$id/start" -XPOST \
-H "Content-Type: application/json" \
-d '{
"Detach": false,
"Tty": true
}' > /dev/nul
else
pihole restartdns
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment