{"id":17568,"date":"2024-11-18T14:38:27","date_gmt":"2024-11-18T06:38:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/17568\/"},"modified":"2024-11-18T14:38:27","modified_gmt":"2024-11-18T06:38:27","slug":"%e4%ba%86%e8%a7%a3-linux-linkwatch-%e8%bf%9b%e7%a8%8b%e7%9a%84%e5%b7%a5%e4%bd%9c%e6%9c%ba%e5%88%b6%e5%92%8c%e5%8a%9f%e8%83%bd%e7%89%b9%e7%82%b9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/17568\/","title":{"rendered":"\u4e86\u89e3 Linux linkwatch \u8fdb\u7a0b\u7684\u5de5\u4f5c\u673a\u5236\u548c\u529f\u80fd\u7279\u70b9"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/170926488813322.jpg\" class=\"aligncenter\" title=\"\u4e86\u89e3 Linux linkwatch \u8fdb\u7a0b\u7684\u5de5\u4f5c\u673a\u5236\u548c\u529f\u80fd\u7279\u70b9\u63d2\u56fe\" alt=\"\u4e86\u89e3 Linux linkwatch \u8fdb\u7a0b\u7684\u5de5\u4f5c\u673a\u5236\u548c\u529f\u80fd\u7279\u70b9\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4e86\u89e3 Linux linkwatch \u8fdb\u7a0b\u7684\u5de5\u4f5c\u673a\u5236\u548c\u529f\u80fd\u7279\u70b9<\/strong><\/p>\n<p>\u5728 Linux \u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0clinkwatch \u8fdb\u7a0b\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5b88\u62a4\u8fdb\u7a0b\uff0c\u5176\u4e3b\u8981\u4f5c\u7528\u662f\u76d1\u63a7\u7f51\u7edc\u63a5\u53e3\u7684\u72b6\u6001\u53d8\u5316\uff0c\u5982\u7f51\u7edc\u8fde\u63a5\u7684\u5efa\u7acb\u548c\u65ad\u5f00\u3002linkwatch \u8fdb\u7a0b\u901a\u8fc7\u76d1\u542c Linux \u5185\u6838\u4e2d\u7684 NETLINK_ROUTE \u5957\u63a5\u5b57\uff0c\u5b9e\u65f6\u83b7\u53d6\u7f51\u7edc\u63a5\u53e3\u7684\u72b6\u6001\u4fe1\u606f\uff0c\u5e76\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<h3>\u5de5\u4f5c\u673a\u5236<\/h3>\n<p>linkwatch \u8fdb\u7a0b\u7684\u5de5\u4f5c\u673a\u5236\u53ef\u4ee5\u7b80\u5355\u63cf\u8ff0\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>linkwatch \u8fdb\u7a0b\u901a\u8fc7 NETLINK_ROUTE \u5957\u63a5\u5b57\u8ba2\u9605\u5bf9\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d8\u5316\u7684\u901a\u77e5\u6d88\u606f\u3002<\/li>\n<li>\u5f53\u6709\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5185\u6838\u5c06\u76f8\u5173\u901a\u77e5\u6d88\u606f\u53d1\u9001\u5230 NETLINK_ROUTE \u5957\u63a5\u5b57\uff0clinkwatch \u8fdb\u7a0b\u63a5\u6536\u5230\u6d88\u606f\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/li>\n<li>linkwatch \u8fdb\u7a0b\u6839\u636e\u63a5\u6536\u5230\u7684\u6d88\u606f\u5185\u5bb9\uff0c\u5224\u65ad\u662f\u7f51\u7edc\u63a5\u53e3\u7684\u8fde\u63a5\u5efa\u7acb\u8fd8\u662f\u65ad\u5f00\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u5982\u66f4\u65b0\u8def\u7531\u8868\u3001\u901a\u77e5\u5176\u4ed6\u8fdb\u7a0b\u7b49\u3002<\/li>\n<\/ol>\n<h3>\u529f\u80fd\u7279\u70b9<\/h3>\n<p>linkwatch \u8fdb\u7a0b\u7684\u529f\u80fd\u4e3b\u8981\u4f53\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n<ol>\n<li>\u5b9e\u65f6\u76d1\u63a7\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\uff1alinkwatch \u8fdb\u7a0b\u53ef\u4ee5\u53ca\u65f6\u611f\u77e5\u5230\u7f51\u7edc\u8fde\u63a5\u7684\u53d8\u5316\uff0c\u4fdd\u8bc1\u7cfb\u7edf\u5728\u7f51\u7edc\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\u80fd\u591f\u505a\u51fa\u54cd\u5e94\u3002<\/li>\n<li>\u66f4\u65b0\u8def\u7531\u8868\uff1a\u5f53\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\uff0clinkwatch \u8fdb\u7a0b\u53ef\u4ee5\u6839\u636e\u89c4\u5219\u66f4\u65b0\u7cfb\u7edf\u7684\u8def\u7531\u8868\uff0c\u786e\u4fdd\u6570\u636e\u5305\u53ef\u4ee5\u6b63\u5e38\u4f20\u8f93\u3002<\/li>\n<li>\u901a\u77e5\u5176\u4ed6\u8fdb\u7a0b\uff1alinkwatch \u8fdb\u7a0b\u53ef\u4ee5\u5c06\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d8\u5316\u7684\u4fe1\u606f\u901a\u77e5\u7ed9\u5176\u4ed6\u76f8\u5173\u7684\u8fdb\u7a0b\uff0c\u4ee5\u4fbf\u5b83\u4eec\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/li>\n<\/ol>\n<h3>\u4ee3\u7801\u793a\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u4e3a\u4e00\u4e2a\u7b80\u5355\u7684 Python \u4ee3\u7801\u793a\u4f8b\uff0c\u6a21\u62df linkwatch \u8fdb\u7a0b\u63a5\u6536\u5230\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d8\u5316\u7684\u901a\u77e5\u6d88\u606f\u5e76\u8fdb\u884c\u5904\u7406\uff1a<\/p>\n<pre>import socket\nimport struct\n\ndef receive_linkwatch_notification():\n    linkwatch_socket = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, socket.NETLINK_ROUTE)\n    linkwatch_socket.bind((0, 0))\n\n    while True:\n        data = linkwatch_socket.recv(65535)\n        msg_type, msg_len, flags, seq, pid = struct.unpack(\"=IHHII\", data[:16])\n\n        if msg_type == 16:  # RTM_NEWLINK or RTM_DELLINK\n            print(\"Received link status change notification.\")\n            # Handle the link status change here...\n\nif __name__ == \"__main__\":\n    receive_linkwatch_notification()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a AF_NETLINK \u7c7b\u578b\u7684\u5957\u63a5\u5b57\uff0c\u7ed1\u5b9a\u5230 NETLINK_ROUTE \u5957\u63a5\u5b57\u4e0a\uff0c\u5e76\u901a\u8fc7\u5faa\u73af\u6765\u63a5\u6536 linkwatch \u8fdb\u7a0b\u7684\u901a\u77e5\u6d88\u606f\u3002\u6839\u636e\u63a5\u6536\u5230\u7684\u6d88\u606f\u7c7b\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5904\u7406\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u7684\u6539\u53d8\u3002<\/p>\n<p>\u901a\u8fc7\u8fd9\u6837\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u76f4\u89c2\u5730\u4e86\u89e3 linkwatch \u8fdb\u7a0b\u7684\u5de5\u4f5c\u673a\u5236\u548c\u5b9e\u73b0\u65b9\u5f0f\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e86\u89e3 Linux linkwatch \u8fdb\u7a0b\u7684\u5de5\u4f5c\u673a\u5236\u548c\u529f\u80fd\u7279\u70b9\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>\u4e86\u89e3 Linux linkwatch \u8fdb\u7a0b\u7684\u5de5\u4f5c\u673a\u5236\u548c\u529f\u80fd\u7279\u70b9 \u5728 Linux \u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0clinkwatch \u8fdb\u7a0b\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5b88\u62a4\u8fdb\u7a0b\uff0c\u5176\u4e3b\u8981\u4f5c\u7528\u662f\u76d1\u63a7\u7f51\u7edc\u63a5\u53e3\u7684\u72b6\u6001\u53d8\u5316\uff0c\u5982\u7f51\u7edc\u8fde\u63a5\u7684\u5efa\u7acb\u548c\u65ad\u5f00\u3002linkwatch \u8fdb\u7a0b\u901a\u8fc7\u76d1\u542c Linux \u5185\u6838\u4e2d\u7684 NETLINK_ROUTE \u5957\u63a5\u5b57\uff0c\u5b9e\u65f6\u83b7\u53d6\u7f51\u7edc\u63a5\u53e3\u7684\u72b6\u6001\u4fe1\u606f\uff0c\u5e76\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002 \u5de5\u4f5c\u673a\u5236 linkwatch \u8fdb\u7a0b\u7684\u5de5\u4f5c\u673a\u5236\u53ef\u4ee5\u7b80\u5355\u63cf\u8ff0\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a linkwatch \u8fdb\u7a0b\u901a\u8fc7 NETLINK_ROUTE \u5957\u63a5\u5b57\u8ba2\u9605\u5bf9\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d8\u5316\u7684\u901a\u77e5\u6d88\u606f\u3002 \u5f53\u6709\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5185\u6838\u5c06\u76f8\u5173\u901a\u77e5\u6d88\u606f\u53d1\u9001\u5230 NETLINK_ROUTE \u5957\u63a5\u5b57\uff0clinkwatch \u8fdb\u7a0b\u63a5\u6536\u5230\u6d88\u606f\u5e76\u8fdb\u884c\u5904\u7406\u3002 linkwatch \u8fdb\u7a0b\u6839\u636e\u63a5\u6536\u5230\u7684\u6d88\u606f\u5185\u5bb9\uff0c\u5224\u65ad\u662f\u7f51\u7edc\u63a5\u53e3\u7684\u8fde\u63a5\u5efa\u7acb\u8fd8\u662f\u65ad\u5f00\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u5982\u66f4\u65b0\u8def\u7531\u8868\u3001\u901a\u77e5\u5176\u4ed6\u8fdb\u7a0b\u7b49\u3002 \u529f\u80fd\u7279\u70b9 linkwatch \u8fdb\u7a0b\u7684\u529f\u80fd\u4e3b\u8981\u4f53\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a \u5b9e\u65f6\u76d1\u63a7\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\uff1alinkwatch \u8fdb\u7a0b\u53ef\u4ee5\u53ca\u65f6\u611f\u77e5\u5230\u7f51\u7edc\u8fde\u63a5\u7684\u53d8\u5316\uff0c\u4fdd\u8bc1\u7cfb\u7edf\u5728\u7f51\u7edc\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\u80fd\u591f\u505a\u51fa\u54cd\u5e94\u3002 \u66f4\u65b0\u8def\u7531\u8868\uff1a\u5f53\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\uff0clinkwatch \u8fdb\u7a0b\u53ef\u4ee5\u6839\u636e\u89c4\u5219\u66f4\u65b0\u7cfb\u7edf\u7684\u8def\u7531\u8868\uff0c\u786e\u4fdd\u6570\u636e\u5305\u53ef\u4ee5\u6b63\u5e38\u4f20\u8f93\u3002 \u901a\u77e5\u5176\u4ed6\u8fdb\u7a0b\uff1alinkwatch \u8fdb\u7a0b\u53ef\u4ee5\u5c06\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d8\u5316\u7684\u4fe1\u606f\u901a\u77e5\u7ed9\u5176\u4ed6\u76f8\u5173\u7684\u8fdb\u7a0b\uff0c\u4ee5\u4fbf\u5b83\u4eec\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002 \u4ee3\u7801\u793a\u4f8b \u4ee5\u4e0b\u4e3a\u4e00\u4e2a\u7b80\u5355\u7684 Python \u4ee3\u7801\u793a\u4f8b\uff0c\u6a21\u62df linkwatch \u8fdb\u7a0b\u63a5\u6536\u5230\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d8\u5316\u7684\u901a\u77e5\u6d88\u606f\u5e76\u8fdb\u884c\u5904\u7406\uff1a import socket import struct def receive_linkwatch_notification(): linkwatch_socket = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, socket.NETLINK_ROUTE) linkwatch_socket.bind((0, 0)) while [&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-17568","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17568","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=17568"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17568\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=17568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=17568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=17568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}