{"id":210,"date":"2023-10-28T14:19:58","date_gmt":"2023-10-28T06:19:58","guid":{"rendered":"https:\/\/www.liaoxinghui.com\/?p=210"},"modified":"2023-10-28T14:19:59","modified_gmt":"2023-10-28T06:19:59","slug":"python%e9%93%be%e6%8e%a5ssh%e6%89%a7%e8%a1%8c%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/www.liaoxinghui.com\/?p=210","title":{"rendered":"Python\u94fe\u63a5ssh\u6267\u884c\u547d\u4ee4"},"content":{"rendered":"<p class=\"wp-block-paragraph\">\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u6279\u91cf\u6267\u884c\u4e00\u4e9b\u547d\u4ee4\uff0c\u6bd4\u5982\u7f51\u7edc\u8bbe\u5907\u6279\u91cf\u5f00\u5c40\uff0c\u670d\u52a1\u5668\u5b9a\u65f6\u91cd\u542f\u670d\u52a1\u7c7b\u4f3c\u7684\u4efb\u52a1\uff0c\u5982\u679c\u8981\u624b\u52a8\u7684\u8bdd\u5c31\u4f1a\u6bd4\u8f83\u8d39\u65f6\u8d39\u529b\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e00\u4e2apython\u5c0f\u811a\u672c\u6765\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\uff0c\u5f53\u7136\u8fd9\u4e2a\u53ea\u662f\u4e00\u4e2a\u5de5\u5177\u7684\u96cf\u5f62\uff0c\u5177\u4f53\u7684\u7684\u9700\u8981\u4f60\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u884c\u5b9a\u5236\u548c\u4fee\u6539<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u5efa\u7acb\u4e00\u4e2asshclient\u5bf9\u8c61\nimport paramiko\nimport sys\n\nssh = paramiko.SSHClient()\n# \u5141\u8bb8\u5c06\u4fe1\u4efb\u7684\u4e3b\u673a\u81ea\u52a8\u52a0\u5165\u5230host_allow \u5217\u8868\uff0c\u6b64\u65b9\u6cd5\u5fc5\u987b\u653e\u5728connect\u65b9\u6cd5\u7684\u524d\u9762\nssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n# \u8c03\u7528connect\u65b9\u6cd5\u8fde\u63a5\u670d\u52a1\u5668\nssh.connect(hostname='192.168.1.1', port=22, username='root', password='123456')\n# \u6267\u884c\u547d\u4ee4\nstdin, stdout, stderr = ssh.exec_command('\u4f60\u9700\u8981\u6267\u884c\u7684\u547d\u4ee4')\n# \u7ed3\u679c\u653e\u5230stdout\u4e2d\uff0c\u5982\u679c\u6709\u9519\u8bef\u5c06\u653e\u5230stderr\u4e2d\nprint(stdout.read().decode())\nif not stderr  == '':\n    print(stderr)\n\n# \u5173\u95ed\u8fde\u63a5\nssh.close()\nsys.exit()<\/pre>\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u6709\u4e2a\u591a\u670d\u52a1\u5668\u7684\u793a\u4f8b\uff0c\u4e5f\u52a0\u5165\u4e86\u65e5\u5fd7\uff0c\u65b9\u4fbf\u67e5\u770b\u7ed3\u679c<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import paramiko\nimport sys\nimport logging\n\n# \u8bbe\u7f6e\u65e5\u5fd7\u914d\u7f6e\nlogging.basicConfig(filename='ssh.log', level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')\n\ndef execute_command(hostname, port, username, password, command):\n    try:\n        ssh = paramiko.SSHClient()\n        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n        ssh.connect(hostname=hostname, port=port, username=username, password=password)\n        stdin, stdout, stderr = ssh.exec_command(command)\n        output = stdout.read().decode()\n        if stderr:\n            logging.error(f'\u5728 {hostname} \u4e0a\u6267\u884c\u547d\u4ee4\u65f6\u51fa\u9519: {stderr.read().decode()}')\n        ssh.close()\n        return output\n    except Exception as e:\n        logging.error(f'\u8fde\u63a5 {hostname} \u65f6\u51fa\u9519: {str(e)}')\n\n# \u670d\u52a1\u5668\u4fe1\u606f\u5217\u8868\nserver_list = [\n    {'hostname': '192.168.1.1', 'port': 22, 'username': 'root', 'password': '123456'},\n    {'hostname': '192.168.1.2', 'port': 22, 'username': 'root', 'password': 'password123'},\n    {'hostname': '192.168.1.3', 'port': 22, 'username': 'root', 'password': 'password123'},\n]\n\n# \u6267\u884c\u547d\u4ee4\ncommand = '\u4f60\u9700\u8981\u6267\u884c\u7684\u547d\u4ee4'\nfor server in server_list:\n    execute_command(server['hostname'], server['port'], server['username'], server['password'], command)\n<\/pre>\n<p class=\"wp-block-paragraph\">\u4f60\u4e5f\u53ef\u4ee5\u8bb0\u5f55output\u770b\u770b\u8fd4\u56de\u4e86\u4ec0\u4e48\uff0c\u6709\u65f6\u5019\u662f\u7a7a\u5c31\u610f\u5473\u7740\u6ca1\u6709\u62a5\u9519\uff0c\u5177\u4f53\u7684\u81ea\u5df1\u4fee\u6539\u5427<\/p>","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u6279\u91cf\u6267\u884c\u4e00\u4e9b\u547d\u4ee4\uff0c\u6bd4\u5982\u7f51\u7edc\u8bbe\u5907\u6279\u91cf\u5f00\u5c40\uff0c\u670d\u52a1\u5668\u5b9a\u65f6\u91cd\u542f\u670d\u52a1\u7c7b\u4f3c\u7684\u4efb\u52a1\uff0c\u5982\u679c\u8981\u624b\u52a8\u7684\u8bdd\u5c31\u4f1a\u6bd4\u8f83\u8d39\u65f6\u8d39\u529b\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e00\u4e2apyth &hellip;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,5,10],"tags":[40],"class_list":["post-210","post","type-post","status-publish","format-standard","hentry","category-linux","category-python","category-10","tag-python"],"views":513,"_links":{"self":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=210"}],"version-history":[{"count":2,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":212,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=\/wp\/v2\/posts\/210\/revisions\/212"}],"wp:attachment":[{"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.liaoxinghui.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}