{"id":1285,"date":"2018-07-26T07:11:48","date_gmt":"2018-07-26T07:11:48","guid":{"rendered":"https:\/\/www.ipcpu.com\/?p=1285"},"modified":"2019-12-26T07:12:52","modified_gmt":"2019-12-26T07:12:52","slug":"python-paramiko","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2018\/07\/python-paramiko\/","title":{"rendered":"Python\u7684Paramiko\u6a21\u5757\u4ecb\u7ecd"},"content":{"rendered":"

Python\u7684Paramiko\u6a21\u5757\u4ecb\u7ecd.md<\/p>\n

\u6982\u8ff0<\/h2>\n

paramiko\u662f\u7528python\u8bed\u8a00\u5199\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u9075\u5faaSSH2\u534f\u8bae\uff0c\u652f\u6301\u4ee5\u52a0\u5bc6\u548c\u8ba4\u8bc1\u7684\u65b9\u5f0f\uff0c\u8fdb\u884c\u8fdc\u7a0b\u670d\u52a1\u5668\u7684\u8fde\u63a5\u3002paramiko\u652f\u6301Linux, Solaris, BSD, MacOS X, Windows\u7b49\u5e73\u53f0\u901a\u8fc7SSH\u4ece\u4e00\u4e2a\u5e73\u53f0\u8fde\u63a5\u5230\u53e6\u5916\u4e00\u4e2a\u5e73\u53f0\u3002\u5229\u7528\u8be5\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u7684\u8fdb\u884cssh\u8fde\u63a5\u548csftp\u534f\u8bae\u8fdb\u884csftp\u6587\u4ef6\u4f20\u8f93\u3002<\/p>\n

\u4e00\u3001paramiko\u6a21\u5757\u7684\u5b89\u88c5<\/h2>\n

\u76f4\u63a5\u4f7f\u7528pip\u5b89\u88c5\u5373\u53ef<\/p>\n

\n
 pip3 install paramiko <\/code><\/pre>\n<\/div>\n

\u4e8c\u3001\u4f7f\u7528paramiko\u521b\u5efaSSH\u8fde\u63a5<\/h2>\n

\u4f7f\u7528paramiko\u6a21\u5757\u6709\u4e24\u79cd\u8fde\u63a5\u65b9\u5f0f\uff0c\u4e00\u79cd\u662f\u901a\u8fc7paramiko.SSHClient()\u51fd\u6570\uff0c\u53e6\u5916\u4e00\u79cd\u662f\u901a\u8fc7paramiko.Transport()\u51fd\u6570\u3002\u6211\u4eec\u4ee5SSHClient()\u4e3e\u4f8b\uff1a<\/p>\n

\n
import paramiko\nimport socket\n\nhostname = '10.140.12.45'\nport = 22222\nusername = 'root'\npassword = 'sohu.com'\n\n#key = paramiko.RSAKey.from_private_key_file('\/root\/.ssh\/id_rsa')\ns = paramiko.SSHClient()\ns.load_system_host_keys()\ns.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n\ntry:\n    #s.connect(hostname=hostname, port=port, username=username, pkey=key)\n    s.connect(hostname=hostname, port=port, username=username, password=password)\n    stdin, stdout, stderr = s.exec_command('\/bin\/bash \/tmp\/publish81.sh', timeout=10)\n    #\u8d85\u65f6\u53c2\u6570\u4e0d\u592a\u597d\u4f7f\uff0c\u9700\u8981\u786e\u4fdd\u811a\u672c\u5bb9\u9519\u6027\u548c\u8d85\u65f6\u8bbe\u7f6e\u3002\n    ret =  stdout.read()\n    status = stdout.channel.recv_exit_status()\n    print(status,ret)\nexcept (paramiko.BadHostKeyException, paramiko.AuthenticationException, paramiko.SSHException,socket.error ) as e:\n    print(e)\n\ns.close()<\/code><\/pre>\n<\/div>\n

\u4e09\u3001\u4f7f\u7528paramiko\u4f20\u6587\u4ef6<\/h2>\n

Paramiko\u9664\u4e86\u6267\u884c\u547d\u4ee4\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u7528\u6765\u4e0a\u4f20\u4e0b\u8f7d\u6587\u4ef6\u3002\u4e00\u822c\u9009\u62e9SCP\u6216\u8005SFTP\u3002<\/p>\n

\n
import paramiko\n\nhostname = '10.140.12.45'\nport = 22222\nusername = 'root'\npassword = 'sohu.com'\n\nscp = paramiko.Transport((hostname, port))  # 1\nscp.connect(username=username, password=password)  # 2\nsftp = paramiko.SFTPClient.from_transport(scp)  # 3\n#sftp.get('\/.ssh\/authorized_keys', '\/usr\/li\/authorized_keys')\nsftp.put('mmp.py', '\/tmp\/lili')\nsftp.close()<\/code><\/pre>\n<\/div>\n

\u56db\u3001paramiko\u53d1\u8d77\u7684shell\u7684\u7c7b\u578b<\/h2>\n

Paramiko\u53d1\u8d77\u7684shell\u7c7b\u578b\u662fnon-interactive\u548clogin\uff0c\u56e0\u6b64\u4e0d\u4f1a\u8f7d\u5165\u5e38\u89c1\u7684\u73af\u5883\u53d8\u91cf\u914d\u7f6e\u6587\u4ef6\uff0c\u5982\u4e0b\u3002
\n
\n\u7f51\u4e0a\u6709\u4f7f\u7528paramiko\u5f00\u542f\u4ea4\u4e92\u5f0fshell\u7684\u65b9\u6cd5\uff0c\u5927\u5bb6\u53ef\u4ee5\u81ea\u884c\u67e5\u627e\u3002<\/p>\n

<\/p>\n

<\/div>\n
<\/div>\n
\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n<\/div>\n
\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n<\/div>\n

<\/wiz_tmp_tag><\/p>\n

\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » Python\u7684Paramiko\u6a21\u5757\u4ecb\u7ecd<\/a><\/p>","protected":false},"excerpt":{"rendered":"

Python\u7684Paramiko\u6a21\u5757\u4ecb\u7ecd.md \u6982\u8ff0 paramiko\u662f\u7528python\u8bed\u8a00\u5199\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u9075\u5faaSSH2\u534f\u8bae\uff0c\u652f\u6301\u4ee5\u52a0\u5bc6\u548c\u8ba4\u8bc1\u7684\u65b9\u5f0f\uff0c\u8fdb\u884c\u8fdc\u7a0b\u670d\u52a1\u5668\u7684\u8fde\u63a5\u3002paramiko\u652f\u6301Linux, Solaris, BSD, MacOS X, Windows\u7b49\u5e73\u53f0\u901a\u8fc7SSH\u4ece\u4e00\u4e2a\u5e73\u53f0\u8fde\u63a5\u5230\u53e6\u5916\u4e00\u4e2a\u5e73\u53f0\u3002\u5229\u7528\u8be5\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u7684\u8fdb\u884cssh\u8fde\u63a5\u548csftp\u534f\u8bae\u8fdb\u884csftp\u6587\u4ef6\u4f20\u8f93\u3002 \u4e00\u3001paramiko\u6a21\u5757\u7684\u5b89\u88c5 \u76f4\u63a5\u4f7f\u7528pip\u5b89\u88c5\u5373\u53ef pip3 install paramiko \u4e8c\u3001\u4f7f\u7528paramiko\u521b\u5efaSSH\u8fde\u63a5 \u4f7f\u7528paramiko\u6a21\u5757\u6709\u4e24\u79cd\u8fde\u63a5\u65b9\u5f0f\uff0c\u4e00\u79cd\u662f\u901a\u8fc7paramiko.SSHClient()\u51fd\u6570\uff0c\u53e6\u5916\u4e00\u79cd\u662f\u901a\u8fc7paramiko.Transport()\u51fd\u6570\u3002\u6211\u4eec\u4ee5SSHClient()\u4e3e\u4f8b\uff1a import paramiko import socket hostname = ‘10.140.12.45’ port = 22222 username = ‘root’ password = ‘sohu.com’ #key = paramiko.RSAKey.from_private_key_file(‘\/root\/.ssh\/id_rsa’) s = paramiko.SSHClient() s.load_system_host_keys() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: #s.connect(hostname=hostname, port=port, username=username, pkey=key) s.connect(hostname=hostname, port=port, username=username, password=password) stdin, stdout, stderr = s.exec_command(‘\/bin\/bash \/tmp\/publish81.sh’, timeout=10) […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[204,59],"_links":{"self":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1285"}],"collection":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/comments?post=1285"}],"version-history":[{"count":1,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1285\/revisions"}],"predecessor-version":[{"id":1286,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1285\/revisions\/1286"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=1285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=1285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=1285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}