Skip to content

Instantly share code, notes, and snippets.

@kingluo
Last active September 7, 2022 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingluo/8944c1435c3c93fd7bccaca3f4f810ba to your computer and use it in GitHub Desktop.
Save kingluo/8944c1435c3c93fd7bccaca3f4f810ba to your computer and use it in GitHub Desktop.
ipset_name="myset"
hosts=("httpbin.org")
interval=3 #secs
while true; do
new_iplist=()
for host in ${hosts[@]}; do
for ip in $(dig +noall +answer +multiline $host | awk '{print $NF}' | sort); do
new_iplist+=($ip)
done
echo "check $host: ${new_iplist[@]}"
done
iplist=$(ipset list $ipset_name | awk 'BEGIN{flag=0}{if(flag==1) print $0; if ($0 ~ /^Members:/) { flag=1 }}')
# remove non-exists ip addresses
for ipp in ${iplist[@]}; do
exists=0
for ip in ${new_iplist[@]}; do
if [[ $ipp == $ip ]]; then
exists=1
break
fi
done
if [[ $exists == 0 ]]; then
echo "del $ip"
ipset del $ipset_name $ip
fi
done
# add new ip addresses
for ip in ${new_iplist[@]}; do
exists=0
for ipp in ${iplist[@]}; do
if [[ $ipp == $ip ]]; then
exists=1
break
fi
done
if [[ $exists == 0 ]]; then
echo "add $ip"
ipset add $ipset_name $ip
fi
done
sleep $interval
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment