{"id":376,"date":"2025-11-17T19:01:28","date_gmt":"2025-11-17T19:01:28","guid":{"rendered":"https:\/\/sites.wp.odu.edu\/isaac-huston\/?page_id=376"},"modified":"2025-11-17T19:59:10","modified_gmt":"2025-11-17T19:59:10","slug":"python-ping-sweep-script","status":"publish","type":"page","link":"https:\/\/sites.wp.odu.edu\/isaac-huston\/skills\/scripting-and-automation\/python-ping-sweep-script\/","title":{"rendered":"Python Ping Sweep Script"},"content":{"rendered":"\n<p>Cross-platform network probe with structured output. Created for reusable and efficient network scanning.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python3\nimport subprocess\nimport ipaddress\nimport netifaces\nimport sys\nfrom multiprocessing import Pool\n\ndef ping_sweep(ip):\n    \"\"\"Perform a single ping and print IP if host responds.\"\"\"\n    result = subprocess.run(\n        &#091;\"ping\", \"-c\", \"1\", \"-W\", \"1\", str(ip)],\n        stdout=subprocess.DEVNULL,\n        stderr=subprocess.DEVNULL,\n    )\n    if result.returncode == 0:\n        print(ip)\n\ndef get_primary_ipv4_network():\n    \"\"\"Return (ip_address, IPv4Network) for the first non-loopback interface.\"\"\"\n    for interface in netifaces.interfaces():\n        addresses = netifaces.ifaddresses(interface).get(netifaces.AF_INET)\n        if not addresses:\n            continue\n        for address in addresses:\n            addr = address.get(\"addr\")\n            netmask = address.get(\"netmask\")\n            if not addr or not netmask:\n                continue\n            if ipaddress.ip_address(addr).is_loopback:\n                continue\n            # Build network using address and netmask\n            network = ipaddress.IPv4Network(f\"{addr}\/{netmask}\", strict=False)\n            return addr, network\n    return None, None\n\nif __name__ == \"__main__\":\n    ip_address, network = get_primary_ipv4_network()\n\n    if not ip_address or not network:\n        print(\"Failed to retrieve IP address or network. Please check your network connection.\")\n        sys.exit(1)\n\n    print(\"Host IP address:\", ip_address)\n    print(\"Network prefix:\", network.network_address)\n    print(\"CIDR network:\", network)\n\n    # Build list of host IPs in this network (skips network and broadcast)\n    ip_addresses = &#091;str(ip) for ip in network.hosts()]\n\n    # Create a pool of worker processes\n    pool = Pool()\n\n    # Perform ping sweep asynchronously\n    for ip in ip_addresses:\n        pool.apply_async(ping_sweep, (ip,))\n\n    # Close the pool to prevent further tasks from being submitted\n    pool.close()\n\n    # Wait for the worker processes to complete\n    pool.join()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Cross-platform network probe with structured output. Created for reusable and efficient network scanning.<\/p>\n","protected":false},"author":30346,"featured_media":0,"parent":326,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/sites.wp.odu.edu\/isaac-huston\/wp-json\/wp\/v2\/pages\/376"}],"collection":[{"href":"https:\/\/sites.wp.odu.edu\/isaac-huston\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sites.wp.odu.edu\/isaac-huston\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sites.wp.odu.edu\/isaac-huston\/wp-json\/wp\/v2\/users\/30346"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.wp.odu.edu\/isaac-huston\/wp-json\/wp\/v2\/comments?post=376"}],"version-history":[{"count":1,"href":"https:\/\/sites.wp.odu.edu\/isaac-huston\/wp-json\/wp\/v2\/pages\/376\/revisions"}],"predecessor-version":[{"id":377,"href":"https:\/\/sites.wp.odu.edu\/isaac-huston\/wp-json\/wp\/v2\/pages\/376\/revisions\/377"}],"up":[{"embeddable":true,"href":"https:\/\/sites.wp.odu.edu\/isaac-huston\/wp-json\/wp\/v2\/pages\/326"}],"wp:attachment":[{"href":"https:\/\/sites.wp.odu.edu\/isaac-huston\/wp-json\/wp\/v2\/media?parent=376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}