Skip to content

Instantly share code, notes, and snippets.

@jgomo3
Created January 3, 2020 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgomo3/cecc3a513bdce6b040bec91c5ee9185e to your computer and use it in GitHub Desktop.
Save jgomo3/cecc3a513bdce6b040bec91c5ee9185e to your computer and use it in GitHub Desktop.
Defines the ip_in_sn function to tell if an IP as string is in a Subnet as string
import ipaddress
def ip_in_sn(ip_str, sn_str):
ip = ipaddress.ip_address(ip_str)
sn = ipaddress.ip_network(sn_str)
return ip in sn
if __name__ == '__main__':
ip = '192.0.2.1'
sn = '192.0.2.0/24'
is_it = {True: 'is', False: "isn't"}.get
print(f'{ip} {is_it(ip_in_sn(ip, sn))} in {sn}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment