I have got a list of public dns (in a file called dns.test).It looks like some of its elements do not resolve. I need to eliminate them. Here is the file:
199.255.137.34
103.112.162.165
103.133.222.202
82.146.26.2
94.236.218.254
185.81.41.81
103.209.52.250
119.160.80.164
8.8.8.8
8.8.4.4
151.80.222.79
note: there is no blanks, trailing or heading spaces.
I made a snippet to check them one by one and remove the faulty ones:
while IFS='\n' read -r LINE;do dig short timeout=1 serverfault.com @${LINE} &>/dev/null;[[ $? -eq 0 ]] && echo "${LINE} -> OK" || { echo "${LINE} -> NOK";sed -i "s/${LINE}//g" dns.test; };done< <(cat dns.test)
I shorten the timeout otherwise it's the hell slow!
but the sed part does not work; I base the sed part of the snippet on the following test:
LINE=199.255.137.34;sed -i "s/${LINE}//g" dns.test #-> this works manually but not when included in the script.
thankx folks!!
CodePudding user response:
sed
is superfluous for this task. Simply print out the addresses of the working dns servers and replace the content of the original file with the output. You can try this:
while read -r ipaddr; do
dig short timeout=1 serverfault.com @"$ipaddr" &>/dev/null && echo "$ipaddr"
done < dns.test > working-dns.test
and mv working-dns.test dns.test
if the contents of the working-dns.test
look fine.
CodePudding user response:
The following code should work, and please notice the usage of char '<' and '>' .
LINE="199\.255\.137\.34";sed -i "/\<$LINE\>/d" dns.test