Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Last active October 18, 2021 02:51
Show Gist options
  • Save eusonlito/5afa0d42f1aff3cd2b82a8c0a8a3b75d to your computer and use it in GitHub Desktop.
Save eusonlito/5afa0d42f1aff3cd2b82a8c0a8a3b75d to your computer and use it in GitHub Desktop.
Script to execute as a cronjob and generate json files with the IPs added to ipset
#!/bin/bash
echo -ne "\nSTART: $(date "+%Y-%m-%d %H:%M:%S")"
logs="/root/logs/ipset-locate"
if [ ! -d "$logs" ]; then
install -d "$logs"
fi
for ip in $(/usr/sbin/ipset list | grep ^[0-9] | awk -F' ' '{print $1}'); do
log="$logs/$ip"
if [ -f "$log" ]; then
continue
fi
response=$(curl --silent --fail --connect-timeout 5 --max-time 5 http://ip-api.com/json/$ip 2> /dev/null)
if [ "$response" == "" ]; then
response=$(curl --silent --fail --connect-timeout 5 --max-time 5 https://ipapi.co/$ip/json/ 2> /dev/null)
fi
if [ "$response" != "" ]; then
echo "$response" > "$log"
fi
echo -en "\n\n$ip: "
if [ -f "$log" ]; then
echo -n "$response"
if [ -x /usr/bin/jq ]; then
/usr/bin/jq -s '.' "$log" > "$log.json"
fi
else
echo -n 'NOT FOUND'
fi
sleep 1
done
echo -e "\n\nEND: $(date "+%Y-%m-%d %H:%M:%S")\n"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment