{"id":372,"date":"2025-11-17T17:49:22","date_gmt":"2025-11-17T17:49:22","guid":{"rendered":"https:\/\/sites.wp.odu.edu\/isaac-huston\/?page_id=372"},"modified":"2025-11-17T19:59:37","modified_gmt":"2025-11-17T19:59:37","slug":"port-sweep-with-service-mapping-script","status":"publish","type":"page","link":"https:\/\/sites.wp.odu.edu\/isaac-huston\/skills\/scripting-and-automation\/port-sweep-with-service-mapping-script\/","title":{"rendered":"Python Port Sweep with Service Mapping Script"},"content":{"rendered":"\n<p>Python automation that enumerates ports and identifies running services for faster triage and analysis.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys\nimport socket\nimport multiprocessing\n\n# Function to scan ports\ndef scan_port(target, port):\n    try:\n        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n        s.settimeout(1)\n        result = s.connect_ex((target, port))\n        if result == 0:\n            try:\n                service = socket.getservbyport(port)\n            except:\n                service = \"unknown\"\n            print(\"Port {} ({}) is open\".format(port, service))\n        s.close()\n\n    except KeyboardInterrupt:\n        print(\"\\nExiting\")\n        sys.exit()\n\n    except socket.gaierror:\n        print(\"\\nHostname could not be resolved\")\n        sys.exit()\n\n    except socket.error:\n        print(\"\\nServer not responding\")\n        sys.exit()\n\n\nif __name__ == \"__main__\":\n    if len(sys.argv) == 2:\n        target = socket.gethostbyname(sys.argv&#091;1])\n    else:\n        target = input(\"Enter target IP address or hostname: \")\n\n    # Create multiprocessing pool\n    pool = multiprocessing.Pool()\n\n    # Launch scans\n    results = &#091;]\n    for port in range(1, 65536):\n        results.append(pool.apply_async(scan_port, args=(target, port)))\n\n    # Close pool and wait for workers\n    pool.close()\n    pool.join()\n\n    # Retrieve results\n    for result in results:\n        result.get()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python automation that enumerates ports and identifies running services for faster triage and analysis.<\/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\/372"}],"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=372"}],"version-history":[{"count":2,"href":"https:\/\/sites.wp.odu.edu\/isaac-huston\/wp-json\/wp\/v2\/pages\/372\/revisions"}],"predecessor-version":[{"id":374,"href":"https:\/\/sites.wp.odu.edu\/isaac-huston\/wp-json\/wp\/v2\/pages\/372\/revisions\/374"}],"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=372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}