Skip to content

Instantly share code, notes, and snippets.

@brootware
Last active November 15, 2022 22:23
Show Gist options
  • Save brootware/e92597b43247b3b784ef5588265b0ae9 to your computer and use it in GitHub Desktop.
Save brootware/e92597b43247b3b784ef5588265b0ae9 to your computer and use it in GitHub Desktop.
import socket
import sys
import ipaddress
import subprocess
banner = r"""
__ __.__ _________ .__ .___
/ \ / \__| ____ \_ ___ \|__| __| _/______
\ \/\/ / |/ \ / \ \/| |/ __ |\_ __ \
\ /| | | \ \ \___| / /_/ | | | \/
\__/\ / |__|___| / \______ /__\____ | |__|
\/ \/ \/ \/
+-+-+-+-+-+-+-+ +-+-+ +-+-+-+-+-+-+-+-+-+
|P|o|w|e|r|e|d| |b|y| |B|r|o|o|t|w|a|r|e|
+-+-+-+-+-+-+-+ +-+-+ +-+-+-+-+-+-+-+-+-+
https://github.com/brootware
https://brootware.github.io
"""
help_menu = """
getcidr - A python script to calculate which cidr subnet range you are on by supplying subnet mask.
Example usage:\n
python getcidr.py 255.255.248.0
"""
def get_current_ip():
return socket.gethostbyname(socket.gethostname())
def calculate_cidr(netmask: str) -> ipaddress.IPv4Network:
current_ipaddr = get_current_ip()
cidr = ipaddress.ip_network(f'{current_ipaddr}/{netmask}', strict=False)
return cidr
def get_broadcast(cidr: ipaddress.IPv4Network) -> str:
ipi = ipaddress.ip_interface(cidr)
return ipi.network.broadcast_address
def main():
print(banner)
if len(sys.argv) == 1:
print(help_menu)
sys.exit("[-] Supply your subnet mask. Check from ipconfig /all or ifconfig")
subnet_mask = sys.argv[1]
cidr = calculate_cidr(subnet_mask)
broadcast = get_broadcast(cidr)
print(f"[+] CIDR address : {cidr}")
print(f"[+] Broadcast address : {broadcast}")
if __name__ == "__main__":
main()
import subprocess
def run(cmd):
completed = subprocess.run(["powershell", "-Command", cmd], capture_output=True, text=True)
return completed
if __name__ == '__main__':
output = run('ipconfig /all')
print(output.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment