Last active
September 7, 2022 07:54
-
-
Save kingluo/8944c1435c3c93fd7bccaca3f4f810ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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