{"id":69,"date":"2026-10-29T12:00:39","date_gmt":"2026-10-29T20:00:39","guid":{"rendered":"\/?p=69"},"modified":"2026-10-29T12:00:39","modified_gmt":"2026-10-29T20:00:39","slug":"post-mvt0mo8e","status":"publish","type":"post","link":"https:\/\/www.woiov.cn\/?p=69","title":{"rendered":"\u5bb9\u5668\u81ea\u52a8\u4e0a\u62a5\uff1a\u7528\u4e00\u6bb5 printf \u62fc JSON\uff0c\u8e29\u4e86\u4e09\u4e2a\u5751"},"content":{"rendered":"<h2>\u5bb9\u5668\u81ea\u52a8\u4e0a\u62a5\uff1a\u7528\u4e00\u6bb5 printf \u62fc JSON\uff0c\u8e29\u4e86\u4e09\u4e2a\u5751<\/h2>\n<p>\u72f8\u82b1\u4e91\u63a7\u5236\u53f0\u4e0a\uff0c\u6bcf\u53f0\u6258\u7ba1\u5bb9\u5668\u90fd\u6709\u4e00\u5f20\u300c\u8fd0\u884c\u72b6\u6001\u300d\u5361\u7247\uff1aIPv4\u3001IPv6\u3001\u7cfb\u7edf\u3001\u5185\u6838\u3001\u78c1\u76d8\u3001\u5185\u5b58\u3001\u5df2\u88c5\u4e86\u54ea\u4e9b\u5de5\u5177\u3002\u8fd9\u4e9b\u6570\u636e\u4e0d\u662f\u4ece PVE \u6765\u7684\u2014\u2014PVE \u53ea\u77e5\u9053\u5bb9\u5668\u5f00\u7740\uff0c\u4e0d\u77e5\u9053\u91cc\u9762\u8dd1\u7684\u4ec0\u4e48\u7cfb\u7edf\u3001\u8fd8\u5269\u591a\u5c11\u5185\u5b58\u3002<\/p>\n<p>\u5bb9\u5668\u91cc\u4e5f\u6ca1\u6709\u6211\u4eec\u7684 agent\u3002\u88c5 agent \u610f\u5473\u7740\u8981\u7ef4\u62a4\u4e00\u4e2a\u5e38\u9a7b\u8fdb\u7a0b\u3001\u8981\u8003\u8651\u5b83\u81ea\u5df1\u5d29\u4e86\u600e\u4e48\u529e\u3001\u8981\u8003\u8651\u7528\u6237\u5378\u8f7d\u5b83\u600e\u4e48\u529e\u3002\u6240\u4ee5\u9009\u4e86\u53e6\u4e00\u6761\u8def\uff1a<strong>\u5e73\u53f0\u4e3b\u52a8 SSH \u8fdb\u53bb\uff0c\u8dd1\u4e00\u6bb5\u4e34\u65f6\u811a\u672c\uff0c\u8ba9\u811a\u672c\u81ea\u5df1\u628a\u73af\u5883\u4fe1\u606f\u6253\u6210 JSON \u56de\u4f20<\/strong>\u3002<\/p>\n<p>\u62c9\u6a21\u5f0f\uff08pull\uff09\u800c\u4e0d\u662f\u63a8\u6a21\u5f0f\uff08push\uff09\u3002\u4ee3\u4ef7\u662f\u5e73\u53f0\u5fc5\u987b\u6301\u6709\u5bb9\u5668\u7684\u767b\u5f55\u51ed\u636e\uff0c\u6536\u76ca\u662f\u88ab\u91c7\u96c6\u7aef\u96f6\u5e38\u9a7b\u3002<\/p>\n<h3>\u811a\u672c\u957f\u4ec0\u4e48\u6837<\/h3>\n<p>\u4e0d\u662f\u72ec\u7acb\u6587\u4ef6\uff0c\u662f\u540e\u7aef\u5b57\u7b26\u4e32\u62fc\u51fa\u6765\u7684\uff0c\u8ddf\u7740\u8bf7\u6c42\u8d70\uff1a<\/p>\n<pre class=\"wp-block-code\"><code>\nVMID=\"212\"\nIP4=\"$(ip -4 addr show scope global | grep -oE 'inet [0-9.]+' | awk '{print $2}' | head -1)\"\nIP6=\"$(ip -6 addr show scope global | grep -oE 'inet6 [0-9a-f:]+' | awk '{print $2}' | grep -v '^fe80' | head -1)\"\nKR=\"$(uname -r)\"\nDT=\"$(df -BG \/ | awk 'NR==2{gsub(\"G\",\"\",$2); print $2}')\"\nMT=\"$(awk '\/MemTotal\/{print int($2\/1024)}' \/proc\/meminfo)\"\nMA=\"$(awk '\/MemAvailable\/{print int($2\/1024)}' \/proc\/meminfo)\"\nMU=$((MT - MA))\n\nTOOLS=\"\"\nfor t in curl wget git htop vim unzip tree netstat dig tcpdump mtr iotop ss; do\n  if command -v \"$t\" &gt;\/dev\/null 2&gt;&amp;1; then\n    TOOLS=\"$TOOLS\\\"$t\\\",\"\n  fi\ndone\nTOOLS=\"[${TOOLS%,}]\"\n\nJSON=$(printf '{\"vmid\":\"%s\",\"hostname\":\"%s\",\"ipv4\":\"%s\",\"ipv6\":\"%s\",\"kernel\":\"%s\",\"os\":\"%s\",\"tools\":%s,\"diskTotalGB\":%s,\"memTotalMB\":%s,\"uptimeSec\":%s}' \\\n  \"$VMID\" \"$HN\" \"$IP4\" \"$IP6\" \"$KR\" \"$OS\" \"$TOOLS\" \"${DT:-0}\" \"${MT:-0}\" \"${UP:-0}\")\n\ncurl -s -m 8 -X POST 'http:\/\/192.168.2.214:37100\/api\/agent\/report' \\\n  -H 'Content-Type: application\/json' -d \"$JSON\" &gt;\/dev\/null\n<\/code><\/pre>\n<p>\u5b57\u6bb5\u6765\u6e90\u5168\u5728\u7cfb\u7edf\u81ea\u5e26\u6587\u4ef6\u91cc\uff0c\u4e0d\u4f9d\u8d56\u4efb\u4f55\u989d\u5916\u5b89\u88c5\uff1a<\/p>\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>\u5b57\u6bb5<\/th>\n<th>\u91c7\u96c6\u65b9\u5f0f<\/th>\n<th>\u7f3a\u503c\u515c\u5e95<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ipv4 \/ ipv6<\/td>\n<td><code>ip addr show scope global<\/code> \u8fc7\u6ee4 fe80<\/td>\n<td>\u7a7a\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>hostname<\/td>\n<td><code>hostname<\/code><\/td>\n<td>\u7a7a<\/td>\n<\/tr>\n<tr>\n<td>os<\/td>\n<td><code>\/etc\/os-release<\/code> \u7684 <code>PRETTY_NAME<\/code><\/td>\n<td><code>Linux<\/code><\/td>\n<\/tr>\n<tr>\n<td>kernel<\/td>\n<td><code>uname -r<\/code><\/td>\n<td>\u7a7a<\/td>\n<\/tr>\n<tr>\n<td>diskTotalGB \/ diskUsedGB<\/td>\n<td><code>df -BG \/<\/code> \u53bb\u5355\u4f4d<\/td>\n<td><code>${DT:-0}<\/code><\/td>\n<\/tr>\n<tr>\n<td>memTotalMB<\/td>\n<td><code>\/proc\/meminfo<\/code> MemTotal \u00f71024<\/td>\n<td><code>0<\/code><\/td>\n<\/tr>\n<tr>\n<td>memUsedMB<\/td>\n<td>MemTotal \u2212 MemAvailable<\/td>\n<td>\u8ba1\u7b97\u5f97\u51fa<\/td>\n<\/tr>\n<tr>\n<td>uptimeSec<\/td>\n<td><code>\/proc\/uptime<\/code> \u53d6\u6574<\/td>\n<td><code>0<\/code><\/td>\n<\/tr>\n<tr>\n<td>tools<\/td>\n<td><code>command -v<\/code> \u9010\u4e2a\u63a2\u6d4b 17 \u4e2a\u547d\u4ee4<\/td>\n<td><code>[]<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h3>\u5751\u4e00\uff1a\u53cc\u5f15\u53f7\u88ab\u5403\u4e86\u4e24\u5c42<\/h3>\n<p>\u7b2c\u4e00\u7248 tools \u957f\u8fd9\u6837\uff1a<\/p>\n<pre class=\"wp-block-code\"><code>\nTOOLS=\"$TOOLS$t,\"        # \u7ed3\u679c\uff1a[curl,wget,]\n<\/code><\/pre>\n<p>\u62fc\u51fa\u6765\u7684 JSON \u662f <code>\"tools\":[curl,wget]<\/code>\u2014\u2014\u6ca1\u6709\u5f15\u53f7\u7684\u88f8\u6807\u8bc6\u7b26\u3002\u670d\u52a1\u7aef <code>express.json()<\/code> \u76f4\u63a5\u629b\u89e3\u6790\u9519\u8bef\uff0c\u63a5\u53e3\u8fd4\u56de 400\uff0c\u524d\u7aef\u53ea\u770b\u5230\u300c\u4e0a\u62a5\u5931\u8d25\u300d\uff0c\u65e5\u5fd7\u91cc\u8fde\u4e2a\u5b57\u6bb5\u540d\u90fd\u6ca1\u6709\u3002<\/p>\n<p>\u539f\u56e0\u662f\u8f6c\u4e49\u5c42\u7ea7\u7b97\u9519\u4e86\u3002\u8fd9\u6761\u811a\u672c\u8981\u8fc7\u4e09\u5c42\uff1a\u540e\u7aef JS \u5b57\u7b26\u4e32 \u2192 SSH \u4f20\u8f93 \u2192 \u8fdc\u7aef shell \u89e3\u6790\u3002JS \u91cc\u4e00\u4e2a <code>\\\"<\/code> \u5230\u8fdc\u7aef\u53ea\u5269\u4e00\u4e2a <code>\"<\/code>\uff0c\u800c\u8fdc\u7aef\u8fd8\u9700\u8981\u4e00\u4e2a <code>\"<\/code> \u624d\u80fd\u771f\u6b63\u5199\u8fdb\u53d8\u91cf\u3002\u6240\u4ee5\u5fc5\u987b\u5199\u6210 <code>\\\\\"<\/code>\uff1a<\/p>\n<pre class=\"wp-block-code\"><code>\nTOOLS=\"$TOOLS\\\\\"$t\\\\\",\"   # JS \u91cc\u770b\u5230\u7684\u662f \\\\\"\uff0c\u8fdc\u7aef\u6536\u5230 \\\"\uff0c\u53d8\u91cf\u91cc\u662f \"\n<\/code><\/pre>\n<p>\u8fd8\u6709\u4e00\u4e2a\u8fb9\u754c\uff1a\u4e00\u4e2a\u547d\u4ee4\u90fd\u6ca1\u88c5\u65f6 <code>TOOLS<\/code> \u662f\u7a7a\u4e32\uff0c<code>[${TOOLS%,}]<\/code> \u4f1a\u9000\u5316\u6210 <code>[]<\/code>\uff0c\u8fd9\u4e2a\u5012\u662f\u610f\u5916\u5730\u6b63\u786e\u3002\u7a7a\u6570\u7ec4\u80fd\u8fc7\uff0c\u88f8\u6807\u8bc6\u7b26\u8fc7\u4e0d\u4e86\u3002<\/p>\n<h3>\u5751\u4e8c\uff1a\u503c\u91cc\u672c\u8eab\u5e26\u5f15\u53f7<\/h3>\n<p><code>PRETTY_NAME<\/code> \u5728 Debian \u7cfb\u4e0a\u5e26\u5f15\u53f7\uff1a<\/p>\n<pre class=\"wp-block-code\"><code>\nPRETTY_NAME=\"Ubuntu 22.04.3 LTS\"\n<\/code><\/pre>\n<p>\u65e9\u671f\u7248\u672c\u7528\u5b57\u7b26\u4e32\u62fc\u63a5 JSON\uff0c<code>\"os\":\"<\/code> + \u8fd9\u4e2a\u503c + <code>\"<\/code>\uff0c\u76f4\u63a5\u62fc\u51fa <code>\"os\":\"\"Ubuntu 22.04.3 LTS\"\"<\/code>\u2014\u2014JSON \u65ad\u4e86\u3002\u6539\u6210 <code>printf<\/code> \u52a0 <code>%s<\/code> \u5360\u4f4d\u7b26\u540e\uff0c\u5f15\u53f7\u53d8\u6210\u666e\u901a\u7684\u6b63\u6587\u5b57\u7b26\uff0c\u524d\u7aef\u663e\u793a <code>\"Ubuntu 22.04.3 LTS\"<\/code>\uff08\u5c31\u662f\u5e26\u5f15\u53f7\u7684\u5b57\u7b26\u4e32\uff09\uff0c\u867d\u7136\u591a\u4e86\u4e24\u4e2a\u5f15\u53f7\uff0c\u4f46 JSON \u5408\u6cd5\u3002<\/p>\n<p>\u73b0\u5728\u505a\u4e86\u4e24\u6b21 <code>sed<\/code> \u5265\u5f15\u53f7\uff0c\u4e5f\u6ca1\u505a JSON \u8f6c\u4e49\u2014\u2014\u56e0\u4e3a\u5269\u4e0b\u53ef\u80fd\u51fa\u73b0\u7684\u7279\u6b8a\u5b57\u7b26\uff08hostname \u91cc\u4e0d\u4f1a\u6709\u5f15\u53f7\uff0c\u5185\u6838\u7248\u672c\u4e0d\u4f1a\u6709\u53cd\u659c\u6760\uff09\u6982\u7387\u6781\u4f4e\u3002\u8fd9\u662f\u660e\u786e\u7684\u6280\u672f\u503a\uff1a<strong>\u8981\u7528\u5b57\u7b26\u4e32\u62fc JSON\uff0c\u5c31\u5f97\u5047\u8bbe\u503c\u91cc\u6ca1\u6709\u5f15\u53f7\u548c\u53cd\u659c\u6760<\/strong>\u3002\u771f\u8981\u505a\u5e72\u51c0\uff0c\u5e94\u8be5\u5728\u8fdc\u7aef\u7528 <code>jq -n --arg<\/code> \u751f\u6210\uff0c\u4f46\u5bb9\u5668\u91cc\u4e0d\u4e00\u5b9a\u6709 jq\uff0c\u90a3\u53c8\u56de\u5230\u300c\u4f9d\u8d56\u989d\u5916\u5b89\u88c5\u300d\u7684\u8001\u95ee\u9898\u3002<\/p>\n<h3>\u5751\u4e09\uff1a10 \u5206\u949f\u7684\u7a97\u53e3\u88c5\u4e0d\u4e0b\u4e00\u8f6e\u5168\u91cf<\/h3>\n<p>\u81ea\u52a8\u4e0a\u62a5\u7684\u8c03\u5ea6\u662f\uff1a\u540e\u7aef\u542f\u52a8 3 \u5206\u949f\u540e\u5148\u8dd1\u4e00\u6b21\uff0c\u4e4b\u540e\u6bcf 10 \u5206\u949f\u4e00\u8f6e\u3002\u5b9e\u73b0\u662f <code>setInterval<\/code>\uff0c\u904d\u5386\u6240\u6709 running \u5bb9\u5668\uff0c\u9010\u4e2a SSH \u6267\u884c\u811a\u672c\uff0c\u5355\u53f0\u8d85\u65f6 20 \u79d2\u3002<\/p>\n<p>\u95ee\u9898\u5728\u4e8e\u4e32\u884c\u300220 \u53f0\u5bb9\u5668\u91cc\u5982\u679c 5 \u53f0 IP \u521a\u5206\u914d\u8fd8\u6ca1\u5c31\u7eea\u3001SSH \u4e00\u8def\u7b49\u5230\u8d85\u65f6\uff0c\u8fd9\u4e00\u8f6e\u5c31\u8981\u82b1 100 \u79d2\uff1b\u5bb9\u5668\u6570\u518d\u7ffb\u4e00\u500d\u3001\u7f51\u7edc\u518d\u5dee\u4e00\u70b9\uff0c\u4e00\u8f6e\u5c31\u53ef\u80fd\u8d85\u8fc7 10 \u5206\u949f\u2014\u2014<code>setInterval<\/code> \u4e0d\u7ba1\u4e0a\u4e00\u8f6e\u6709\u6ca1\u6709\u8dd1\u5b8c\uff0c\u5230\u70b9\u5c31\u518d\u8d77\u4e00\u8f6e\uff0c\u4e24\u8f6e\u53e0\u5728\u4e00\u8d77\u540c\u65f6 SSH \u540c\u4e00\u53f0\u673a\u5668\uff0c\u8fde\u63a5\u6570\u7ffb\u500d\uff0c\u8fd8\u53ef\u80fd\u4e92\u76f8\u628a\u5bf9\u65b9\u7684\u8d85\u65f6\u62d6\u957f\u3002<\/p>\n<p>\u76ee\u524d\u9760\u4e09\u4e2a\u7ea6\u675f\u538b\u4f4f\uff1a\u5355\u53f0 <code>catch<\/code> \u6389\u4e0d\u4e2d\u65ad\u6574\u8f6e\u3001<code>terminal.guestIp()<\/code> \u62ff\u4e0d\u5230 IP \u76f4\u63a5 <code>continue<\/code>\uff08\u8df3\u8fc7\u800c\u4e0d\u662f\u7b49\u8d85\u65f6\uff09\u3001\u5355\u53f0\u8d85\u65f6\u538b\u5230 20 \u79d2\u3002\u8fd9\u662f\u300c\u591f\u7528\u300d\u800c\u4e0d\u662f\u300c\u6b63\u786e\u300d\u2014\u2014\u6b63\u786e\u505a\u6cd5\u662f\u52a0\u4e00\u4e2a\u8fdb\u884c\u4e2d\u6807\u5fd7\uff0c\u4e0a\u4e00\u8f6e\u6ca1\u7ed3\u675f\u5c31\u8df3\u8fc7\u672c\u8f6e\uff0c\u6216\u8005\u6539\u6210\u961f\u5217\u52a0\u5e76\u53d1\u4e0a\u9650\u3002<\/p>\n<h3>\u987a\u5e26\u8bf4\u4e00\u53e5\u300c\u4e00\u952e\u8865\u62a5\u300d<\/h3>\n<p>\u81ea\u52a8\u4e0a\u62a5\u4e4b\u5916\u8fd8\u7559\u4e86\u4e2a\u624b\u52a8\u5165\u53e3\uff1a\u7ba1\u7406\u5458\u70b9\u4e00\u4e0b\uff0c\u7acb\u523b\u5bf9\u5168\u90e8 running \u5bb9\u5668\u8dd1\u4e00\u6b21\u3002\u8fd9\u6761\u8def\u5f84\u5355\u53f0\u8d85\u65f6\u7ed9\u5230 30 \u79d2\uff0c\u56e0\u4e3a\u4eba\u5c31\u5728\u5c4f\u5e55\u524d\u7b49\u7ed3\u679c\uff0c\u5b81\u53ef\u6162\u4e00\u70b9\u4e5f\u8981\u62ff\u5168\u3002<\/p>\n<p>\u4e24\u6761\u8def\u5f84\u5171\u7528\u540c\u4e00\u4e2a\u811a\u672c\u751f\u6210\u51fd\u6570\uff0c\u552f\u4e00\u7684\u5dee\u522b\u5c31\u662f\u8d85\u65f6\u503c\u3002\u5171\u7528\u662f\u6709\u610f\u7684\u2014\u2014\u4e0a\u62a5\u5b57\u6bb5\u4e00\u65e6\u4e24\u8fb9\u5404\u5199\u4e00\u4efd\uff0c\u65e9\u665a\u4f1a\u51fa\u73b0\u300c\u624b\u52a8\u8865\u62a5\u6709 tools\u3001\u81ea\u52a8\u4e0a\u62a5\u6ca1\u6709\u300d\u8fd9\u79cd\u5bf9\u4e0d\u4e0a\u7684\u60c5\u51b5\u3002<\/p>\n<h3>\u5c0f\u7ed3<\/h3>\n<p>\u6ca1\u6709 agent \u7684\u91c7\u96c6\uff0c\u672c\u8d28\u662f\u62ff\u300c\u6bcf\u6b21\u91cd\u65b0\u63a2\u6d4b\u300d\u6362\u300c\u96f6\u5e38\u9a7b\u300d\u3002\u8fd9\u7b14\u4ea4\u6613\u5728\u4e09\u5c42 shell \u8f6c\u4e49\u3001\u503c\u91cc\u5e26\u5f15\u53f7\u3001\u8c03\u5ea6\u7a97\u53e3\u8fd9\u4e09\u4e2a\u5730\u65b9\u5404\u6536\u4e86\u4e00\u6b21\u5229\u606f\u3002\u90fd\u4e0d\u81f4\u547d\uff0c\u4f46\u6bcf\u4e00\u4e2a\u90fd\u80fd\u8ba9\u524d\u7aef\u663e\u793a\u6210\u4e00\u7247\u7a7a\u767d\uff0c\u800c\u65e5\u5fd7\u91cc\u53ea\u6709\u4e00\u53e5 400\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5bb9\u5668\u81ea\u52a8\u4e0a\u62a5\uff1a\u7528\u4e00\u6bb5 printf \u62fc JSON\uff0c\u8e29\u4e86\u4e09\u4e2a\u5751 \u72f8\u82b1\u4e91\u63a7\u5236\u53f0\u4e0a\uff0c\u6bcf\u53f0\u6258\u7ba1\u5bb9\u5668\u90fd\u6709\u4e00\u5f20\u300c\u8fd0\u884c\u72b6\u6001\u300d\u5361\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-69","post","type-post","status-publish","format-standard","hentry","category-cat-e68a80e69cafe697"],"_links":{"self":[{"href":"https:\/\/www.woiov.cn\/index.php?rest_route=\/wp\/v2\/posts\/69","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.woiov.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.woiov.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.woiov.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.woiov.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=69"}],"version-history":[{"count":0,"href":"https:\/\/www.woiov.cn\/index.php?rest_route=\/wp\/v2\/posts\/69\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.woiov.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.woiov.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.woiov.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}