{"id":1163674,"date":"2025-01-15T14:57:51","date_gmt":"2025-01-15T06:57:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1163674.html"},"modified":"2025-01-15T14:57:54","modified_gmt":"2025-01-15T06:57:54","slug":"python-%e5%a6%82%e4%bd%95ping%e8%8e%b7%e5%8f%96ip%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1163674.html","title":{"rendered":"python \u5982\u4f55ping\u83b7\u53d6ip\u5730\u5740"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25204419\/e824376a-9ac2-41aa-a33d-45a77cad0038.webp\" alt=\"python \u5982\u4f55ping\u83b7\u53d6ip\u5730\u5740\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u4f7f\u7528ping\u547d\u4ee4\u6765\u83b7\u53d6IP\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>subprocess<\/code>\u6a21\u5757\u6765\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\uff0c\u5e76\u89e3\u6790\u8f93\u51fa\u3002<strong>\u53ef\u4ee5\u4f7f\u7528subprocess\u6a21\u5757\u3001scapy\u5e93\u3001ping3\u5e93\u6765\u83b7\u53d6IP\u5730\u5740<\/strong>\u3002\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528subprocess\u6a21\u5757\uff0c\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528subprocess\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684<code>subprocess<\/code>\u6a21\u5757\u5141\u8bb8\u4f60\u751f\u6210\u65b0\u7684\u7cfb\u7edf\u8fdb\u7a0b\u3001\u8fde\u63a5\u5b83\u4eec\u7684\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\uff0c\u5e76\u83b7\u53d6\u8fd4\u56de\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u4f8b\u5b50\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u6765ping\u4e00\u4e2a\u57df\u540d\u5e76\u89e3\u6790\u5176IP\u5730\u5740\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7684Ping\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528subprocess\u6a21\u5757\u6765\u6267\u884cping\u547d\u4ee4\u3002<code>ping<\/code>\u547d\u4ee4\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u53ef\u80fd\u7565\u6709\u4e0d\u540c\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u7684\u7c7b\u578b\u6765\u9009\u62e9\u5408\u9002\u7684\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>import platform<\/p>\n<p>def ping_host(host):<\/p>\n<p>    # \u68c0\u67e5\u64cd\u4f5c\u7cfb\u7edf\u7c7b\u578b<\/p>\n<p>    param = &#39;-n&#39; if platform.system().lower() == &#39;windows&#39; else &#39;-c&#39;<\/p>\n<p>    # \u4f7f\u7528subprocess\u6267\u884cping\u547d\u4ee4<\/p>\n<p>    command = [&#39;ping&#39;, param, &#39;1&#39;, host]<\/p>\n<p>    result = subprocess.run(command, stdout=subprocess.PIPE, text=True)<\/p>\n<p>    return result.stdout<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>output = ping_host(&#39;google.com&#39;)<\/p>\n<p>print(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u89e3\u6790Ping\u547d\u4ee4\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u6267\u884cping\u547d\u4ee4\u540e\uff0c\u6211\u4eec\u9700\u8981\u89e3\u6790\u547d\u4ee4\u7684\u8f93\u51fa\u4ee5\u63d0\u53d6IP\u5730\u5740\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4eceping\u547d\u4ee4\u7684\u8f93\u51fa\u4e2d\u63d0\u53d6IP\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_ip(output):<\/p>\n<p>    # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914dIP\u5730\u5740<\/p>\n<p>    ip_pattern = re.compile(r&#39;(\\d{1,3}\\.){3}\\d{1,3}&#39;)<\/p>\n<p>    match = ip_pattern.search(output)<\/p>\n<p>    if match:<\/p>\n<p>        return match.group(0)<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>output = ping_host(&#39;google.com&#39;)<\/p>\n<p>ip_address = extract_ip(output)<\/p>\n<p>print(f&#39;IP Address: {ip_address}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5b8c\u6574\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u4ee3\u7801\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528subprocess\u6a21\u5757ping\u4e00\u4e2a\u57df\u540d\u5e76\u63d0\u53d6\u5176IP\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>import platform<\/p>\n<p>import re<\/p>\n<p>def ping_host(host):<\/p>\n<p>    param = &#39;-n&#39; if platform.system().lower() == &#39;windows&#39; else &#39;-c&#39;<\/p>\n<p>    command = [&#39;ping&#39;, param, &#39;1&#39;, host]<\/p>\n<p>    result = subprocess.run(command, stdout=subprocess.PIPE, text=True)<\/p>\n<p>    return result.stdout<\/p>\n<p>def extract_ip(output):<\/p>\n<p>    ip_pattern = re.compile(r&#39;(\\d{1,3}\\.){3}\\d{1,3}&#39;)<\/p>\n<p>    match = ip_pattern.search(output)<\/p>\n<p>    if match:<\/p>\n<p>        return match.group(0)<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>output = ping_host(&#39;google.com&#39;)<\/p>\n<p>ip_address = extract_ip(output)<\/p>\n<p>print(f&#39;IP Address: {ip_address}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528scapy\u5e93<\/h3>\n<\/p>\n<p><p><code>scapy<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7f51\u7edc\u6570\u636e\u5305\u5904\u7406\u5e93\uff0c\u652f\u6301\u5404\u79cd\u7f51\u7edc\u534f\u8bae\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528scapy\u6765\u53d1\u9001ping\u8bf7\u6c42\u5e76\u83b7\u53d6IP\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5scapy<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5scapy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install scapy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528scapy\u53d1\u9001Ping\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528scapy\u53d1\u9001ping\u8bf7\u6c42\u5e76\u83b7\u53d6IP\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scapy.all import sr1, IP, ICMP<\/p>\n<p>def ping_host(host):<\/p>\n<p>    pkt = IP(dst=host)\/ICMP()<\/p>\n<p>    reply = sr1(pkt, timeout=2)<\/p>\n<p>    if reply:<\/p>\n<p>        return reply.src<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>ip_address = ping_host(&#39;google.com&#39;)<\/p>\n<p>print(f&#39;IP Address: {ip_address}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528ping3\u5e93<\/h3>\n<\/p>\n<p><p><code>ping3<\/code>\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684ping\u5e93\uff0c\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u7b80\u5355\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528ping3\u5e93\u6765ping\u4e00\u4e2a\u57df\u540d\u5e76\u83b7\u53d6IP\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5ping3<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5ping3\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install ping3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528ping3\u53d1\u9001Ping\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528ping3\u53d1\u9001ping\u8bf7\u6c42\u5e76\u83b7\u53d6IP\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from ping3 import ping, verbose_ping<\/p>\n<p>def ping_host(host):<\/p>\n<p>    delay = ping(host)<\/p>\n<p>    if delay is not None:<\/p>\n<p>        return host<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>ip_address = ping_host(&#39;google.com&#39;)<\/p>\n<p>print(f&#39;IP Address: {ip_address}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5ping\u4e00\u4e2a\u57df\u540d\u5e76\u83b7\u53d6IP\u5730\u5740\uff0c\u5305\u62ec\u4f7f\u7528subprocess\u6a21\u5757\u3001scapy\u5e93\u548cping3\u5e93\u3002<strong>\u4f7f\u7528subprocess\u6a21\u5757\u3001scapy\u5e93\u3001ping3\u5e93\u6765\u83b7\u53d6IP\u5730\u5740<\/strong>\u3002\u5176\u4e2d\uff0csubprocess\u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u4f60\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u5e76\u89e3\u6790\u8f93\u51fa\u3002scapy\u5e93\u548cping3\u5e93\u4e5f\u662f\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\u548c\u7b80\u5355\u7684\u63a5\u53e3\u3002\u6839\u636e\u4f60\u7684\u9700\u6c42\u548c\u504f\u597d\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0ping\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0ping\u64cd\u4f5c\u6765\u83b7\u53d6IP\u5730\u5740\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8fdb\u884cping\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u6216\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6765\u5b9e\u73b0\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u6765\u6267\u884cping\u547d\u4ee4\uff0c\u6216\u8005\u4f7f\u7528<code>ping3<\/code>\u7b49\u5e93\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u6709\u6548\u8fd4\u56de\u76ee\u6807IP\u7684\u8fde\u901a\u6027\u548c\u5ef6\u8fdf\u4fe1\u606f\u3002<\/p>\n<p><strong>\u4f7f\u7528Python ping\u7684\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u9047\u5230\u54ea\u4e9b\u5e38\u89c1\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8fdb\u884cping\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u95ee\u9898\uff0c\u7279\u522b\u662f\u5728Linux\u6216Mac\u7cfb\u7edf\u4e0a\uff0c\u666e\u901a\u7528\u6237\u53ef\u80fd\u65e0\u6cd5\u6267\u884cping\u547d\u4ee4\u3002\u6b64\u5916\uff0c\u7f51\u7edc\u9632\u706b\u5899\u8bbe\u7f6e\u4e5f\u53ef\u80fd\u5bfc\u81f4ping\u8bf7\u6c42\u88ab\u963b\u6b62\u3002\u786e\u4fdd\u60a8\u7684\u7f51\u7edc\u73af\u5883\u5141\u8bb8ping\u64cd\u4f5c\uff0c\u5e76\u68c0\u67e5\u662f\u5426\u5177\u6709\u76f8\u5e94\u7684\u6267\u884c\u6743\u9650\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python ping\u64cd\u4f5c\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8fdb\u884cping\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u56e0\u4e3a\u7f51\u7edc\u4e0d\u901a\u3001\u76ee\u6807\u4e3b\u673a\u5173\u95ed\u6216\u5176\u4ed6\u539f\u56e0\u5bfc\u81f4\u5f02\u5e38\u3002\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u6355\u6349\u8fd9\u4e9b\u9519\u8bef\uff0c\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u5904\u7406<code>subprocess.CalledProcessError<\/code>\uff0c\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u9519\u8bef\u800c\u5d29\u6e83\uff0c\u5e76\u80fd\u591f\u7ed9\u51fa\u76f8\u5e94\u7684\u9519\u8bef\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u63a8\u8350\u7684\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u7b80\u5316ping\u64cd\u4f5c\u7684\u5b9e\u73b0\uff1f<\/strong><br \/>\u662f\u7684\uff0c<code>ping3<\/code>\u548c<code>scapy<\/code>\u7b49\u5e93\u53ef\u4ee5\u5e2e\u52a9\u7b80\u5316ping\u64cd\u4f5c\u7684\u5b9e\u73b0\u3002<code>ping3<\/code>\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u76f4\u63a5\u53d1\u9001ping\u8bf7\u6c42\u5e76\u83b7\u53d6\u7ed3\u679c\uff0c\u800c<code>scapy<\/code>\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u7f51\u7edc\u6570\u636e\u5305\u5904\u7406\u529f\u80fd\uff0c\u9002\u5408\u66f4\u590d\u6742\u7684\u7f51\u7edc\u64cd\u4f5c\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u4f7f\u7528ping\u547d\u4ee4\u6765\u83b7\u53d6IP\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684subprocess\u6a21\u5757\u6765\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\uff0c\u5e76\u89e3\u6790 [&hellip;]","protected":false},"author":3,"featured_media":1163679,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163674"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1163674"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163674\/revisions"}],"predecessor-version":[{"id":1163680,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163674\/revisions\/1163680"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1163679"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1163674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1163674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1163674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}