If you use your hosting company or datacenter provided DNS servers you have probably experienced your fair share of blips and outages of these services. Although some companies are quite good, some can be quite bad.
Lots of programs seem to behave strangely when DNS servers listed in resolv.conf stop working. For this reason I present a simple script to keep your resolv.conf filled with only valid name servers. Run it on a cron job (at an acceptable interval) to achieve the required effect.
#!/bin/bash
NAMESERVERS="--NAMESERVERS HERE--"
NMAP_V5=$(nmap -V | grep 5.00 | wc -l)
NMAP_V6=$(nmap -V | grep 5.00 | wc -l)
if [ "$NMAP_V5" -eq "1" ]
then
echo "nmap version 5"
IPS_VALID=$(nmap -sP $NAMESERVERS | grep "is up" | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
elif [ "$NMAP_V6" -eq "1" ]
then
echo "nmap version 6"
nmap -n -sn -sP -oG /tmp/resolv.nmap $NAMESERVERS
IPS_VALID=$(cat /tmp/resolv.nmap | grep "Status: Up" | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
else
echo "Unknown nmap version"
exit
fi
IPS_COUNT=$(echo "$IPS_VALID" | grep -v "^$" | wc -l)
if [ "$IPS_COUNT" -eq 0 ]
then
echo "Unable to find online DNS servers"
else
echo "$IPS_COUNT DNS Servers found, updating resolv.conf"
echo "$IPS_VALID" | sed -e 's/^/nameserver /' > /etc/resolv.conf
fi
Requires nmap (5.0 or 6.0)
Quite a simple script but essential to those running services such as rsyslog or zabbix (both of which do not nicely handle failing DNS services where they are used for resolving upstream servers).
Enjoy.
Lots of programs seem to behave strangely when DNS servers listed in resolv.conf stop working. For this reason I present a simple script to keep your resolv.conf filled with only valid name servers. Run it on a cron job (at an acceptable interval) to achieve the required effect.
#!/bin/bash
NAMESERVERS="--NAMESERVERS HERE--"
NMAP_V5=$(nmap -V | grep 5.00 | wc -l)
NMAP_V6=$(nmap -V | grep 5.00 | wc -l)
if [ "$NMAP_V5" -eq "1" ]
then
echo "nmap version 5"
IPS_VALID=$(nmap -sP $NAMESERVERS | grep "is up" | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
elif [ "$NMAP_V6" -eq "1" ]
then
echo "nmap version 6"
nmap -n -sn -sP -oG /tmp/resolv.nmap $NAMESERVERS
IPS_VALID=$(cat /tmp/resolv.nmap | grep "Status: Up" | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
else
echo "Unknown nmap version"
exit
fi
IPS_COUNT=$(echo "$IPS_VALID" | grep -v "^$" | wc -l)
if [ "$IPS_COUNT" -eq 0 ]
then
echo "Unable to find online DNS servers"
else
echo "$IPS_COUNT DNS Servers found, updating resolv.conf"
echo "$IPS_VALID" | sed -e 's/^/nameserver /' > /etc/resolv.conf
fi
Requires nmap (5.0 or 6.0)
Quite a simple script but essential to those running services such as rsyslog or zabbix (both of which do not nicely handle failing DNS services where they are used for resolving upstream servers).
Enjoy.