Skip to content

Instantly share code, notes, and snippets.

@Chaz6
Last active May 13, 2022 07:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chaz6/51489bbd2b3ae78ea4e06c8ef100042a to your computer and use it in GitHub Desktop.
Save Chaz6/51489bbd2b3ae78ea4e06c8ef100042a to your computer and use it in GitHub Desktop.
Generate an ipset for a given autonomous system number
#!/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