{"id":133,"date":"2026-07-23T23:41:19","date_gmt":"2026-07-23T15:41:19","guid":{"rendered":"https:\/\/fsdata.site\/?p=133"},"modified":"2026-07-23T23:41:19","modified_gmt":"2026-07-23T15:41:19","slug":"python-ops-automation","status":"publish","type":"post","link":"https:\/\/fsdata.site\/?p=133","title":{"rendered":"Python \u8fd0\u7ef4\u81ea\u52a8\u5316\uff1a\u6279\u91cf\u670d\u52a1\u5668\u7ba1\u7406\u811a\u672c\u5b9e\u6218"},"content":{"rendered":"<h2>\u4e3a\u4ec0\u4e48\u7528 Python \u505a\u8fd0\u7ef4\u81ea\u52a8\u5316<\/h2>\n<p>\u5f53\u670d\u52a1\u5668\u4ece 1 \u53f0\u53d8 10 \u53f0\uff0c\u624b\u5de5 SSH \u4e00\u53f0\u53f0\u6572\u547d\u4ee4\u5c31\u6210\u4e86\u707e\u96be\u3002Python \u7684 <code>paramiko<\/code>\/<code>fabric<\/code> \u8ba9\u4f60\u7528\u51e0\u884c\u4ee3\u7801\u6279\u91cf\u6267\u884c\u547d\u4ee4\u3001\u5206\u53d1\u6587\u4ef6\u3001\u6536\u96c6\u65e5\u5fd7\uff0c\u628a\u91cd\u590d\u52b3\u52a8\u4ea4\u7ed9\u811a\u672c\u3002\u672c\u6587\u7ed9\u4f60\u4e00\u5957\u80fd\u76f4\u63a5\u6284\u7684\u5b9e\u6218\u6a21\u677f\u3002<\/p>\n<h2>\u4e00\u3001\u7528 paramiko \u6279\u91cf\u6267\u884c\u547d\u4ee4<\/h2>\n<pre><code>import paramiko\n\nhosts = [\n    {\"host\": \"192.168.1.10\", \"user\": \"ops\", \"pwd\": \"***\"},\n    {\"host\": \"192.168.1.11\", \"user\": \"ops\", \"pwd\": \"***\"},\n]\n\ndef run(host_cfg, cmd):\n    ssh = paramiko.SSHClient()\n    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n    ssh.connect(host_cfg[\"host\"], username=host_cfg[\"user\"],\n                password=host_cfg[\"pwd\"], timeout=10)\n    stdin, stdout, stderr = ssh.exec_command(cmd)\n    out = stdout.read().decode()\n    ssh.close()\n    return out\n\nfor h in hosts:\n    print(h[\"host\"], \"->\", run(h, \"uptime\").strip())\n<\/code><\/pre>\n<h2>\u4e8c\u3001\u5e76\u53d1\u6267\u884c\uff0c\u522b\u4e00\u53f0\u5361\u4f4f\u7b49\u6240\u6709<\/h2>\n<p>\u7528\u7ebf\u7a0b\u6c60\u8ba9\u591a\u53f0\u5e76\u884c\uff0c\u6548\u7387\u7ffb\u51e0\u500d\uff1a<\/p>\n<pre><code>from concurrent.futures import ThreadPoolExecutor\n\ndef task(h):\n    return h[\"host\"], run(h, \"df -h \/ | tail -1\")\n\nwith ThreadPoolExecutor(max_workers=10) as ex:\n    for host, res in ex.map(task, hosts):\n        print(host, res.strip())\n<\/code><\/pre>\n<h2>\u4e09\u3001\u6279\u91cf\u5206\u53d1\u6587\u4ef6<\/h2>\n<pre><code>def push(h, local, remote):\n    t = paramiko.Transport((h[\"host\"], 22))\n    t.connect(username=h[\"user\"], password=h[\"pwd\"])\n    sftp = paramiko.SFTPClient.from_transport(t)\n    sftp.put(local, remote)\n    sftp.close(); t.close()\n\npush(hosts[0], \"deploy.sh\", \"\/tmp\/deploy.sh\")\n<\/code><\/pre>\n<h2>\u56db\u3001\u96c6\u4e2d\u6536\u96c6\u65e5\u5fd7<\/h2>\n<p>\u628a\u5404\u673a\u7684\u5173\u952e\u65e5\u5fd7\u62c9\u5230\u672c\u5730\u7edf\u4e00\u5206\u6790\uff1a<\/p>\n<pre><code>def collect(h):\n    out = run(h, \"tail -n 200 \/var\/log\/nginx\/error.log\")\n    with open(f\"logs\/{h['host']}.log\", \"w\") as f:\n        f.write(out)\n\nfor h in hosts:\n    collect(h)\n<\/code><\/pre>\n<h2>\u4e94\u3001\u751f\u4ea7\u53ef\u7528\u7684\u6ce8\u610f\u70b9<\/h2>\n<ul>\n<li><strong>\u5bc6\u7801\u522b\u786c\u7f16\u7801<\/strong>\uff1a\u653e\u73af\u5883\u53d8\u91cf\u6216\u5bc6\u94a5\u7ba1\u7406\uff0c\u522b\u5199\u8fdb\u811a\u672c\u63d0\u4ea4\u5230 git\u3002<\/li>\n<li><strong>\u52a0\u8d85\u65f6\u548c\u91cd\u8bd5<\/strong>\uff1a\u67d0\u53f0\u7f51\u7edc\u6296\u52a8\u4e86\u4e0d\u8981\u5361\u6b7b\u6574\u4e2a\u811a\u672c\u3002<\/li>\n<li><strong>\u9519\u8bef\u522b\u541e<\/strong>\uff1a\u628a\u5931\u8d25\u7684\u4e3b\u673a\u5355\u72ec\u5217\u51fa\u6765\uff0c\u6700\u540e\u6c47\u603b\u62a5\u544a\u3002<\/li>\n<li><strong>\u5371\u9669\u547d\u4ee4\u5148 dry-run<\/strong>\uff1a\u6279\u91cf <code>rm<\/code>\/<code>reboot<\/code> \u524d\u5148\u6253\u5370\u8981\u6267\u884c\u7684\u547d\u4ee4\u786e\u8ba4\u3002<\/li>\n<li><strong>\u7528\u5bc6\u94a5\u767b\u5f55\u66f4\u7a33<\/strong>\uff1a<code>ssh.connect(..., key_filename=\"id_rsa\")<\/code> \u6bd4\u5bc6\u7801\u66f4\u5b89\u5168\u548c\u53ef\u811a\u672c\u5316\u3002<\/li>\n<\/ul>\n<h2>\u516d\u3001fabric \u66f4\u7b80\u6d01\u7684\u5199\u6cd5<\/h2>\n<pre><code>from fabric import Connection\nfor ip in [\"10.0.0.1\", \"10.0.0.2\"]:\n    c = Connection(ip, user=\"ops\", connect_kwargs={\"key_filename\": \"id_rsa\"})\n    print(c.run(\"free -m\").stdout)\n<\/code><\/pre>\n<h2>\u5c0f\u7ed3<\/h2>\n<p>\u8fd0\u7ef4\u81ea\u52a8\u5316\u7684\u672c\u8d28\uff0c\u662f\u628a&#8221;\u5bf9 N \u53f0\u673a\u5668\u91cd\u590d\u505a\u7684\u4e8b&#8221;\u62bd\u8c61\u6210&#8221;\u5bf9\u4e00\u53f0\u673a\u5668\u505a\u7684\u4e8b + \u4e00\u4e2a\u5faa\u73af&#8221;\u3002paramiko \u7ed9\u4f60\u5e95\u5c42\u63a7\u5236\uff0cfabric \u7ed9\u4f60\u4e0a\u5c42\u8bed\u6cd5\u7cd6\u3002\u638c\u63e1\u8fd9\u4e24\u5957\uff0c10 \u53f0\u670d\u52a1\u5668\u7684\u65e5\u5e38\u5de1\u68c0\u3001\u53d1\u5e03\u3001\u6392\u9519\uff0c\u90fd\u80fd\u538b\u7f29\u6210\u4e00\u6761\u547d\u4ee4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7528 paramiko\/fabric \u6279\u91cf\u6267\u884c\u547d\u4ee4\u3001\u5206\u53d1\u6587\u4ef6\u3001\u6536\u96c6\u65e5\u5fd7\uff0c10 \u53f0\u670d\u52a1\u5668\u8fd0\u7ef4\u53d8\u51e0\u884c\u811a\u672c\u3002<\/p>\n","protected":false},"author":2,"featured_media":124,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-133","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tools"],"_links":{"self":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts\/133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=133"}],"version-history":[{"count":0,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/posts\/133\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=\/wp\/v2\/media\/124"}],"wp:attachment":[{"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fsdata.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}