{"id":1004476,"date":"2024-12-27T10:26:44","date_gmt":"2024-12-27T02:26:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1004476.html"},"modified":"2024-12-27T10:26:47","modified_gmt":"2024-12-27T02:26:47","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%89%a7%e8%a1%8cifconfig","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1004476.html","title":{"rendered":"\u5982\u4f55\u7528python\u6267\u884cifconfig"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081412\/843a5555-2046-4fec-a1f7-7d25ff9c6f42.webp\" alt=\"\u5982\u4f55\u7528python\u6267\u884cifconfig\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u6267\u884c<code>ifconfig<\/code>\u547d\u4ee4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528subprocess\u6a21\u5757\uff0c\u901a\u8fc7\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3001\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u5b89\u5168\u6027\uff0c\u5efa\u8bae\u5bf9\u547d\u4ee4\u7684\u8f93\u51fa\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\u3001\u6b64\u5916\uff0c\u4f7f\u7528try-except\u5757\u53ef\u4ee5\u6355\u83b7\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u901a\u8fc7<code>subprocess<\/code>\u6a21\u5757\u3002\u8be5\u6a21\u5757\u5141\u8bb8\u4f60\u4ee5\u5b50\u8fdb\u7a0b\u7684\u5f62\u5f0f\u8fd0\u884c\u547d\u4ee4\uff0c\u5e76\u53ef\u4ee5\u6355\u83b7\u5176\u8f93\u51fa\u3002\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728Python\u811a\u672c\u4e2d\u6267\u884c<code>ifconfig<\/code>\u547d\u4ee4\uff0c\u5e76\u5bf9\u5176\u8f93\u51fa\u8fdb\u884c\u89e3\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528subprocess\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>subprocess<\/code>\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u7684\u6807\u51c6\u6a21\u5757\u3002\u901a\u8fc7\u8be5\u6a21\u5757\uff0cPython\u7a0b\u5e8f\u53ef\u4ee5\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\uff0c\u5e76\u53ef\u4ee5\u9009\u62e9\u6027\u5730\u6355\u83b7\u547d\u4ee4\u7684\u8f93\u51fa\u3001\u9519\u8bef\u4fe1\u606f\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u8981\u6267\u884c<code>ifconfig<\/code>\u547d\u4ee4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess.run()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u6267\u884c\u4e00\u4e2a\u547d\u4ee4\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a<code>CompletedProcess<\/code>\u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\u3001\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>def execute_ifconfig():<\/p>\n<p>    try:<\/p>\n<p>        result = subprocess.run([&#39;ifconfig&#39;], capture_output=True, text=True, check=True)<\/p>\n<p>        print(result.stdout)<\/p>\n<p>    except subprocess.CalledProcessError as e:<\/p>\n<p>        print(f&quot;Error occurred: {e}&quot;)<\/p>\n<p>execute_ifconfig()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>capture_output=True<\/code>\u8868\u793a\u8981\u6355\u83b7\u547d\u4ee4\u7684\u8f93\u51fa\uff0c<code>text=True<\/code>\u8868\u793a\u5c06\u8f93\u51fa\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c<code>check=True<\/code>\u8868\u793a\u5982\u679c\u547d\u4ee4\u8fd4\u56de\u4e00\u4e2a\u975e\u96f6\u9000\u51fa\u72b6\u6001\uff0c\u5219\u4f1a\u5f15\u53d1\u4e00\u4e2a<code>CalledProcessError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>2. \u5904\u7406\u547d\u4ee4\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u547d\u4ee4\u7684\u8f93\u51fa\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u63d0\u53d6\u7279\u5b9a\u7684\u7f51\u7edc\u63a5\u53e3\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u89e3\u6790\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>import re<\/p>\n<p>def parse_ifconfig_output():<\/p>\n<p>    try:<\/p>\n<p>        result = subprocess.run([&#39;ifconfig&#39;], capture_output=True, text=True, check=True)<\/p>\n<p>        output = result.stdout<\/p>\n<p>        # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u89e3\u6790IP\u5730\u5740<\/p>\n<p>        ip_pattern = re.compile(r&#39;inet (\\d+\\.\\d+\\.\\d+\\.\\d+)&#39;)<\/p>\n<p>        ips = ip_pattern.findall(output)<\/p>\n<p>        print(&quot;IP Addresses found:&quot;)<\/p>\n<p>        for ip in ips:<\/p>\n<p>            print(ip)<\/p>\n<p>    except subprocess.CalledProcessError as e:<\/p>\n<p>        print(f&quot;Error occurred: {e}&quot;)<\/p>\n<p>parse_ifconfig_output()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u6240\u6709\u7684IP\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u6355\u83b7<\/h3>\n<\/p>\n<p><p>\u5728\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u547d\u4ee4\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u3001\u547d\u4ee4\u6267\u884c\u9519\u8bef\u7b49\u3002\u4f7f\u7528<code>try-except<\/code>\u5757\u53ef\u4ee5\u6709\u6548\u5730\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\uff0c\u5e76\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1. \u6355\u83b7\u6267\u884c\u9519\u8bef<\/h4>\n<\/p>\n<p><p><code>subprocess.CalledProcessError<\/code>\u5f02\u5e38\u7528\u4e8e\u6355\u83b7\u547d\u4ee4\u8fd4\u56de\u975e\u96f6\u9000\u51fa\u72b6\u6001\u7684\u60c5\u51b5\u3002\u4f60\u53ef\u4ee5\u5728<code>except<\/code>\u5757\u4e2d\u5904\u7406\u8fd9\u79cd\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_execute_ifconfig():<\/p>\n<p>    try:<\/p>\n<p>        result = subprocess.run([&#39;ifconfig&#39;], capture_output=True, text=True, check=True)<\/p>\n<p>        print(result.stdout)<\/p>\n<p>    except subprocess.CalledProcessError as e:<\/p>\n<p>        print(f&quot;Command f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led with return code {e.returncode}&quot;)<\/p>\n<p>        print(f&quot;Error output: {e.stderr}&quot;)<\/p>\n<p>safe_execute_ifconfig()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6355\u83b7\u5176\u4ed6\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u9664\u4e86<code>CalledProcessError<\/code>\uff0c\u8fd8\u53ef\u80fd\u4f1a\u9047\u5230\u5176\u4ed6\u5f02\u5e38\uff0c\u5982<code>FileNotFoundError<\/code>\uff08\u547d\u4ee4\u4e0d\u5b58\u5728\uff09\u7b49\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def robust_execute_ifconfig():<\/p>\n<p>    try:<\/p>\n<p>        result = subprocess.run([&#39;ifconfig&#39;], capture_output=True, text=True, check=True)<\/p>\n<p>        print(result.stdout)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(&quot;Command not found: ifconfig&quot;)<\/p>\n<p>    except subprocess.CalledProcessError as e:<\/p>\n<p>        print(f&quot;Command failed with return code {e.returncode}&quot;)<\/p>\n<p>        print(f&quot;Error output: {e.stderr}&quot;)<\/p>\n<p>robust_execute_ifconfig()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u66ff\u4ee3\u65b9\u6848\u548c\u517c\u5bb9\u6027\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\uff0c<code>ifconfig<\/code>\u547d\u4ee4\u53ef\u80fd\u4e0d\u53ef\u7528\u6216\u88ab\u66ff\u4ee3\u4e3a\u5176\u4ed6\u5de5\u5177\uff08\u5982<code>ip<\/code>\u547d\u4ee4\uff09\u3002\u4e3a\u4e86\u63d0\u9ad8\u811a\u672c\u7684\u517c\u5bb9\u6027\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>ip<\/code>\u547d\u4ee4<\/h4>\n<\/p>\n<p><p>\u5728\u73b0\u4ee3Linux\u7cfb\u7edf\u4e2d\uff0c<code>ifconfig<\/code>\u53ef\u80fd\u4f1a\u88ab<code>ip<\/code>\u547d\u4ee4\u66ff\u4ee3\u3002\u4f60\u53ef\u4ee5\u4fee\u6539\u811a\u672c\u4ee5\u4f7f\u7528<code>ip<\/code>\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def execute_ip_command():<\/p>\n<p>    try:<\/p>\n<p>        result = subprocess.run([&#39;ip&#39;, &#39;addr&#39;], capture_output=True, text=True, check=True)<\/p>\n<p>        print(result.stdout)<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(&quot;Command not found: ip&quot;)<\/p>\n<p>    except subprocess.CalledProcessError as e:<\/p>\n<p>        print(f&quot;Command failed with return code {e.returncode}&quot;)<\/p>\n<p>        print(f&quot;Error output: {e.stderr}&quot;)<\/p>\n<p>execute_ip_command()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u68c0\u67e5\u547d\u4ee4\u53ef\u7528\u6027<\/h4>\n<\/p>\n<p><p>\u5728\u6267\u884c\u547d\u4ee4\u524d\uff0c\u53ef\u4ee5\u68c0\u67e5\u547d\u4ee4\u662f\u5426\u53ef\u7528\uff0c\u4ee5\u4fbf\u5728\u811a\u672c\u4e2d\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p>def check_command_availability():<\/p>\n<p>    if shutil.which(&#39;ifconfig&#39;):<\/p>\n<p>        print(&quot;Using ifconfig command&quot;)<\/p>\n<p>        execute_ifconfig()<\/p>\n<p>    elif shutil.which(&#39;ip&#39;):<\/p>\n<p>        print(&quot;Using ip command&quot;)<\/p>\n<p>        execute_ip_command()<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;No suitable command found&quot;)<\/p>\n<p>check_command_availability()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7<code>subprocess<\/code>\u6a21\u5757\uff0cPython\u53ef\u4ee5\u65b9\u4fbf\u5730\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\uff0c\u5982<code>ifconfig<\/code>\u3002\u901a\u8fc7\u6355\u83b7\u547d\u4ee4\u8f93\u51fa\u5e76\u5bf9\u5176\u8fdb\u884c\u89e3\u6790\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8bb8\u591a\u5b9e\u7528\u7684\u529f\u80fd\u3002\u5728\u7f16\u5199\u811a\u672c\u65f6\uff0c\u6ce8\u610f\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u4ee5\u63d0\u9ad8\u811a\u672c\u7684\u5065\u58ee\u6027\u548c\u517c\u5bb9\u6027\u3002\u9009\u62e9\u5408\u9002\u7684\u547d\u4ee4\u548c\u5904\u7406\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u9002\u5e94\u4e0d\u540c\u7684\u7cfb\u7edf\u73af\u5883\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u6765\u6267\u884c\u7cfb\u7edf\u547d\u4ee4\uff0c\u5305\u62ec<code>ifconfig<\/code>\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u542f\u52a8\u65b0\u8fdb\u7a0b\u3001\u4e0e\u5176\u4ea4\u4e92\u5e76\u83b7\u53d6\u8f93\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import subprocess\n\nresult = subprocess.run([&#39;ifconfig&#39;], capture_output=True, text=True)\nprint(result.stdout)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u4f1a\u6267\u884c<code>ifconfig<\/code>\u547d\u4ee4\u5e76\u6253\u5370\u51fa\u5176\u8f93\u51fa\u3002<\/p>\n<p><strong>\u6267\u884cifconfig\u547d\u4ee4\u65f6\u5982\u4f55\u5904\u7406\u6743\u9650\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\uff0c\u6267\u884c<code>ifconfig<\/code>\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u3002\u5982\u679c\u5728Python\u811a\u672c\u4e2d\u9047\u5230\u6743\u9650\u4e0d\u8db3\u7684\u9519\u8bef\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>sudo<\/code>\u547d\u4ee4\u3002\u7136\u800c\uff0c\u4f7f\u7528<code>sudo<\/code>\u65f6\u9700\u8981\u6ce8\u610f\u5b89\u5168\u6027\uff0c\u786e\u4fdd\u4e0d\u4f1a\u5728\u672a\u6388\u6743\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u547d\u4ee4\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u89e3\u6790ifconfig\u7684\u8f93\u51fa\uff1f<\/strong><br \/>\u83b7\u53d6<code>ifconfig<\/code>\u7684\u8f93\u51fa\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u89e3\u6790\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u63d0\u53d6IP\u5730\u5740\u548c\u7f51\u7edc\u63a9\u7801\u7b49\u5173\u952e\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\noutput = result.stdout\nip_addresses = re.findall(r&#39;inet (\\d+\\.\\d+\\.\\d+\\.\\d+)&#39;, output)\nprint(ip_addresses)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u4ece<code>ifconfig<\/code>\u7684\u8f93\u51fa\u4e2d\u63d0\u53d6\u51fa\u6240\u6709\u7684IP\u5730\u5740\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u6267\u884cifconfig\u547d\u4ee4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528subprocess\u6a21\u5757\uff0c\u901a\u8fc7\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1004490,"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\/1004476"}],"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=1004476"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004476\/revisions"}],"predecessor-version":[{"id":1004492,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004476\/revisions\/1004492"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1004490"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1004476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1004476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1004476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}