Last active
May 13, 2022 07:46
-
-
Save Chaz6/51489bbd2b3ae78ea4e06c8ef100042a to your computer and use it in GitHub Desktop.
Generate an ipset for a given autonomous system number
This file contains hidden or 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
#!/bin/bash | |
usage() | |
{ | |
echo "Usage: $0 [ASN]" | |
} | |
if [ $# -ne 1 ]; then | |
usage | |
exit 1 | |
fi | |
ASN="${1}" | |
input=$(curl -s 'https://asn.ipinfo.app/api/text/list/AS'"${ASN}") | |
linecount=$(wc -l <<< "${input}") | |
ipv4count=$(grep -Eco "([0-9.]+){4}/[0-9]{1,2}" <<< "${input}") | |
ipv6count=$(grep -Eco "([0-9a-f]+:){1,7}:/[0-9]{1,3}" <<< "${input}") | |
flag=0 | |
if [ ${ipv4count} -gt 0 ]; then | |
flag=1 | |
echo '/usr/sbin/ipset -N asn_'"${ASN}"'_v4 hash:net family inet' | |
while read -r line; do | |
echo '/usr/sbin/ipset -A asn_'"${ASN}"'_v4 '"${line}" | |
done <<< "$(grep -E "([0-9.]+){4}/[0-9]{1,2}" <<< "${input}")" | |
fi | |
if [ ${ipv6count} -gt 0 ]; then | |
flag=1 | |
echo '/usr/sbin/ipset -N asn_'"${ASN}"'_v6 hash:net family inet6' | |
while read -r line; do | |
echo '/usr/sbin/ipset -A asn_'"${ASN}"'_v6 '"${line}" | |
done <<< "$(grep -E "([0-9a-f]+:){1,7}:/[0-9]{1,3}" <<< "${input}")" | |
fi | |
if [ ${flag} -eq 1 ]; then | |
echo '/usr/sbin/ipset -N asn_'"${ASN}"' list:set' | |
if [ ${ipv4count} -gt 0 ]; then | |
echo '/usr/sbin/ipset -A asn_'"${ASN}"' asn_'"${ASN}"'_v4' | |
fi | |
if [ ${ipv6count} -gt 0 ]; then | |
echo '/usr/sbin/ipset -A asn_'"${ASN}"' asn_'"${ASN}"'_v6' | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment