{"id":51524,"date":"2024-12-03T15:05:39","date_gmt":"2024-12-03T07:05:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/51524\/"},"modified":"2024-12-03T15:05:39","modified_gmt":"2024-12-03T07:05:39","slug":"%e4%ba%86%e8%a7%a3-linux-%e4%b8%ad-linkwatch-%e8%bf%9b%e7%a8%8b%e7%9a%84%e5%8a%9f%e8%83%bd%e5%92%8c%e5%b7%a5%e4%bd%9c%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/51524\/","title":{"rendered":"\u4e86\u89e3 Linux \u4e2d linkwatch \u8fdb\u7a0b\u7684\u529f\u80fd\u548c\u5de5\u4f5c\u539f\u7406"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4e86\u89e3 Linux \u4e2d linkwatch \u8fdb\u7a0b\u7684\u529f\u80fd\u548c\u5de5\u4f5c\u539f\u7406<\/h1>\n<p>\u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u7c73\u4e91\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u4e86\u89e3 Linux \u4e2d linkwatch \u8fdb\u7a0b\u7684\u529f\u80fd\u548c\u5de5\u4f5c\u539f\u7406\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p><strong>Linux \u4e2d linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\u548c\u539f\u7406<\/strong><\/p>\n<p>\u5728 Linux \u7cfb\u7edf\u4e2d\uff0clinkwatch \u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5185\u6838\u8fdb\u7a0b\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5b9e\u65f6\u76d1\u6d4b\u7f51\u7edc\u63a5\u53e3\u7684\u72b6\u6001\u53d8\u5316\uff0c\u5e76\u5728\u63a5\u53e3\u72b6\u6001\u53d1\u751f\u6539\u53d8\u65f6\u901a\u77e5\u7cfb\u7edf\u548c\u76f8\u5173\u7684\u5e94\u7528\u7a0b\u5e8f\u3002linkwatch \u8fdb\u7a0b\u8d1f\u8d23\u76d1\u63a7\u7f51\u7edc\u63a5\u53e3\u7684\u94fe\u63a5\u72b6\u6001\uff0c\u4f8b\u5982\u7f51\u7ebf\u7684\u63d2\u62d4\u3001\u7f51\u53e3\u7684\u542f\u7528\u548c\u505c\u7528\u7b49\u64cd\u4f5c\u3002\u4e00\u65e6\u53d1\u73b0\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u7684\u53d8\u5316\uff0clinkwatch \u8fdb\u7a0b\u4f1a\u7acb\u5373\u5411\u7cfb\u7edf\u53d1\u9001\u4fe1\u53f7\uff0c\u901a\u77e5\u7cfb\u7edf\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<p>\u94fe\u63a5\u72b6\u6001\u7684\u53d8\u5316\u53ef\u80fd\u4f1a\u6d89\u53ca\u5230 IP \u5730\u5740\u7684\u53d8\u66f4\u3001\u8def\u7531\u8868\u7684\u66f4\u65b0\u7b49\u91cd\u8981\u4fe1\u606f\uff0c\u56e0\u6b64 linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\u662f\u975e\u5e38\u5173\u952e\u7684\u3002\u901a\u8fc7 linkwatch \u8fdb\u7a0b\uff0cLinux \u7cfb\u7edf\u53ef\u4ee5\u5b9e\u65f6\u54cd\u5e94\u7f51\u7edc\u63a5\u53e3\u7684\u72b6\u6001\u53d8\u5316\uff0c\u786e\u4fdd\u7f51\u7edc\u8fde\u63a5\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>linkwatch \u8fdb\u7a0b\u7684\u539f\u7406\u662f\u901a\u8fc7\u76d1\u542c netlink socket \u5b9e\u73b0\u7684\u3002netlink \u662f Linux \u5185\u6838\u4e0e\u7528\u6237\u7a7a\u95f4\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\u7684\u4e00\u79cd\u673a\u5236\uff0c\u53ef\u4ee5\u5b9e\u73b0\u975e\u5e38\u9ad8\u6548\u7684\u6570\u636e\u4f20\u8f93\u3002linkwatch \u8fdb\u7a0b\u901a\u8fc7\u76d1\u542c netlink socket\uff0c\u5b9e\u65f6\u83b7\u53d6\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u7684\u6539\u53d8\u60c5\u51b5\uff0c\u5e76\u53ca\u65f6\u5411\u7cfb\u7edf\u8fdb\u884c\u901a\u77e5\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 C \u8bed\u8a00\u7f16\u5199\u7684\u7b80\u5355\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u5b9e\u73b0 linkwatch \u8fdb\u7a0b\u7684\u529f\u80fd\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;sys\/socket.h&gt;\n#include &lt;linux\/netlink.h&gt;\n\n#define MAX_PAYLOAD 1024\n\nint main() {\n    struct sockaddr_nl addr;\n    int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);\n    char buf[MAX_PAYLOAD];\n\n    memset(&amp;addr, 0, sizeof(addr));\n    addr.nl_family = AF_NETLINK;\n    addr.nl_groups = RTMGRP_LINK;\n\n    bind(sock, (struct sockaddr *)&amp;addr, sizeof(addr));\n\n    while (1) {\n        recv(sock, buf, sizeof(buf), 0);\n        \/\/ \u5904\u7406\u63a5\u6536\u5230\u7684\u6570\u636e\uff0c\u5224\u65ad\u63a5\u53e3\u72b6\u6001\u53d8\u5316\u5e76\u901a\u77e5\u7cfb\u7edf\n        printf(\"Received link state change notification\n\");\n    }\n\n    close(sock);\n    return 0;\n}<\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u6f14\u793a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 linkwatch \u8fdb\u7a0b\uff0c\u901a\u8fc7\u76d1\u542c netlink socket \u7684 RTMGRP_LINK \u7ec4\uff0c\u5b9e\u65f6\u63a5\u6536\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u53d8\u5316\u7684\u901a\u77e5\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5bf9\u63a5\u6536\u5230\u7684\u6570\u636e\u8fdb\u884c\u89e3\u6790\u548c\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u7cfb\u7edf\u80fd\u591f\u53ca\u65f6\u54cd\u5e94\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u7684\u53d8\u5316\u3002<\/p>\n<p>\u603b\u4e4b\uff0clinkwatch \u8fdb\u7a0b\u5728 Linux \u7cfb\u7edf\u4e2d\u626e\u6f14\u7740\u91cd\u8981\u7684\u89d2\u8272\uff0c\u901a\u8fc7\u5b9e\u65f6\u76d1\u6d4b\u7f51\u7edc\u63a5\u53e3\u7684\u72b6\u6001\u53d8\u5316\uff0c\u4fdd\u8bc1\u4e86\u7f51\u7edc\u8fde\u63a5\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002\u901a\u8fc7\u5bf9 linkwatch \u8fdb\u7a0b\u7684\u7406\u89e3\u548c\u5b9e\u73b0\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u638c\u63e1 Linux \u7cfb\u7edf\u4e2d\u7f51\u7edc\u7ba1\u7406\u7684\u539f\u7406\u548c\u673a\u5236\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u4e86\u89e3 Linux \u4e2d linkwatch \u8fdb\u7a0b\u7684\u529f\u80fd\u548c\u5de5\u4f5c\u539f\u7406\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \uff08CursorNotFound\uff09\u5728\u547d\u540d\u7a7a\u95f4&#8221;dbName.collection&#8221;\u4e2d\u65e0\u6cd5\u627e\u5230\u6e38\u6807\uff0cID\u4e3a885805460243113719\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 Linux \u4e2d linkwatch \u8fdb\u7a0b\u7684\u529f\u80fd\u548c\u5de5\u4f5c\u539f\u7406 \u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u7c73\u4e91\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u4e86\u89e3 Linux \u4e2d linkwatch \u8fdb\u7a0b\u7684\u529f\u80fd\u548c\u5de5\u4f5c\u539f\u7406\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b Linux \u4e2d linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\u548c\u539f\u7406 \u5728 Linux \u7cfb\u7edf\u4e2d\uff0clinkwatch \u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5185\u6838\u8fdb\u7a0b\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5b9e\u65f6\u76d1\u6d4b\u7f51\u7edc\u63a5\u53e3\u7684\u72b6\u6001\u53d8\u5316\uff0c\u5e76\u5728\u63a5\u53e3\u72b6\u6001\u53d1\u751f\u6539\u53d8\u65f6\u901a\u77e5\u7cfb\u7edf\u548c\u76f8\u5173\u7684\u5e94\u7528\u7a0b\u5e8f\u3002linkwatch \u8fdb\u7a0b\u8d1f\u8d23\u76d1\u63a7\u7f51\u7edc\u63a5\u53e3\u7684\u94fe\u63a5\u72b6\u6001\uff0c\u4f8b\u5982\u7f51\u7ebf\u7684\u63d2\u62d4\u3001\u7f51\u53e3\u7684\u542f\u7528\u548c\u505c\u7528\u7b49\u64cd\u4f5c\u3002\u4e00\u65e6\u53d1\u73b0\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u7684\u53d8\u5316\uff0clinkwatch \u8fdb\u7a0b\u4f1a\u7acb\u5373\u5411\u7cfb\u7edf\u53d1\u9001\u4fe1\u53f7\uff0c\u901a\u77e5\u7cfb\u7edf\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002 \u94fe\u63a5\u72b6\u6001\u7684\u53d8\u5316\u53ef\u80fd\u4f1a\u6d89\u53ca\u5230 IP \u5730\u5740\u7684\u53d8\u66f4\u3001\u8def\u7531\u8868\u7684\u66f4\u65b0\u7b49\u91cd\u8981\u4fe1\u606f\uff0c\u56e0\u6b64 linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\u662f\u975e\u5e38\u5173\u952e\u7684\u3002\u901a\u8fc7 linkwatch \u8fdb\u7a0b\uff0cLinux \u7cfb\u7edf\u53ef\u4ee5\u5b9e\u65f6\u54cd\u5e94\u7f51\u7edc\u63a5\u53e3\u7684\u72b6\u6001\u53d8\u5316\uff0c\u786e\u4fdd\u7f51\u7edc\u8fde\u63a5\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002 linkwatch \u8fdb\u7a0b\u7684\u539f\u7406\u662f\u901a\u8fc7\u76d1\u542c netlink socket \u5b9e\u73b0\u7684\u3002netlink \u662f Linux \u5185\u6838\u4e0e\u7528\u6237\u7a7a\u95f4\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\u7684\u4e00\u79cd\u673a\u5236\uff0c\u53ef\u4ee5\u5b9e\u73b0\u975e\u5e38\u9ad8\u6548\u7684\u6570\u636e\u4f20\u8f93\u3002linkwatch \u8fdb\u7a0b\u901a\u8fc7\u76d1\u542c netlink socket\uff0c\u5b9e\u65f6\u83b7\u53d6\u7f51\u7edc\u63a5\u53e3\u72b6\u6001\u7684\u6539\u53d8\u60c5\u51b5\uff0c\u5e76\u53ca\u65f6\u5411\u7cfb\u7edf\u8fdb\u884c\u901a\u77e5\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 C \u8bed\u8a00\u7f16\u5199\u7684\u7b80\u5355\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u5b9e\u73b0 linkwatch \u8fdb\u7a0b\u7684\u529f\u80fd\uff1a #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #include &lt;sys\/socket.h&gt; #include [&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-51524","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/51524","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=51524"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/51524\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=51524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=51524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=51524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}