{"id":38408,"date":"2024-11-26T08:17:32","date_gmt":"2024-11-26T00:17:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38408\/"},"modified":"2024-11-26T08:17:32","modified_gmt":"2024-11-26T00:17:32","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e6%9e%84%e5%bb%baping-sweeper","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38408\/","title":{"rendered":"\u5982\u4f55\u5728Python\u4e2d\u6784\u5efaPing Sweeper"},"content":{"rendered":"<p> \u662f\u4e00\u79cd\u901a\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u7528\u4e8e\u7f16\u5199\u4e0d\u540c\u7c7b\u578b\u7684 gui \u548c\u57fa\u4e8e cli \u7684\u5e94\u7528\u7a0b\u5e8f\u3002 \u5982\u679c\u60a8\u662f python \u65b0\u624b\uff0c\u6ca1\u6709\u6bd4\u4ece\u4e8b\u5c0f\u578b\u9879\u76ee\u66f4\u597d\u7684\u65b9\u6cd5\u6765\u52a0\u5f3a\u60a8\u7684\u5b66\u4e60\u4e86\u3002<\/p>\n<p>\u4e00\u4e2a\u5f88\u68d2\u7684 Python \u9879\u76ee\u793a\u4f8b\u662f\u4e00\u4e2a ping \u626b\u63cf\u5668\uff0c\u5b83\u662f\u4e00\u4e2a\u68c0\u67e5\u7f51\u7edc\u4e3b\u673a\u7684\u5c0f\u5b9e\u7528\u7a0b\u5e8f\u3002 \u8be5\u811a\u672c\u5c06\u6db5\u76d6\u57fa\u672c\u7684\u7f16\u7a0b\u6982\u5ff5\uff0c\u5305\u62ec\u6253\u5370\u8bed\u53e5\u3001\u5faa\u73af\u548c\u51fd\u6570\u3002<\/p>\n<h2>\u4ec0\u4e48\u662fPing Sweeper<\/h2>\n<p>ping \u626b\u63cf\u5668\u662f\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u63a5\u53d7\u7f51\u7edc\u5730\u5740\u4f5c\u4e3a\u8f93\u5165\uff0cping \u7f51\u7edc\u4e2d\u7684\u4e3b\u673a\uff0c\u5e76\u8f93\u51fa\u6b7b\u6d3b\u4e3b\u673a\u5217\u8868\u3002 \u8fd9\u662f\u4e00\u79cd\u4f30\u7b97\u7f51\u7edc\u4e2d\u5728\u7ebf\u4e3b\u673a\u6570\u91cf\u5e76\u627e\u51fa\u5b83\u4eec\u7684 IPv4 \u5730\u5740\u7684\u7b80\u5355\u65b9\u6cd5\u3002<\/p>\n<p>\u4f5c\u4e3a\u521d\u5b66\u8005\uff0c\u521b\u5efa\u4e00\u4e2a ping \u626b\u63cf\u5668\u662f\u590d\u4e60 Python \u57fa\u7840\u77e5\u8bc6\u7684\u597d\u65b9\u6cd5\u3002 \u8fd9\u4e2a\u52a8\u624b\u5c0f\u9879\u76ee\u8fd8\u5c06\u5237\u65b0\u60a8\u5bf9\u7f51\u7edc\u57fa\u7840\u77e5\u8bc6\u7684\u7406\u89e3\u3002<\/p>\n<h2>Ping Sweeper \u7684\u5148\u51b3\u6761\u4ef6<\/h2>\n<p>\u5728\u5f00\u59cb\u5f00\u53d1\u8fc7\u7a0b\u4e4b\u524d\uff0c\u60a8\u5e94\u8be5\u786e\u4fdd\u60a8\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86\u6700\u65b0\u7248\u672c\u7684 Python\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u901a\u8fc7\u5728\u547d\u4ee4\u63d0\u793a\u7b26\uff08\u5bf9\u4e8e Windows \u7528\u6237\uff09\u6216\u7ec8\u7aef\uff08\u5bf9\u4e8e UNIX\/Linux \u7cfb\u7edf\uff09\u4e2d\u952e\u5165\u6b64\u547d\u4ee4\u5e76\u70b9\u51fb\u6765\u68c0\u67e5\u60a8\u662f\u5426\u53ef\u4ee5\u8fd0\u884c Python <strong>\u8fdb\u5165<\/strong>\uff1a<\/p>\n<pre>python --version<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8fd0\u884c\uff1a<\/p>\n<pre>python3 --version<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u547d\u4ee4\u5e94\u8fd4\u56de\u7cfb\u7edf\u4e0a\u5b89\u88c5\u7684 Python \u7248\u672c\u3002 \u5982\u679c\u5b83\u8fd4\u56de\u7c7b\u4f3c\u201cpython not found\u201d\u7684\u9519\u8bef\uff0c\u60a8\u5e94\u8be5\u5b89\u88c5 Python3\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\u3002<\/p>\n<h2>\u7f16\u7801 Ping Sweeper<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/242\/473\/172803734425446.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728Python\u4e2d\u6784\u5efaPing Sweeper\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728Python\u4e2d\u6784\u5efaPing Sweeper\u63d2\u56fe\" \/> <\/p>\n<p>\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6784\u5efa\u6b64\u811a\u672c\u3002 \u6709\u4e9b\u4f1a\u8981\u6c42\u60a8\u5b89\u88c5\u548c\u5bfc\u5165\u51e0\u4e2a\u6a21\u5757\u3002 \u5728\u8fd9\u91cc\uff0c\u60a8\u5c06\u91c7\u7528\u4e00\u79cd\u6781\u7b80\u4e3b\u4e49\u7684\u65b9\u6cd5\uff0c\u9664\u4e86\u5173\u952e\u7684\u4e4b\u5916\u6ca1\u6709\u5916\u90e8\u4f9d\u8d56\u9879 <strong>\u6a21\u5757<\/strong>.<\/p>\n<p>\u5728\u5f00\u59cb\u7f16\u7801\u4e4b\u524d\uff0c\u5206\u89e3\u9700\u6c42\u4ee5\u66f4\u597d\u5730\u4e86\u89e3\u60a8\u9700\u8981\u5b9e\u73b0\u54ea\u4e9b\u529f\u80fd\u3002 \u8be5\u811a\u672c\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a<\/p>\n<ol>\n<li>\u63a5\u53d7\u8f93\u5165\u7684 IP \u5730\u5740\u3002<\/li>\n<li>\u4ece IP \u5730\u5740\u4e2d\u63d0\u53d6\u7f51\u7edc ID\u3002<\/li>\n<li>\u904d\u5386\u7f51\u7edc\u4e2d\u7684\u6240\u6709\u4e3b\u673a\u5e76\u6253\u5370\u4e3b\u673a\u662f\u6b7b\u662f\u6d3b\u3002<\/li>\n<\/ol>\n<p>\u73b0\u5728\u60a8\u5df2\u7ecf\u6e05\u695a\u5730\u4e86\u89e3\u4e86\u5de5\u4f5c\u6d41\u7a0b\uff0c\u8ba9\u6211\u4eec\u5f00\u59cb\u7f16\u7a0b\u3002<\/p>\n<h3>\u63a5\u53d7\u548c\u5904\u7406\u8f93\u5165<\/h3>\n<p>\u811a\u672c\u7684\u7b2c\u4e00\u90e8\u5206\u5904\u7406\u63a5\u53d7\u6765\u81ea\u7528\u6237\u7684\u8f93\u5165\u5e76\u5c06 IPv4 \u5730\u5740\u51cf\u5c11\u5230\u5b83\u7684\u524d\u4e09\u4e2a\u516b\u4f4d\u5b57\u8282\u3002 \u8fd9\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u7f51\u7edc ID\uff1a<\/p>\n<pre>import os<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>IP = input(\u201c[+] Enter the Host IP Address: \u201d)<br \/>print(\u201c[+] Starting Ping Sweeper on \u201d + IP)<br \/>dot = IP.rfind(\u201c.\u201d)<br \/>IP = IP[0:dot + 1]\u8fd9 <strong>\u8f93\u5165\uff08\uff09 <\/strong>\u51fd\u6570\u63a5\u53d7\u7528\u6237\u8f93\u5165\u3002 \u60a8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684 <strong>rfind()<\/strong> \u65b9\u6cd5\u63d0\u53d6\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u5c0f\u6570\u70b9\u7684\u7d22\u5f15\u5e76\u5c06\u5176\u5b58\u50a8\u5728 <strong>\u70b9 <\/strong>\u591a\u53d8\u7684\u3002 \u901a\u8fc7\u4fdd\u7559\u4ece\u8f93\u5165\u5230\u6700\u53f3\u8fb9\u51fa\u73b0\u5c0f\u6570\u70b9\u7684\u6240\u6709\u5185\u5bb9\u6765\u8ddf\u8fdb\u5b83\u3002<\/p>\n<p>\u626b\u63cf\u4e3b\u673a\u5e76\u6253\u5370\u4e3b\u673a\u72b6\u6001<\/p>\n<pre><\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u60a8\u5df2\u4ece\u8f93\u5165 IP \u6d3e\u751f\u7f51\u7edc\u5730\u5740\u3002 \u60a8\u73b0\u5728\u53ef\u4ee5\u904d\u5386\u6700\u7ec8 IPv4 \u516b\u4f4d\u5b57\u8282\u7684\u6240\u6709\u53ef\u80fd\u503c\uff1a1\u2013254\u3002 \u5728 for \u5faa\u73af\u4e2d\uff0c\u5c06\u65b0 IP \u5b58\u50a8\u5728 <strong>\u4e3b\u6301\u4eba<\/strong> \u591a\u53d8\u7684\u3002 \u6b64 IP \u662f\u57fa\u7840 IP\uff0c\u540e\u8ddf\u8fed\u4ee3\u5668\u53d8\u91cf\u7684\u503c\u3002 \u7136\u540e\uff0c\u4f7f\u7528 <strong>os.system()<\/strong> \u8fd0\u884c\u65b9\u6cd5 <strong>\u5e73<\/strong> \u547d\u4ee4\u53cd\u5bf9 <strong>\u4e3b\u6301\u4eba <\/strong>\u591a\u53d8\u7684\u3002<\/p>\n<pre>for i in range(1, 255):\nhost = IP + str(i)\nresponse = os.system(\"ping -c 1 -w 1 \" + host + \" &gt;\/dev\/null\")<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>if response == 0:<br \/>print(host + \u201d is up\u201d)<br \/>else:<br \/>print(host + \u201d is down\u201d)<\/p>\n<pre><\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6d4b\u8bd5 <strong>\u56de\u590d<\/strong> \u503c\u5bf9 0 \u6765\u786e\u5b9a\u4e3b\u673a\u7684\u72b6\u6001\u5e76\u51b3\u5b9a\u5b83\u662f\u5728\u7ebf\u8fd8\u662f\u79bb\u7ebf\u3002 \u5982\u679c ping \u9047\u5230\u65e0\u54cd\u5e94\u7684\u4e3b\u673a\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u975e\u96f6\u503c\u3002 \u5426\u5219\uff0c\u5b83\u8fd4\u56de\u96f6\u4ee5\u6307\u793a\u5b83\u53ef\u4ee5\u5230\u8fbe\u7684\u4e3b\u673a\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u6dfb\u52a0 <strong>C <\/strong>\u6807\u5fd7\u548c <strong>w<\/strong> \u6807\u5fd7\uff0c\u503c\u4e3a 1\uff0c\u7528\u4e8e\u539f\u59cb ping \u547d\u4ee4\u3002 \u8fd9\u4f7f\u5f97\u5b83\u53ea\u53d1\u9001\u4e00\u4e2a\u6570\u636e\u5305\u5e76\u7b49\u5f85\u4e00\u79d2\u949f\u6765\u63a5\u6536\u54cd\u5e94\u3002 \u60a8\u7684 ping \u7248\u672c\u53ef\u80fd\u652f\u6301\u4e5f\u53ef\u80fd\u4e0d\u652f\u6301\u8fd9\u4e9b\u9009\u9879\uff1b \u8bf7\u67e5\u9605 ping \u624b\u518c\u9875\u8fdb\u884c\u68c0\u67e5\u3002<\/p>\n<p>\u60a8\u8fd8\u5e94\u8be5\u5c06\u8f93\u51fa\u91cd\u5b9a\u5411\u5230 <strong>\/dev\/null<\/strong> \u9690\u85cf ping \u8f93\u51fa\u7684\u8be6\u7ec6\u4fe1\u606f\u3002 \u8bf7\u6ce8\u610f\uff0cping \u548c \/dev\/null \u8bed\u6cd5\u4ec5\u4e0e Unix \u6216 Linux \u7cfb\u7edf\u517c\u5bb9\u3002 \u60a8\u53ef\u4ee5\u901a\u8fc7\u66ff\u6362 <strong>C<\/strong> \u6807\u5fd7\u4e0e <strong>n <\/strong>\u548c <strong>&gt;\/\u5f00\u53d1\/\u7a7a<\/strong> \u548c <strong>&gt;\u65e0<\/strong>.<\/p>\n<h2>\u8fd0\u884c Ping Sweeper \u811a\u672c<\/h2>\n<p>\u60a8\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u6216\u901a\u8fc7\u547d\u4ee4\u63d0\u793a\u7b26\u8fd0\u884c\u6b64\u811a\u672c\u3002 \u542f\u52a8\u4e00\u4e2a\u7ec8\u7aef\uff0c\u79fb\u52a8\u5230\u811a\u672c\u7684\u4f4d\u7f6e\u5e76\u4f7f\u7528 python3 \u6267\u884c\u5b83\uff1a<\/p>\n<pre>cd \/directory\/sweeper\/\npython3 sweeper.py <\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165 IPv4 \u5730\u5740\u6216\uff0cping \u626b\u63cf\u5668\u5e94\u8be5\u5f00\u59cb\u5de5\u4f5c\u5e76\u8fd4\u56de\u9884\u671f\u7684\u8f93\u51fa\u3002<\/p>\n<h2>\u6709\u8da3\u7684 Python \u9879\u76ee\u521b\u610f<\/h2>\n<p>\u52a8\u624b\u5b66\u4e60\u53ef\u4ee5\u8bf4\u662f\u5b66\u4e60\u7f16\u7a0b\u8bed\u8a00\u7684\u6700\u597d\u548c\u6700\u5feb\u7684\u65b9\u6cd5\u3002 \u60a8\u4ece\u4e8b\u7684\u9879\u76ee\u8d8a\u591a\uff0c\u60a8\u5c31\u8d8a\u80fd\u638c\u63e1\u6982\u5ff5\u3001\u57f9\u517b\u57fa\u672c\u6280\u80fd\u5e76\u4e86\u89e3\u5982\u4f55\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<p>\u5982\u679c\u60a8\u7f3a\u5c11\u9879\u76ee\u521b\u610f\uff0c\u8bf7\u67e5\u770b\u8fd9\u4efd\u7cbe\u9009\u7684 Python \u6700\u4f73\u9879\u76ee\u521b\u610f\u5217\u8868\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Python\u4e2d\u6784\u5efaPing Sweeper\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u662f\u4e00\u79cd\u901a\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u7528\u4e8e\u7f16\u5199\u4e0d\u540c\u7c7b\u578b\u7684 gui \u548c\u57fa\u4e8e cli \u7684\u5e94\u7528\u7a0b\u5e8f\u3002 \u5982\u679c\u60a8\u662f python \u65b0\u624b\uff0c\u6ca1\u6709\u6bd4\u4ece\u4e8b\u5c0f\u578b\u9879\u76ee\u66f4\u597d\u7684\u65b9\u6cd5\u6765\u52a0\u5f3a\u60a8\u7684\u5b66\u4e60\u4e86\u3002 \u4e00\u4e2a\u5f88\u68d2\u7684 Python \u9879\u76ee\u793a\u4f8b\u662f\u4e00\u4e2a ping \u626b\u63cf\u5668\uff0c\u5b83\u662f\u4e00\u4e2a\u68c0\u67e5\u7f51\u7edc\u4e3b\u673a\u7684\u5c0f\u5b9e\u7528\u7a0b\u5e8f\u3002 \u8be5\u811a\u672c\u5c06\u6db5\u76d6\u57fa\u672c\u7684\u7f16\u7a0b\u6982\u5ff5\uff0c\u5305\u62ec\u6253\u5370\u8bed\u53e5\u3001\u5faa\u73af\u548c\u51fd\u6570\u3002 \u4ec0\u4e48\u662fPing Sweeper ping \u626b\u63cf\u5668\u662f\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u63a5\u53d7\u7f51\u7edc\u5730\u5740\u4f5c\u4e3a\u8f93\u5165\uff0cping \u7f51\u7edc\u4e2d\u7684\u4e3b\u673a\uff0c\u5e76\u8f93\u51fa\u6b7b\u6d3b\u4e3b\u673a\u5217\u8868\u3002 \u8fd9\u662f\u4e00\u79cd\u4f30\u7b97\u7f51\u7edc\u4e2d\u5728\u7ebf\u4e3b\u673a\u6570\u91cf\u5e76\u627e\u51fa\u5b83\u4eec\u7684 IPv4 \u5730\u5740\u7684\u7b80\u5355\u65b9\u6cd5\u3002 \u4f5c\u4e3a\u521d\u5b66\u8005\uff0c\u521b\u5efa\u4e00\u4e2a ping \u626b\u63cf\u5668\u662f\u590d\u4e60 Python \u57fa\u7840\u77e5\u8bc6\u7684\u597d\u65b9\u6cd5\u3002 \u8fd9\u4e2a\u52a8\u624b\u5c0f\u9879\u76ee\u8fd8\u5c06\u5237\u65b0\u60a8\u5bf9\u7f51\u7edc\u57fa\u7840\u77e5\u8bc6\u7684\u7406\u89e3\u3002 Ping Sweeper \u7684\u5148\u51b3\u6761\u4ef6 \u5728\u5f00\u59cb\u5f00\u53d1\u8fc7\u7a0b\u4e4b\u524d\uff0c\u60a8\u5e94\u8be5\u786e\u4fdd\u60a8\u7684\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86\u6700\u65b0\u7248\u672c\u7684 Python\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u901a\u8fc7\u5728\u547d\u4ee4\u63d0\u793a\u7b26\uff08\u5bf9\u4e8e Windows \u7528\u6237\uff09\u6216\u7ec8\u7aef\uff08\u5bf9\u4e8e UNIX\/Linux \u7cfb\u7edf\uff09\u4e2d\u952e\u5165\u6b64\u547d\u4ee4\u5e76\u70b9\u51fb\u6765\u68c0\u67e5\u60a8\u662f\u5426\u53ef\u4ee5\u8fd0\u884c Python \u8fdb\u5165\uff1a python &#8211;version \u767b\u5f55\u540e\u590d\u5236 \u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8fd0\u884c\uff1a python3 &#8211;version \u767b\u5f55\u540e\u590d\u5236 \u6b64\u547d\u4ee4\u5e94\u8fd4\u56de\u7cfb\u7edf\u4e0a\u5b89\u88c5\u7684 Python \u7248\u672c\u3002 \u5982\u679c\u5b83\u8fd4\u56de\u7c7b\u4f3c\u201cpython not found\u201d\u7684\u9519\u8bef\uff0c\u60a8\u5e94\u8be5\u5b89\u88c5 Python3\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-38408","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=38408"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38408\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}