{"id":131205,"date":"2019-01-10T10:00:52","date_gmt":"2019-01-10T02:00:52","guid":{"rendered":"https:\/\/gulass.cn\/?p=131205"},"modified":"2019-01-02T11:09:47","modified_gmt":"2019-01-02T03:09:47","slug":"python-paramiko-work","status":"publish","type":"post","link":"https:\/\/gulass.cn\/python-paramiko-work.html","title":{"rendered":"Python Paramiko\u591a\u8fdb\u7a0b\u5de5\u4f5c"},"content":{"rendered":"
\u4ec0\u4e48\u662f\u8fdb\u7a0b\uff1f<\/strong><\/span><\/div>\n

\u8fdb\u7a0b\u662f\u6307\u7cfb\u7edf\u4e2d\u6b63\u5728\u8fd0\u884c\u7684\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u3002\u8fdb\u7a0b\u5c31\u597d\u6bd4\u5de5\u5382\u7684\u8f66\u95f4\uff0c\u5b83\u4ee3\u8868cpu\u6240\u80fd\u5904\u7406\u7684\u5355\u4e2a\u4efb\u52a1\u3002\u4efb\u4e00\u65f6\u523b\uff0ccpu\u603b\u662f\u8fd0\u884c\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u5176\u5b83\u8fdb\u7a0b\u5904\u4e8e\u975e\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n

\u4e00\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u5b83\u80af\u5b9a\u6709\u591a\u4e2a\u8fdb\u7a0b\uff0c\u4f46\u662f\u591a\u4e2a\u8fdb\u7a0b\u5b83\u662f\u4e92\u4e0d\u5e72\u6270\u7684\u3002\u540c\u4e00\u65f6\u523b\u53ea\u80fd\u6709\u4e00\u4e2a\u8fdb\u7a0b\u5728\u8fd0\u884c\uff0c\u6211\u4eec\u770b\u5230\u7684\u5c31\u662f\u4e00\u4e2a\u6d4f\u89c8\u5668Chrome\uff0c\u5b83\u7684\u4e00\u4e2a\u8fdb\u7a0b\u7684\u6d88\u606f\u7684\u4e00\u4e2a\u6a21\u578b\uff0c\u5c31\u662f\u8bf4\u5b83\u6bcf\u4e2a\u6807\u7b7e\u9875\u5176\u5b9e\u662f\u5355\u72ec\u5f00\u7684\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u5b83\u5c31\u8fbe\u5230\u4e86\u4e00\u79cd\u4e92\u4e0d\u5e72\u6270\u7684\uff0c\u5982\u679c\u4f60\u6b63\u5728\u770b\u8fd9\u4e2a\u6807\u7b7e\u9875\u7684\u8bdd\uff0c\u5b83\u4e0d\u4f1a\u5f71\u54cd\u5176\u5b83\u6807\u7b7e\u9875\u7684\u60c5\u51b5\uff0c\u8fd9\u4e2a\u5c31\u662f\u4e00\u4e2a\u5229\u7528\u591a\u8fdb\u7a0b\u7684\u4f8b\u5b50\u3002<\/p>\n

\u4e3a\u4ec0\u4e48\u4f7f\u7528\u591a\u8fdb\u7a0b\uff1f<\/strong><\/span><\/div>\n

Python\u4e2d\u7684\u591a\u7ebf\u7a0b\u5176\u5b9e\u786c\u4e0d\u662f\u6b63\u771f\u7684\u591a\u7ebf\u7a0b\uff0c\u5982\u679c\u60f3\u8981\u5145\u5206\u5730\u4f7f\u7528\u591a\u6838cpu\u7684\u8d44\u6e90\uff0c\u5728Python\u4e2d\u5927\u90e8\u5206\u60c5\u51b5\u9700\u8981\u4f7f\u7528\u591a\u8fdb\u7a0b\u3002<\/p>\n

Python\u63d0\u4f9b\u4e86\u975e\u5e38\u597d\u7528\u7684\u591a\u8fdb\u7a0b\u5305multiprocessing\uff0c\u53ea\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0cPython\u4f1a\u5b8c\u6210\u5176\u5b83\u6240\u6709\u4e8b\u60c5\u3002\u501f\u52a9\u8fd9\u4e2a\u5305\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b8c\u6210\u4ece\u5355\u8fdb\u7a0b\u5e76\u53d1\u6267\u884c\u7684\u8f6c\u6362\u3002<\/p>\n

\u5982\u4f55\u4f7f\u7528\u591a\u8fdb\u7a0b\u5b9e\u73b0Paramiko\u64cd\u4f5c\uff1f<\/strong><\/span><\/div>\n

\u4e0b\u9762\u5f88\u7b80\u5355\u5730\u5b9e\u73b0\u4e00\u4e2a\u591a\u8fdb\u7a0b\u7248\u672c\u7684paramiko\u8fde\u63a5\u8fdc\u7a0b\u670d\u52a1\u5668\u3002\u672c\u7ae0\u662f\u63a5\u7740Python\u8fdc\u7a0b\u8fde\u63a5\u6a21\u5757Paramiko\u4f7f\u7528\u8fd9\u7bc7\u6587\u7ae0\u6765\u5b8c\u6210\u591a\u8fdb\u7a0b\u5b9e\u73b0paramiko\u64cd\u4f5c\u7684\u3002<\/p>\n

\u4e4b\u524d\u6211\u5efa\u7acb\u4e00\u4e2a\u6587\u4ef6\uff0c\u53eb\u505aconfig.conf<\/p>\n

\r\n[ssh]\r\nhost=10.10.0.112\r\nport=22\r\nusername=root\r\npassword=123456\r\ntimeout=1.0\r\n<\/pre>\n

\u88ab\u5c01\u88c5\u8fc7\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\nimport paramiko\r\nimport configparser\r\n \r\nclass ParamikoClient:\r\n    def __init__(self, file):\r\n        self.client = paramiko.SSHClient()\r\n        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())\r\n        self.config = configparser.ConfigParser()\r\n        self.config.read(file)\r\n \r\n    def connect(self):\r\n        try:\r\n            self.client.connect(\r\n                hostname = self.config.get('ssh','host'), \r\n                port = self.config.getint('ssh','port'), \r\n                username = self.config.get('ssh','username'), \r\n                password = self.config.get('ssh','password'), \r\n                timeout = self.config.getfloat('ssh','timeout')\r\n            )\r\n        except Exception as e:\r\n            print(e)\r\n            try:\r\n                self.client.close()\r\n            except:\r\n                pass\r\n \r\n    def runcmd(self, cmd):\r\n        stdin, stdout, stderr = self.client.exec_command(cmd)\r\n        return stdout.read().decode()\r\n<\/pre>\n

\u5355\u8fdb\u7a0b\u76f4\u63a5\u8fd9\u4e48\u4f7f\u7528\u5373\u53ef\uff1a<\/p>\n

\r\nfrom ParamikoClient import ParamikoClient\r\n \r\ndef process():\r\n    client = ParamikoClient('config.conf')\r\n    client.connect()\r\n    client.runcmd('date')\r\n \r\nif __name__ == '__main__':\r\n    process()\r\n<\/pre>\n

\u8fd9\u5c31\u662f\u5728\u4e00\u4e2a\u8fdb\u7a0b\u5185\u6765\u5904\u7406\u8fd9\u4e2aparamiko\u7684\u64cd\u4f5c\u3002\u4e00\u4e2aprocess\u5b83\u5176\u5b9e\u5728\u8fd9\u4e2amain\u51fd\u6570\u91cc\u9762\uff0c\u5b83\u5c31\u5728\u8fd9\u4e2a\u8fdb\u7a0b\u91cc\u9762\u8fd0\u884c\u4e86\uff0c\u7136\u540e\u5982\u679c\u6211\u4eec\u8981\u7528\u591a\u4e2a\u8fdb\u7a0b\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u8bbe\u8ba1config\u3002<\/p>\n

\r\n[ssh1]\r\nhost=10.10.0.112\r\nport=22\r\nusername=root\r\npassword=123456\r\ntimeout=1.0\r\n \r\n[ssh2]\r\nhost=10.10.0.112\r\nport=22\r\nusername=root\r\npassword=123456\r\ntimeout=1.0\r\n \r\n[ssh3]\r\nhost=10.10.0.112\r\nport=22\r\nusername=root\r\npassword=123456\r\ntimeout=1.0\r\n<\/pre>\n

\u6211\u4eec\u53ef\u4ee5\u8ba9\u5b83\u6709\u591a\u4e2asection\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u8fd0\u884c\u4e00\u4e2asection\u3002<\/p>\n

\u9996\u5148\u6539\u52a8ParamikoClient.py\u811a\u672c\uff0c\u5982\u4e0b\uff1a<\/p>\n

\r\nimport paramiko\r\nimport configparser\r\n \r\nclass ParamikoClient:\r\n    def __init__(self, file, section):\r\n        self.client = paramiko.SSHClient()\r\n        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())\r\n        self.config = configparser.ConfigParser()\r\n        self.config.read(file)\r\n        self.section = section\r\n \r\n    def connect(self):\r\n        try:\r\n            self.client.connect(\r\n                hostname = self.config.get(self.section,'host'), \r\n                port = self.config.getint(self.section,'port'), \r\n                username = self.config.get(self.section,'username'), \r\n                password = self.config.get(self.section,'password'), \r\n                timeout = self.config.getfloat(self.section,'timeout')\r\n            )\r\n        except Exception as e:\r\n            print(e)\r\n            try:\r\n                self.client.close()\r\n            except:\r\n                pass\r\n \r\n    def runcmd(self, cmd):\r\n        stdin, stdout, stderr = self.client.exec_command(cmd)\r\n        return stdout.read().decode()\r\n<\/pre>\n

\u7136\u540e\u5199\u4e00\u4e2a\u591a\u8fdb\u7a0b\u8c03\u7528\u811a\u672c\uff0c\u5982\u4e0b\uff1a<\/p>\n

\r\nfrom ParamikoClient import ParamikoClient\r\nfrom multiprocessing import Pool\r\nimport time\r\n \r\ndef process(section):\r\n    client = ParamikoClient('config.conf', section)\r\n    client.connect()\r\n    a = client.runcmd('date')\r\n    print(a, end='')\r\n \r\ntask_num = 3\r\n \r\nif __name__ == '__main__':\r\n    # single process time;\r\n    start = time.time()\r\n    pool = Pool()\r\n    process('ssh1')\r\n    process('ssh2')\r\n    process('ssh3')\r\n    print(time.time() - start)\r\n \r\n    # multi process time;\r\n    start = time.time()\r\n    task_num = task_num + 1\r\n    for i in range(1, task_num):\r\n        section = 'ssh'+str(i)\r\n        pool.apply_async(process, args=(section, ))\r\n    pool.close()\r\n    pool.join()\r\n    print(time.time() - start)\r\n<\/pre>\n

\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

\r\n$ python process.py \r\nTue Oct 24 04:29:10 EDT 2017\r\nTue Oct 24 04:29:10 EDT 2017\r\nTue Oct 24 04:29:10 EDT 2017\r\n0.6342382431030273\r\nTue Oct 24 04:29:11 EDT 2017\r\nTue Oct 24 04:29:11 EDT 2017\r\nTue Oct 24 04:29:11 EDT 2017\r\n0.2790086269378662\r\n<\/pre>\n

\u4ece\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\u65f6\u95f4\u7f29\u77ed\u4e862\/3\u3002\u591a\u8fdb\u7a0b\u5b83\u7684\u4e00\u4e2a\u7279\u70b9\uff0c\u5c31\u662f\u8bf4\u5b83\u6709\u5b83\u7684\u5e76\u53d1\u6027\uff0c\u5c31\u662f\u8bf4\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\u4e92\u4e0d\u5f71\u54cd\u5b83\u4eec\u662f\u540c\u662f\u8fdb\u884c\u7684\u3002\u53ea\u8981\u4f60\u7684\u673a\u5b50\u662f\u591a\u6838\uff0c\u5b83\u5c31\u4f1a\u5145\u5206\u5229\u7528\u591a\u6838\u7684\u4f18\u52bf\u6765\u8fdb\u884c\u67e5\u8be2\uff0c\u7136\u540e\u6211\u4eec\u5c31\u770b\u5230\u4e86\u591a\u8fdb\u7a0b\u64cd\u4f5c\uff0c\u8fdc\u7a0b\u673a\u5668\u7684\u8fd9\u4e48\u4e00\u4e2a\u4f18\u70b9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u8fdb\u7a0b\u662f\u6307\u7cfb\u7edf\u4e2d\u6b63\u5728\u8fd0\u884c\u7684\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u3002\u8fdb\u7a0b\u5c31\u597d\u6bd4\u5de5\u5382\u7684\u8f66\u95f4\uff0c\u5b83\u4ee3\u8868cpu\u6240\u80fd\u5904\u7406\u7684\u5355\u4e2a\u4efb\u52a1\u3002\u4efb\u4e00\u65f6\u523b\uff0ccpu\u603b […]<\/p>\n","protected":false},"author":1479,"featured_media":28057,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-131205","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/131205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=131205"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/131205\/revisions"}],"predecessor-version":[{"id":132934,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/131205\/revisions\/132934"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/28057"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=131205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=131205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=131205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}