{"id":1200,"date":"2023-09-11T22:40:24","date_gmt":"2023-09-11T13:40:24","guid":{"rendered":"https:\/\/tippang.net\/?p=1200"},"modified":"2023-09-11T22:40:27","modified_gmt":"2023-09-11T13:40:27","slug":"mastering-network-scanning-with-python","status":"publish","type":"post","link":"https:\/\/tippang.net\/?p=1200","title":{"rendered":"Mastering Network Scanning with Python"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-4.png\" alt=\"\" class=\"wp-image-1201\" srcset=\"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-4.png 1024w, https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-4-300x169.png 300w, https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-4-768x432.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Network scanning is the process of discovering devices on a network and identifying their open ports and services. It is an essential part of network security, allowing system administrators to identify potential vulnerabilities and attack vectors. In this article, we will explore network scanning techniques using Python.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Port Scanning<\/h2>\n\n\n\n<p>Port scanning is a technique used to identify open ports on a target machine. Python provides several libraries for implementing port scanning, including the socket and nmap libraries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using the socket Library<\/h3>\n\n\n\n<p>To perform a basic port scan using the socket library, you can use the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import socket\n\ntarget_host = \"192.168.1.1\"\ntarget_ports = [21, 22, 80, 443]\n\nfor port in target_ports:\n    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    s.settimeout(2)\n    result = s.connect_ex((target_host, port))\n    if result == 0:\n        print(\"Port {} is open\".format(port))\n    s.close()\n<\/code><\/pre>\n\n\n\n<p>This code creates a socket for each port in the target_ports list and attempts to connect to the target_host. If the connection is successful, the port is considered open.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using the nmap Library<\/h3>\n\n\n\n<p>The nmap library is a more advanced port scanning tool that provides additional functionality, such as OS detection and service identification. To use the nmap library, you can use the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import nmap\n\nnm = nmap.PortScanner()\n\ntarget_host = \"192.168.1.1\"\ntarget_ports = \"21,22,80,443\"\n\nnm.scan(hosts=target_host, arguments=\"-p \" + target_ports)\n\nfor host in nm.all_hosts():\n    print(\"Host : %s\" % host)\n    for port in nm[host]['tcp'].keys():\n        print(\"Port : %s\\tState : %s\" % (port, nm[host]['tcp'][port]['state']))\n<\/code><\/pre>\n\n\n\n<p>This code creates an instance of the nmap.PortScanner() class and uses the scan() method to perform a port scan on the target_host using the specified target_ports. The results are then printed to the console.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Network Discovery<\/h2>\n\n\n\n<p>Network discovery is the process of identifying devices on a network. Python provides several libraries for implementing network discovery, including the scapy and netifaces libraries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using the scapy Library<\/h3>\n\n\n\n<p>To perform network discovery using the scapy library, you can use the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>from scapy.all import ARP, Ether, srp\n\ntarget_ip = \"192.168.1.0\/24\"\n\n# Create ARP request\narp = ARP(pdst=target_ip)\n\n# Create Ethernet frame\nether = Ether(dst=\"ff:ff:ff:ff:ff:ff\")\n\n# Combine Ethernet and ARP requests\npacket = ether\/arp\n\n# Send packet and capture response\nresult = srp(packet, timeout=3, verbose=0)[0]\n\n# Extract results\ndevices = []\nfor sent, received in result:\n    devices.append({'ip': received.psrc, 'mac': received.hwsrc})\n\n# Print results\nfor device in devices:\n    print(\"IP Address: {}\\tMAC Address: {}\".format(device['ip'], device['mac']))\n<\/code><\/pre>\n\n\n\n<p>This code creates an ARP request for the target_ip and sends it to the network using the srp() function. The response is then captured and parsed to extract the IP and MAC addresses of the devices on the network.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using the netifaces Library<\/h3>\n\n\n\n<p>The netifaces library is a simpler tool that can be used to retrieve information about the network interfaces on a machine. To use the netifaces library, you can use the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import net<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Using the netifaces Library (continued)<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import netifaces\n\n# Get network interfaces\ninterfaces = netifaces.interfaces()\n\n# Print information for each interface\nfor interface in interfaces:\n    iface = netifaces.ifaddresses(interface)\n    if netifaces.AF_INET in iface:\n        ip = iface[netifaces.AF_INET][0]['addr']\n        netmask = iface[netifaces.AF_INET][0]['netmask']\n        broadcast = iface[netifaces.AF_INET][0]['broadcast']\n        print(\"Interface: {}\\tIP Address: {}\\tNetmask: {}\\tBroadcast: {}\".format(interface, ip, netmask, broadcast))\n<\/code><\/pre>\n\n\n\n<p>This code retrieves a list of network interfaces on the machine using the interfaces() function and retrieves the IP address, netmask, and broadcast address for each interface using the ifaddresses() function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Network scanning is an important part of network security, allowing system administrators to identify potential vulnerabilities and attack vectors. In this article, we explored network scanning techniques using Python, including port scanning and network discovery. By using these techniques, you can better understand the devices and services on your network and take steps to secure them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python Documentation: <a href=\"https:\/\/docs.python.org\/3\/library\/socket.html\" target=\"_blank\" rel=\"noopener\">socket<\/a><\/li>\n\n\n\n<li>Python Documentation: <a href=\"https:\/\/pypi.org\/project\/python-nmap\/\" target=\"_blank\" rel=\"noopener\">nmap<\/a><\/li>\n\n\n\n<li>Scapy Documentation: <a href=\"https:\/\/scapy.readthedocs.io\/en\/latest\/api\/scapy.all.html\" target=\"_blank\" rel=\"noopener\">scapy.all<\/a><\/li>\n\n\n\n<li>Netifaces Documentation: <a href=\"https:\/\/pypi.org\/project\/netifaces\/\" target=\"_blank\" rel=\"noopener\">netifaces<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Network scanning is the process of discovering devices on a network and identifying their open<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"footnotes":""},"categories":[18],"tags":[],"class_list":["post-1200","post","type-post","status-publish","format-standard","hentry","category-python"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Mastering Network Scanning with Python - tippang.net<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tippang.net\/?p=1200\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"og:locale\" content=\"en_US\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"og:type\" content=\"article\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"og:title\" content=\"Mastering Network Scanning with Python - tippang.net\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"og:description\" content=\"Network scanning is the process of discovering devices on a network and identifying their open\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tippang.net\/?p=1200\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"og:site_name\" content=\"tippang.net\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-11T13:40:24+00:00\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-11T13:40:27+00:00\" class=\"yoast-seo-meta-tag\" \/>\n<meta property=\"og:image\" content=\"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-4.png\" class=\"yoast-seo-meta-tag\" \/>\n<meta name=\"author\" content=\"charles kim\" class=\"yoast-seo-meta-tag\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" class=\"yoast-seo-meta-tag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" class=\"yoast-seo-meta-tag\" \/>\n\t<meta name=\"twitter:data1\" content=\"charles kim\" class=\"yoast-seo-meta-tag\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" class=\"yoast-seo-meta-tag\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" class=\"yoast-seo-meta-tag\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/tippang.net\/?p=1200#article\",\"isPartOf\":{\"@id\":\"https:\/\/tippang.net\/?p=1200\"},\"author\":{\"name\":\"charles kim\",\"@id\":\"https:\/\/tippang.net\/#\/schema\/person\/5fba0966333bf1aa9f72ad464d264d4a\"},\"headline\":\"Mastering Network Scanning with Python\",\"datePublished\":\"2023-09-11T13:40:24+00:00\",\"dateModified\":\"2023-09-11T13:40:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/tippang.net\/?p=1200\"},\"wordCount\":434,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/tippang.net\/#organization\"},\"image\":{\"@id\":\"https:\/\/tippang.net\/?p=1200#primaryimage\"},\"thumbnailUrl\":\"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-4.png\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/tippang.net\/?p=1200#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tippang.net\/?p=1200\",\"url\":\"https:\/\/tippang.net\/?p=1200\",\"name\":\"Mastering Network Scanning with Python - tippang.net\",\"isPartOf\":{\"@id\":\"https:\/\/tippang.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/tippang.net\/?p=1200#primaryimage\"},\"image\":{\"@id\":\"https:\/\/tippang.net\/?p=1200#primaryimage\"},\"thumbnailUrl\":\"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-4.png\",\"datePublished\":\"2023-09-11T13:40:24+00:00\",\"dateModified\":\"2023-09-11T13:40:27+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/tippang.net\/?p=1200#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tippang.net\/?p=1200\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tippang.net\/?p=1200#primaryimage\",\"url\":\"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-4.png\",\"contentUrl\":\"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-4.png\",\"width\":1024,\"height\":576},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tippang.net\/?p=1200#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tippang.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Mastering Network Scanning with Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tippang.net\/#website\",\"url\":\"https:\/\/tippang.net\/\",\"name\":\"tippang.net\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/tippang.net\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tippang.net\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/tippang.net\/#organization\",\"name\":\"tippang.net\",\"url\":\"https:\/\/tippang.net\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tippang.net\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/tippang.net\/wp-content\/uploads\/2021\/02\/cropped-\u110e\u1162\u1102\u1165\u11af\u110b\u1161\u110b\u1175\u110f\u1169\u11ab1_\u1100\u1165\u1107\u116e\u11a8\u110b\u1175.png\",\"contentUrl\":\"https:\/\/tippang.net\/wp-content\/uploads\/2021\/02\/cropped-\u110e\u1162\u1102\u1165\u11af\u110b\u1161\u110b\u1175\u110f\u1169\u11ab1_\u1100\u1165\u1107\u116e\u11a8\u110b\u1175.png\",\"width\":280,\"height\":280,\"caption\":\"tippang.net\"},\"image\":{\"@id\":\"https:\/\/tippang.net\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/tippang.net\/#\/schema\/person\/5fba0966333bf1aa9f72ad464d264d4a\",\"name\":\"charles kim\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tippang.net\/#\/schema\/person\/image\/\",\"url\":\"\/\/www.gravatar.com\/avatar\/fdb7dc5bc7fe5f3bf11d8491a1e8d9c4?s=96&#038;r=g&#038;d=wavatar\",\"contentUrl\":\"\/\/www.gravatar.com\/avatar\/fdb7dc5bc7fe5f3bf11d8491a1e8d9c4?s=96&#038;r=g&#038;d=wavatar\",\"caption\":\"charles kim\"},\"description\":\"Hello, Nice to meet you!\",\"url\":\"https:\/\/tippang.net\/author\/charles-kim\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Mastering Network Scanning with Python - tippang.net","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/tippang.net\/?p=1200","og_locale":"en_US","og_type":"article","og_title":"Mastering Network Scanning with Python - tippang.net","og_description":"Network scanning is the process of discovering devices on a network and identifying their open","og_url":"https:\/\/tippang.net\/?p=1200","og_site_name":"tippang.net","article_published_time":"2023-09-11T13:40:24+00:00","article_modified_time":"2023-09-11T13:40:27+00:00","og_image":[{"url":"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-4.png","type":"","width":"","height":""}],"author":"charles kim","twitter_card":"summary_large_image","twitter_misc":{"Written by":"charles kim","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/tippang.net\/?p=1200#article","isPartOf":{"@id":"https:\/\/tippang.net\/?p=1200"},"author":{"name":"charles kim","@id":"https:\/\/tippang.net\/#\/schema\/person\/5fba0966333bf1aa9f72ad464d264d4a"},"headline":"Mastering Network Scanning with Python","datePublished":"2023-09-11T13:40:24+00:00","dateModified":"2023-09-11T13:40:27+00:00","mainEntityOfPage":{"@id":"https:\/\/tippang.net\/?p=1200"},"wordCount":434,"commentCount":0,"publisher":{"@id":"https:\/\/tippang.net\/#organization"},"image":{"@id":"https:\/\/tippang.net\/?p=1200#primaryimage"},"thumbnailUrl":"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-4.png","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/tippang.net\/?p=1200#respond"]}]},{"@type":"WebPage","@id":"https:\/\/tippang.net\/?p=1200","url":"https:\/\/tippang.net\/?p=1200","name":"Mastering Network Scanning with Python - tippang.net","isPartOf":{"@id":"https:\/\/tippang.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/tippang.net\/?p=1200#primaryimage"},"image":{"@id":"https:\/\/tippang.net\/?p=1200#primaryimage"},"thumbnailUrl":"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-4.png","datePublished":"2023-09-11T13:40:24+00:00","dateModified":"2023-09-11T13:40:27+00:00","breadcrumb":{"@id":"https:\/\/tippang.net\/?p=1200#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tippang.net\/?p=1200"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tippang.net\/?p=1200#primaryimage","url":"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-4.png","contentUrl":"https:\/\/tippang.net\/wp-content\/uploads\/2023\/09\/image-4.png","width":1024,"height":576},{"@type":"BreadcrumbList","@id":"https:\/\/tippang.net\/?p=1200#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tippang.net\/"},{"@type":"ListItem","position":2,"name":"Mastering Network Scanning with Python"}]},{"@type":"WebSite","@id":"https:\/\/tippang.net\/#website","url":"https:\/\/tippang.net\/","name":"tippang.net","description":"","publisher":{"@id":"https:\/\/tippang.net\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tippang.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/tippang.net\/#organization","name":"tippang.net","url":"https:\/\/tippang.net\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tippang.net\/#\/schema\/logo\/image\/","url":"https:\/\/tippang.net\/wp-content\/uploads\/2021\/02\/cropped-\u110e\u1162\u1102\u1165\u11af\u110b\u1161\u110b\u1175\u110f\u1169\u11ab1_\u1100\u1165\u1107\u116e\u11a8\u110b\u1175.png","contentUrl":"https:\/\/tippang.net\/wp-content\/uploads\/2021\/02\/cropped-\u110e\u1162\u1102\u1165\u11af\u110b\u1161\u110b\u1175\u110f\u1169\u11ab1_\u1100\u1165\u1107\u116e\u11a8\u110b\u1175.png","width":280,"height":280,"caption":"tippang.net"},"image":{"@id":"https:\/\/tippang.net\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/tippang.net\/#\/schema\/person\/5fba0966333bf1aa9f72ad464d264d4a","name":"charles kim","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tippang.net\/#\/schema\/person\/image\/","url":"\/\/www.gravatar.com\/avatar\/fdb7dc5bc7fe5f3bf11d8491a1e8d9c4?s=96&#038;r=g&#038;d=wavatar","contentUrl":"\/\/www.gravatar.com\/avatar\/fdb7dc5bc7fe5f3bf11d8491a1e8d9c4?s=96&#038;r=g&#038;d=wavatar","caption":"charles kim"},"description":"Hello, Nice to meet you!","url":"https:\/\/tippang.net\/author\/charles-kim"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/tippang.net\/index.php?rest_route=\/wp\/v2\/posts\/1200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tippang.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tippang.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tippang.net\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/tippang.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1200"}],"version-history":[{"count":1,"href":"https:\/\/tippang.net\/index.php?rest_route=\/wp\/v2\/posts\/1200\/revisions"}],"predecessor-version":[{"id":1202,"href":"https:\/\/tippang.net\/index.php?rest_route=\/wp\/v2\/posts\/1200\/revisions\/1202"}],"wp:attachment":[{"href":"https:\/\/tippang.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tippang.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tippang.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}