{"id":63116,"date":"2025-04-29T12:28:13","date_gmt":"2025-04-29T04:28:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/63116\/"},"modified":"2025-04-29T12:28:13","modified_gmt":"2025-04-29T04:28:13","slug":"linux-%e4%b8%ad-linkwatch-%e8%bf%9b%e7%a8%8b%e7%9a%84%e4%bd%9c%e7%94%a8%e5%92%8c%e5%8e%9f%e7%90%86-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/63116\/","title":{"rendered":"Linux \u4e2d linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\u548c\u539f\u7406"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/170934193043601.jpg\" class=\"aligncenter\" title=\"Linux \u4e2d linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\u548c\u539f\u7406\u63d2\u56fe\" alt=\"Linux \u4e2d linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\u548c\u539f\u7406\u63d2\u56fe\" \/><\/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&gt;\n#include &lt;linux&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;amp;addr, 0, sizeof(addr));\n    addr.nl_family = AF_NETLINK;\n    addr.nl_groups = RTMGRP_LINK;\n\n    bind(sock, (struct sockaddr *)&amp;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}&lt;\/linux&gt;&lt;\/sys&gt;&lt;\/unistd.h&gt;&lt;\/string.h&gt;&lt;\/stdlib.h&gt;&lt;\/stdio.h&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\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>\u4ee5\u4e0a\u5c31\u662fLinux \u4e2d linkwatch \u8fdb\u7a0b\u7684\u4f5c\u7528\u548c\u539f\u7406\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8FDCServers\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&gt; #include &lt;linux&gt; #define MAX_PAYLOAD 1024 int main() { struct sockaddr_nl addr; [&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-63116","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63116","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=63116"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63116\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=63116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=63116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=63116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}