Skip to content

Instantly share code, notes, and snippets.

@mrcrilly
Last active January 4, 2016 13:49
Show Gist options
  • Save mrcrilly/8630288 to your computer and use it in GitHub Desktop.
Save mrcrilly/8630288 to your computer and use it in GitHub Desktop.
Banned IPs
import sys
from ipaddress import IPv4Network, IPv4Address
def error_banned(ip_addr):
print "That's a banned IP: {}".format(str(ip_addr))
def error_private(ip_addr):
print "That IP is in a private range: {}".format(str(ip_addr))
def main():
banned = ['192.168.0.0/255.255.0.0', '10.0.0.0/255.0.0.0']
ip = IPv4Address(sys.argv[1].decode())
if ip.is_private or ip.is_link_local:
error_banned(ip)
for network in banned:
if ip in IPv4Network(network.decode()):
error_private(ip)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment