{"id":17570,"date":"2024-11-18T16:57:09","date_gmt":"2024-11-18T08:57:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/17570\/"},"modified":"2024-11-18T16:57:09","modified_gmt":"2024-11-18T08:57:09","slug":"linux%e4%b8%ad%e7%9a%84%e7%ab%af%e5%8f%a3%e5%8d%a0%e7%94%a8%e6%a3%80%e6%9f%a5%e6%93%8d%e4%bd%9c%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/17570\/","title":{"rendered":"Linux\u4e2d\u7684\u7aef\u53e3\u5360\u7528\u68c0\u67e5\u64cd\u4f5c\u8be6\u89e3"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/170926615568706.jpg\" class=\"aligncenter\" title=\"Linux\u4e2d\u7684\u7aef\u53e3\u5360\u7528\u68c0\u67e5\u64cd\u4f5c\u8be6\u89e3\u63d2\u56fe\" alt=\"Linux\u4e2d\u7684\u7aef\u53e3\u5360\u7528\u68c0\u67e5\u64cd\u4f5c\u8be6\u89e3\u63d2\u56fe\" \/><\/p>\n<p>Linux\u4e2d\u7684\u7aef\u53e3\u5360\u7528\u68c0\u67e5\u64cd\u4f5c\u8be6\u89e3<\/p>\n<p>\u5728Linux\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u7f51\u7edc\u901a\u4fe1\u662f\u901a\u8fc7\u7aef\u53e3\u6765\u5b9e\u73b0\u7684\uff0c\u4e0d\u540c\u7684\u7aef\u53e3\u5bf9\u5e94\u7740\u4e0d\u540c\u7684\u7f51\u7edc\u670d\u52a1\u6216\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u5f00\u53d1\u6216\u8fd0\u7ef4\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u6c42\u8981\u68c0\u67e5\u67d0\u4e2a\u7aef\u53e3\u662f\u5426\u88ab\u5360\u7528\uff0c\u4ee5\u786e\u4fdd\u670d\u52a1\u80fd\u591f\u6b63\u5e38\u542f\u52a8\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5728Linux\u73af\u5883\u4e2d\u5982\u4f55\u8fdb\u884c\u7aef\u53e3\u5360\u7528\u68c0\u67e5\u64cd\u4f5c\uff0c\u5305\u62ec\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u548c\u7f16\u7a0b\u8bed\u8a00\u7f16\u5199\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u68c0\u67e5\u7aef\u53e3\u5360\u7528\u60c5\u51b5<\/p>\n<ol>\n<li>netstat\u547d\u4ee4<\/li>\n<\/ol>\n<p>netstat\u662f\u4e00\u4e2a\u7528\u6765\u663e\u793a\u7f51\u7edc\u72b6\u6001\u4fe1\u606f\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u67e5\u770b\u7cfb\u7edf\u4e2d\u5f00\u653e\u7684\u7aef\u53e3\u4ee5\u53ca\u5bf9\u5e94\u7684\u8fdb\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9bnetstat\u547d\u4ee4\u7684\u5e38\u7528\u9009\u9879\uff1a<\/p>\n<ul>\n<li>-t\uff1a\u663e\u793aTCP\u7aef\u53e3<\/li>\n<li>-u\uff1a\u663e\u793aUDP\u7aef\u53e3<\/li>\n<li>-n\uff1a\u663e\u793a\u6570\u5b57\u683c\u5f0f\uff08\u4e0d\u89e3\u6790\u57df\u540d\uff09<\/li>\n<li>-p\uff1a\u663e\u793a\u8fdb\u7a0b\u6807\u8bc6\u7b26\u548c\u8fdb\u7a0b\u540d\u79f0<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e9b\u9009\u9879\uff0c\u53ef\u4ee5\u7528\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u7cfb\u7edf\u4e2d\u6240\u6709\u7684\u76d1\u542c\u7aef\u53e3\u53ca\u5bf9\u5e94\u7684\u8fdb\u7a0b\uff1a<\/p>\n<pre>netstat -tulnp<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>ss\u547d\u4ee4<\/li>\n<\/ol>\n<p>ss\u662fnetstat\u7684\u66ff\u4ee3\u5de5\u5177\uff0c\u6027\u80fd\u66f4\u597d\uff0c\u7528\u6cd5\u4e5f\u66f4\u7b80\u6d01\u3002\u4e0enetstat\u7c7b\u4f3c\uff0css\u4e5f\u53ef\u4ee5\u67e5\u770b\u7aef\u53e3\u5360\u7528\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5e38\u7528\u7684ss\u547d\u4ee4\u793a\u4f8b\uff1a<\/p>\n<pre>ss -tlnp<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6761\u547d\u4ee4\u4f1a\u663e\u793a\u7cfb\u7edf\u4e2d\u6240\u6709\u5904\u4e8e\u76d1\u542c\u72b6\u6001\u7684TCP\u7aef\u53e3\u53ca\u5bf9\u5e94\u7684\u8fdb\u7a0b\u3002<\/p>\n<p>\u4e8c\u3001\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u7f16\u5199\u4ee3\u7801\u68c0\u67e5\u7aef\u53e3\u5360\u7528\u60c5\u51b5<\/p>\n<p>\u9664\u4e86\u547d\u4ee4\u884c\u5de5\u5177\u5916\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u6765\u7f16\u5199\u4ee3\u7801\u6765\u68c0\u67e5\u7aef\u53e3\u5360\u7528\u60c5\u51b5\u3002\u4e0b\u9762\u4ee5Python\u4e3a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u901a\u8fc7\u7f16\u7a0b\u8bed\u8a00\u67e5\u770b\u7aef\u53e3\u5360\u7528\u60c5\u51b5\u3002<\/p>\n<pre>import socket\n\ndef check_port(port):\n    try:\n        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n        s.bind((\"0.0.0.0\", port))\n        s.close()\n        return True\n    except Exception as e:\n        return False\n\nport_to_check = 8080\nif check_port(port_to_check):\n    print(f\"\u7aef\u53e3 {port_to_check} \u53ef\u7528\")\nelse:\n    print(f\"\u7aef\u53e3 {port_to_check} \u5df2\u88ab\u5360\u7528\")<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c\uff0c\u901a\u8fc7\u521b\u5efa\u4e00\u4e2asocket\u8fde\u63a5\u6765\u68c0\u67e5\u6307\u5b9a\u7aef\u53e3\u662f\u5426\u88ab\u5360\u7528\u3002\u901a\u8fc7\u8c03\u7528check_port\u51fd\u6570\uff0c\u5e76\u4f20\u5165\u9700\u8981\u68c0\u67e5\u7684\u7aef\u53e3\u53f7\uff0c\u5373\u53ef\u83b7\u53d6\u5230\u8be5\u7aef\u53e3\u7684\u5360\u7528\u60c5\u51b5\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<\/p>\n<p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Linux\u73af\u5883\u4e2d\u5982\u4f55\u901a\u8fc7\u547d\u4ee4\u884c\u5de5\u5177\u548c\u7f16\u7a0b\u8bed\u8a00\u6765\u68c0\u67e5\u7aef\u53e3\u5360\u7528\u60c5\u51b5\u3002\u901a\u8fc7\u4f7f\u7528netstat\u3001ss\u547d\u4ee4\u6216\u7f16\u5199Python\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u53d6\u5230\u7aef\u53e3\u5360\u7528\u60c5\u51b5\uff0c\u5e2e\u52a9\u6211\u4eec\u786e\u4fdd\u7cfb\u7edf\u670d\u52a1\u7684\u6b63\u5e38\u542f\u52a8\u548c\u8fd0\u884c\u3002\u5e0c\u671b\u4ee5\u4e0a\u5185\u5bb9\u5bf9\u8bfb\u8005\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLinux\u4e2d\u7684\u7aef\u53e3\u5360\u7528\u68c0\u67e5\u64cd\u4f5c\u8be6\u89e3\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>Linux\u4e2d\u7684\u7aef\u53e3\u5360\u7528\u68c0\u67e5\u64cd\u4f5c\u8be6\u89e3 \u5728Linux\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u7f51\u7edc\u901a\u4fe1\u662f\u901a\u8fc7\u7aef\u53e3\u6765\u5b9e\u73b0\u7684\uff0c\u4e0d\u540c\u7684\u7aef\u53e3\u5bf9\u5e94\u7740\u4e0d\u540c\u7684\u7f51\u7edc\u670d\u52a1\u6216\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u5f00\u53d1\u6216\u8fd0\u7ef4\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u6c42\u8981\u68c0\u67e5\u67d0\u4e2a\u7aef\u53e3\u662f\u5426\u88ab\u5360\u7528\uff0c\u4ee5\u786e\u4fdd\u670d\u52a1\u80fd\u591f\u6b63\u5e38\u542f\u52a8\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5728Linux\u73af\u5883\u4e2d\u5982\u4f55\u8fdb\u884c\u7aef\u53e3\u5360\u7528\u68c0\u67e5\u64cd\u4f5c\uff0c\u5305\u62ec\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u548c\u7f16\u7a0b\u8bed\u8a00\u7f16\u5199\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u68c0\u67e5\u7aef\u53e3\u5360\u7528\u60c5\u51b5 netstat\u547d\u4ee4 netstat\u662f\u4e00\u4e2a\u7528\u6765\u663e\u793a\u7f51\u7edc\u72b6\u6001\u4fe1\u606f\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u67e5\u770b\u7cfb\u7edf\u4e2d\u5f00\u653e\u7684\u7aef\u53e3\u4ee5\u53ca\u5bf9\u5e94\u7684\u8fdb\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9bnetstat\u547d\u4ee4\u7684\u5e38\u7528\u9009\u9879\uff1a -t\uff1a\u663e\u793aTCP\u7aef\u53e3 -u\uff1a\u663e\u793aUDP\u7aef\u53e3 -n\uff1a\u663e\u793a\u6570\u5b57\u683c\u5f0f\uff08\u4e0d\u89e3\u6790\u57df\u540d\uff09 -p\uff1a\u663e\u793a\u8fdb\u7a0b\u6807\u8bc6\u7b26\u548c\u8fdb\u7a0b\u540d\u79f0 \u901a\u8fc7\u7ed3\u5408\u8fd9\u4e9b\u9009\u9879\uff0c\u53ef\u4ee5\u7528\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u7cfb\u7edf\u4e2d\u6240\u6709\u7684\u76d1\u542c\u7aef\u53e3\u53ca\u5bf9\u5e94\u7684\u8fdb\u7a0b\uff1a netstat -tulnp \u767b\u5f55\u540e\u590d\u5236 ss\u547d\u4ee4 ss\u662fnetstat\u7684\u66ff\u4ee3\u5de5\u5177\uff0c\u6027\u80fd\u66f4\u597d\uff0c\u7528\u6cd5\u4e5f\u66f4\u7b80\u6d01\u3002\u4e0enetstat\u7c7b\u4f3c\uff0css\u4e5f\u53ef\u4ee5\u67e5\u770b\u7aef\u53e3\u5360\u7528\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5e38\u7528\u7684ss\u547d\u4ee4\u793a\u4f8b\uff1a ss -tlnp \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6761\u547d\u4ee4\u4f1a\u663e\u793a\u7cfb\u7edf\u4e2d\u6240\u6709\u5904\u4e8e\u76d1\u542c\u72b6\u6001\u7684TCP\u7aef\u53e3\u53ca\u5bf9\u5e94\u7684\u8fdb\u7a0b\u3002 \u4e8c\u3001\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u7f16\u5199\u4ee3\u7801\u68c0\u67e5\u7aef\u53e3\u5360\u7528\u60c5\u51b5 \u9664\u4e86\u547d\u4ee4\u884c\u5de5\u5177\u5916\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u6765\u7f16\u5199\u4ee3\u7801\u6765\u68c0\u67e5\u7aef\u53e3\u5360\u7528\u60c5\u51b5\u3002\u4e0b\u9762\u4ee5Python\u4e3a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u901a\u8fc7\u7f16\u7a0b\u8bed\u8a00\u67e5\u770b\u7aef\u53e3\u5360\u7528\u60c5\u51b5\u3002 import socket def check_port(port): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((&#8220;0.0.0.0&#8243;, port)) s.close() return True except Exception as e: return False port_to_check = 8080 if check_port(port_to_check): print(f&#8221;\u7aef\u53e3 {port_to_check} \u53ef\u7528&#8221;) else: print(f&#8221;\u7aef\u53e3 {port_to_check} \u5df2\u88ab\u5360\u7528&#8221;) \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c\uff0c\u901a\u8fc7\u521b\u5efa\u4e00\u4e2asocket\u8fde\u63a5\u6765\u68c0\u67e5\u6307\u5b9a\u7aef\u53e3\u662f\u5426\u88ab\u5360\u7528\u3002\u901a\u8fc7\u8c03\u7528check_port\u51fd\u6570\uff0c\u5e76\u4f20\u5165\u9700\u8981\u68c0\u67e5\u7684\u7aef\u53e3\u53f7\uff0c\u5373\u53ef\u83b7\u53d6\u5230\u8be5\u7aef\u53e3\u7684\u5360\u7528\u60c5\u51b5\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-17570","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17570","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=17570"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17570\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=17570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=17570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=17570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}