Created
January 3, 2020 13:27
-
-
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
This file contains 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
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