Skip to content

Instantly share code, notes, and snippets.

@mooyoul
Last active July 6, 2017 11:42
Show Gist options
  • Save mooyoul/6d45dc5194c38cd07bde6f5e3b498fb8 to your computer and use it in GitHub Desktop.
Save mooyoul/6d45dc5194c38cd07bde6f5e3b498fb8 to your computer and use it in GitHub Desktop.
Gets CIDR by query whois with specific netname (e.g. Name of ISP)
#!/bin/bash
# First of all, you'll need ipcalc utility.
# ipcalc can be downloaded at jodies.de/ipcalc
WHOIS_QUERY = "some-netname"
whois -h whois.apnic.net $WHOIS_QUERY | grep inetnum | grep -Eo '[0-9].+' | sed 's/ //g' | xargs -I IP_RANGE sh -c 'ipcalc -rn IP_RANGE | tail -n +2'
# ######################
# Explaination
# ######################
#
# Query to whois server, you can change whois host to another whois server.
# > whois -h whois.apnic.net "LXN"
# Grap inetnum line, there are ip adddress like 10.0.0.0 - 10.0.0.255.
# > grep inetnum
# Matches IP Address range only
# > grep -Eo '[0-9].+'
# Remove spaces
# > sed 's/ //g'
# Finally, convert target ip range to cidr
# > xargs -I IP_RANGE sh -c 'ipcalc -rn IP_RANGE | tail -n +2'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment