{"id":17580,"date":"2024-11-18T08:46:11","date_gmt":"2024-11-18T00:46:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/17580\/"},"modified":"2024-11-18T08:46:11","modified_gmt":"2024-11-18T00:46:11","slug":"%e5%88%86%e6%9e%90-linux-linkwatch-%e8%bf%9b%e7%a8%8b%e7%9a%84%e4%bd%9c%e7%94%a8%e5%92%8c%e8%bf%90%e8%a1%8c%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/17580\/","title":{"rendered":"\u5206\u6790 Linux linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\u548c\u8fd0\u884c\u673a\u5236"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/170927838953067.jpg\" class=\"aligncenter\" title=\"\u5206\u6790 Linux linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\u548c\u8fd0\u884c\u673a\u5236\u63d2\u56fe\" alt=\"\u5206\u6790 Linux linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\u548c\u8fd0\u884c\u673a\u5236\u63d2\u56fe\" \/><\/p>\n<p>Linux linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\u548c\u8fd0\u884c\u673a\u5236<\/p>\n<p>\u5728Linux\u7cfb\u7edf\u4e2d\uff0clinkwatch \u8fdb\u7a0b\u662f\u4e00\u4e2a\u8d1f\u8d23\u76d1\u6d4b\u7f51\u7edc\u8fde\u63a5\u72b6\u6001\u7684\u5b88\u62a4\u8fdb\u7a0b\uff0c\u5176\u4e3b\u8981\u4f5c\u7528\u662f\u5728\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u6539\u53d8\u65f6\uff0c\u5373\u8fde\u63a5\u6216\u65ad\u5f00\u65f6\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002linkwatch \u8fdb\u7a0b\u901a\u8fc7\u76d1\u542c\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d8\u5316\u6765\u5b9e\u73b0\u5bf9\u7f51\u7edc\u8fde\u63a5\u7684\u5b9e\u65f6\u76d1\u63a7\uff0c\u5e76\u6839\u636e\u72b6\u6001\u53d8\u5316\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<h2>linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\uff1a<\/h2>\n<ol>\n<li>\u76d1\u6d4b\u7f51\u7edc\u8fde\u63a5\u72b6\u6001\uff1alinkwatch \u8fdb\u7a0b\u4f1a\u6301\u7eed\u76d1\u542c\u7f51\u7edc\u63a5\u53e3\u7684\u72b6\u6001\u53d8\u5316\uff0c\u5305\u62ec\u7f51\u7edc\u8fde\u63a5\u7684\u5efa\u7acb\u3001\u65ad\u5f00\u4ee5\u53ca\u91cd\u65b0\u8fde\u63a5\u7b49\u60c5\u51b5\u3002<\/li>\n<li>\u5904\u7406\u7f51\u7edc\u8fde\u63a5\u4e8b\u4ef6\uff1a\u5728\u63a5\u6536\u5230\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d8\u5316\u7684\u901a\u77e5\u540e\uff0clinkwatch \u8fdb\u7a0b\u4f1a\u6839\u636e\u5177\u4f53\u7684\u4e8b\u4ef6\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\uff0c\u6bd4\u5982\u91cd\u65b0\u914d\u7f6e\u7f51\u7edc\u53c2\u6570\u3001\u89e6\u53d1\u5176\u4ed6\u670d\u52a1\u6216\u811a\u672c\u7684\u6267\u884c\u7b49\u3002<\/li>\n<li>\u63d0\u4f9b\u5b9e\u65f6\u7f51\u7edc\u72b6\u6001\u4fe1\u606f\uff1alinkwatch \u8fdb\u7a0b\u53ef\u4ee5\u5411\u7cfb\u7edf\u7ba1\u7406\u5458\u63d0\u4f9b\u5b9e\u65f6\u7684\u7f51\u7edc\u72b6\u6001\u4fe1\u606f\uff0c\u5e2e\u52a9\u7ba1\u7406\u5458\u53ca\u65f6\u4e86\u89e3\u7f51\u7edc\u8fde\u63a5\u7684\u60c5\u51b5\u5e76\u53ca\u65f6\u505a\u51fa\u8c03\u6574\u3002<\/li>\n<\/ol>\n<h2>linkwatch \u8fdb\u7a0b\u7684\u8fd0\u884c\u673a\u5236\uff1a<\/h2>\n<p>linkwatch \u8fdb\u7a0b\u901a\u8fc7\u76d1\u542c\u5185\u6838\u7684 Netlink socket \u5b9e\u73b0\u5bf9\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u7684\u5b9e\u65f6\u76d1\u63a7\u3002\u5f53\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5185\u6838\u4f1a\u53d1\u9001\u76f8\u5e94\u7684\u901a\u77e5\u6d88\u606f\u5230 linkwatch \u8fdb\u7a0b\uff0c\u901a\u77e5 linkwatch \u8fdb\u7a0b\u6709\u5173\u8fde\u63a5\u53d8\u5316\u7684\u5177\u4f53\u4fe1\u606f\u3002linkwatch \u8fdb\u7a0b\u63a5\u6536\u5230\u901a\u77e5\u540e\uff0c\u4fbf\u6839\u636e\u5177\u4f53\u7684\u4e8b\u4ef6\u7c7b\u578b\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Python \u7f16\u5199\u4e00\u4e2a\u6a21\u62df linkwatch \u8fdb\u7a0b\u7684\u7a0b\u5e8f\uff0c\u5b9e\u73b0\u5bf9\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u7684\u76d1\u63a7\u5e76\u8f93\u51fa\u72b6\u6001\u4fe1\u606f\uff1a<\/p>\n<pre>import socket\nimport struct\n\n# \u521b\u5efa Netlink socket\uff0c\u7528\u4e8e\u63a5\u6536\u5185\u6838\u7684\u901a\u77e5\nsock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, socket.NETLINK_ROUTE)\nsock.bind((0, 0))\n\n# \u76d1\u542c\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d8\u5316\nwhile True:\n    data = sock.recv(65535)\n    _, _, _, _, msg_type, flags, seq, pid = struct.unpack(\"=IHHHBBII\", data[:20])\n\n    if msg_type == 16:  # RTM_NEWLINK\n        print(\"Network interface state changed: connected\")\n    elif msg_type == 17:  # RTM_DELLINK\n        print(\"Network interface state changed: disconnected\")<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u4ee3\u7801\u793a\u4f8b\u6a21\u62df\u4e86 linkwatch \u8fdb\u7a0b\u7684\u57fa\u672c\u529f\u80fd\uff0c\u901a\u8fc7\u76d1\u542c Netlink socket \u63a5\u6536\u5230\u7684\u6d88\u606f\uff0c\u5224\u65ad\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u7684\u53d8\u5316\u5e76\u8f93\u51fa\u76f8\u5e94\u7684\u4fe1\u606f\u3002\u5b9e\u9645 linkwatch \u8fdb\u7a0b\u7684\u5b9e\u73b0\u4f1a\u66f4\u52a0\u590d\u6742\uff0c\u6d89\u53ca\u5230\u66f4\u591a\u7684\u7f51\u7edc\u72b6\u6001\u5904\u7406\u548c\u7ba1\u7406\u673a\u5236\u3002<\/p>\n<p>\u603b\u800c\u8a00\u4e4b\uff0cLinux linkwatch \u8fdb\u7a0b\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u7f51\u7edc\u8fde\u63a5\u76d1\u63a7\u5de5\u5177\uff0c\u901a\u8fc7\u5b9e\u65f6\u76d1\u542c\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d8\u5316\uff0c\u5e2e\u52a9\u7cfb\u7edf\u7ba1\u7406\u5458\u8ffd\u8e2a\u7f51\u7edc\u8fde\u63a5\u60c5\u51b5\uff0c\u4fdd\u8bc1\u7f51\u7edc\u7684\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5206\u6790 Linux linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\u548c\u8fd0\u884c\u673a\u5236\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 linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\u548c\u8fd0\u884c\u673a\u5236 \u5728Linux\u7cfb\u7edf\u4e2d\uff0clinkwatch \u8fdb\u7a0b\u662f\u4e00\u4e2a\u8d1f\u8d23\u76d1\u6d4b\u7f51\u7edc\u8fde\u63a5\u72b6\u6001\u7684\u5b88\u62a4\u8fdb\u7a0b\uff0c\u5176\u4e3b\u8981\u4f5c\u7528\u662f\u5728\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u6539\u53d8\u65f6\uff0c\u5373\u8fde\u63a5\u6216\u65ad\u5f00\u65f6\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002linkwatch \u8fdb\u7a0b\u901a\u8fc7\u76d1\u542c\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d8\u5316\u6765\u5b9e\u73b0\u5bf9\u7f51\u7edc\u8fde\u63a5\u7684\u5b9e\u65f6\u76d1\u63a7\uff0c\u5e76\u6839\u636e\u72b6\u6001\u53d8\u5316\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002 linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\uff1a \u76d1\u6d4b\u7f51\u7edc\u8fde\u63a5\u72b6\u6001\uff1alinkwatch \u8fdb\u7a0b\u4f1a\u6301\u7eed\u76d1\u542c\u7f51\u7edc\u63a5\u53e3\u7684\u72b6\u6001\u53d8\u5316\uff0c\u5305\u62ec\u7f51\u7edc\u8fde\u63a5\u7684\u5efa\u7acb\u3001\u65ad\u5f00\u4ee5\u53ca\u91cd\u65b0\u8fde\u63a5\u7b49\u60c5\u51b5\u3002 \u5904\u7406\u7f51\u7edc\u8fde\u63a5\u4e8b\u4ef6\uff1a\u5728\u63a5\u6536\u5230\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d8\u5316\u7684\u901a\u77e5\u540e\uff0clinkwatch \u8fdb\u7a0b\u4f1a\u6839\u636e\u5177\u4f53\u7684\u4e8b\u4ef6\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\uff0c\u6bd4\u5982\u91cd\u65b0\u914d\u7f6e\u7f51\u7edc\u53c2\u6570\u3001\u89e6\u53d1\u5176\u4ed6\u670d\u52a1\u6216\u811a\u672c\u7684\u6267\u884c\u7b49\u3002 \u63d0\u4f9b\u5b9e\u65f6\u7f51\u7edc\u72b6\u6001\u4fe1\u606f\uff1alinkwatch \u8fdb\u7a0b\u53ef\u4ee5\u5411\u7cfb\u7edf\u7ba1\u7406\u5458\u63d0\u4f9b\u5b9e\u65f6\u7684\u7f51\u7edc\u72b6\u6001\u4fe1\u606f\uff0c\u5e2e\u52a9\u7ba1\u7406\u5458\u53ca\u65f6\u4e86\u89e3\u7f51\u7edc\u8fde\u63a5\u7684\u60c5\u51b5\u5e76\u53ca\u65f6\u505a\u51fa\u8c03\u6574\u3002 linkwatch \u8fdb\u7a0b\u7684\u8fd0\u884c\u673a\u5236\uff1a linkwatch \u8fdb\u7a0b\u901a\u8fc7\u76d1\u542c\u5185\u6838\u7684 Netlink socket \u5b9e\u73b0\u5bf9\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u7684\u5b9e\u65f6\u76d1\u63a7\u3002\u5f53\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5185\u6838\u4f1a\u53d1\u9001\u76f8\u5e94\u7684\u901a\u77e5\u6d88\u606f\u5230 linkwatch \u8fdb\u7a0b\uff0c\u901a\u77e5 linkwatch \u8fdb\u7a0b\u6709\u5173\u8fde\u63a5\u53d8\u5316\u7684\u5177\u4f53\u4fe1\u606f\u3002linkwatch \u8fdb\u7a0b\u63a5\u6536\u5230\u901a\u77e5\u540e\uff0c\u4fbf\u6839\u636e\u5177\u4f53\u7684\u4e8b\u4ef6\u7c7b\u578b\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Python \u7f16\u5199\u4e00\u4e2a\u6a21\u62df linkwatch \u8fdb\u7a0b\u7684\u7a0b\u5e8f\uff0c\u5b9e\u73b0\u5bf9\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u7684\u76d1\u63a7\u5e76\u8f93\u51fa\u72b6\u6001\u4fe1\u606f\uff1a import socket import struct # \u521b\u5efa Netlink socket\uff0c\u7528\u4e8e\u63a5\u6536\u5185\u6838\u7684\u901a\u77e5 sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, socket.NETLINK_ROUTE) sock.bind((0, 0)) # \u76d1\u542c\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d8\u5316 while True: data = sock.recv(65535) _, _, [&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-17580","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17580","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=17580"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17580\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=17580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=17580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=17580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}