{"id":1924,"date":"2023-11-21T10:54:09","date_gmt":"2023-11-21T02:54:09","guid":{"rendered":"http:\/\/zhang.mba\/?p=1924"},"modified":"2023-11-21T10:57:25","modified_gmt":"2023-11-21T02:57:25","slug":"paramiko-shi-xiansshv2xie-yi-de-ke-hu-duan-he-fu-w","status":"publish","type":"post","link":"http:\/\/zhang.mba\/index.php\/2023\/11\/21\/10\/54\/09\/1924\/paramiko-shi-xiansshv2xie-yi-de-ke-hu-duan-he-fu-w\/python\/zhangzhiqi\/","title":{"rendered":"Paramiko\uff1a\u5b9e\u73b0SSHv2\u534f\u8bae\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7684\u5e93"},"content":{"rendered":"<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>Python\u7684Paramiko\u5e93\uff0c\u5b83\u662f\u4e00\u4e2a\u7528\u4e8e\u5b9e\u73b0SSHv2\u534f\u8bae\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7684\u5e93\u3002\u901a\u8fc7\u4f7f\u7528Paramiko\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u7a0b\u5e8f\u4e2d\u8f7b\u677e\u5730\u5b9e\u73b0\u8fdc\u7a0b\u670d\u52a1\u5668\u7684\u7ba1\u7406\u3001\u6587\u4ef6\u4f20\u8f93\u7b49\u529f\u80fd\u3002\u7279\u522b\u505a\u667a\u80fd\u786c\u4ef6\u4ea7\u54c1\u7684\u540c\u5b66\u8981\u719f\u6089\u5b83\uff0c\u56e0\u4e3a\u5b83\u80fd\u4e3a\u4f60\u51cf\u5c11\u5f88\u591a\u9ebb\u70e6\uff0c\u5728\u6211\u4ee5\u524d\u7684\u5de5\u4f5c\u4e2d\uff0c\u5bf9\u667a\u80fd\u786c\u4ef6\u505a\u5404\u79cd\u6d4b\u8bd5\u5c31\u662f\u901a\u8fc7Paramiko\u8fde\u63a5\u8bbe\u5907\u4e0b\u53d1\u5404\u79cd\u547d\u4ee4<\/p>\n<p><strong>\u4e00.\u5b89\u88c5<\/strong><\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">pip install paramiko\n<\/code><\/pre>\n<p><strong>\u4e8c.\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<p>\u00a0\u00a0\u00a0\u00a01.\u521b\u5efa\u4e00\u4e2assh\u8fde\u63a5\u5ba2\u6237\u7aef<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">import paramiko\n\nssh = paramiko.SSHClient()\nssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n<\/code><\/pre>\n<p>2.\u6dfb\u52a0\u670d\u52a1\u5668\u7684SSH\u5bc6\u94a5\uff08\u53ef\u9009\uff09<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n<\/code><\/pre>\n<p>2.connect()\u521b\u5efa\u8fde\u63a5\uff0c\u8fde\u63a5\u8fdc\u7a0b\u670d\u52a1\uff0c\u5404\u79cd\u667a\u80fd\u786c\u4ef6\uff0c\u670d\u52a1\u5668\u7b49<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">hostname = '192.168.31.230'\nport = 22\nusername = 'kali'\npassword = 'kali'\nssh.connect(hostname, port, username, password)\n<\/code><\/pre>\n<p>3.\u6267\u884c\u8fdc\u7a0b\u547d\u4ee4<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">stdin, stdout, stderr = ssh.exec_command('ls')\nprint(stdout.read().decode()) # \u6253\u5370\u8f93\u51fa\u7ed3\u679c\nprint(stderrh.read().decode())# \u8f93\u51fa\u9519\u8bef\u4fe1\u606f\n# \u8fd4\u56de\u7ed3\u679c \nDesktop\nDocuments\nDownloads\nMusic\nPictures\nPublic\nTemplates\nVideos\n<\/code><\/pre>\n<p>4.\u6587\u4ef6\u4e0b\u8f7d<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">local_file = 'D:\/code\/ssh\/local_file.txt'  # \u672c\u5730\u6587\u4ef6\u8def\u5f84\u548c\u540d\u79f0\nremote_file = '\/home\/kali\/remote_file.txt'  # \u8fdc\u7a0b\u6587\u4ef6\u8def\u5f84\u548c\u540d\u79f0\nsftp = ssh.open_sftp()  # \u6253\u5f00SFTP\u4f1a\u8bdd\nsftp.get(remote_file, local_file)  # \u4ece\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0b\u8f7d\u6587\u4ef6\u5230\u672c\u5730\nsftp.close()  # \u5173\u95edSFTP\u4f1a\u8bdd\n<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/mmbiz.qpic.cn\/mmbiz_png\/84ohWMaAlOpSz0eqbEaU9vIbVwjHcwxZyLFDoc59OoYYcg2tlSIeyhKiap1rZM93Cdib4lZkpL72ASPoeqQuuXtw\/640?wx_fmt=png&amp;from=appmsg&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/84ohWMaAlOpSz0eqbEaU9vIbVwjHcwxZyLFDoc59OoYYcg2tlSIeyhKiap1rZM93Cdib4lZkpL72ASPoeqQuuXtw\/640?wx_fmt=png&amp;from=appmsg&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\u56fe\u7247\" \/><\/div><\/p>\n<p>5.\u6587\u4ef6\u4e0a\u4f20<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">local_file = 'D:\/code\/ssh\/local_file2.txt'  # \u672c\u5730\u6587\u4ef6\u8def\u5f84\u548c\u540d\u79f0\nremote_file = '\/home\/kali\/remote_file2.txt'  # \u8fdc\u7a0b\u6587\u4ef6\u8def\u5f84\u548c\u540d\u79f0\nsftp = ssh.open_sftp()  # \u6253\u5f00SFTP\u4f1a\u8bdd\nsftp.put(local_file, remote_file)  # \u4e0a\u4f20\u672c\u5730\u6587\u4ef6\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\nsftp.close()  # \u5173\u95edSFTP\u4f1a\u8bdd\n<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/mmbiz.qpic.cn\/mmbiz_png\/84ohWMaAlOpSz0eqbEaU9vIbVwjHcwxZmIshxbJDGTwHK4sgK2CyO1E7WAtdaXPPXCyEeMpWhEmqV000nzJatw\/640?wx_fmt=png&amp;from=appmsg&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/84ohWMaAlOpSz0eqbEaU9vIbVwjHcwxZmIshxbJDGTwHK4sgK2CyO1E7WAtdaXPPXCyEeMpWhEmqV000nzJatw\/640?wx_fmt=png&amp;from=appmsg&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\u56fe\u7247\" \/><\/div> \u00a0<\/p>\n<p>6.\u5173\u95ed\u8fde\u63a5<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">ssh.close() #\u5173\u95ed\u8fde\u63a5\n<\/code><\/pre>\n<p><strong>\u4e09.\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b<\/strong><\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">import paramiko\n\nssh = paramiko.SSHClient() # \u521b\u5efa\u5ba2\u670d\u7aef\nssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # \u6dfb\u52a0\u79d8\u94a5\n\nhostname = '192.168.31.230'\nport = 22\nusername = 'kali'\npassword = 'kali'\n\nssh.connect(hostname, port, username, password) # \u521b\u5efa\u8fde\u63a5\n\nstdin, stdout, stderr = ssh.exec_command('ls') # \u6267\u884cls\nprint(stdout.read().decode())\n\n# \u4e0b\u8f7d\nlocal_file = 'D:\/code\/ssh\/local_file.txt'  # \u672c\u5730\u6587\u4ef6\u8def\u5f84\u548c\u540d\u79f0\nremote_file = '\/home\/kali\/remote_file.txt'  # \u8fdc\u7a0b\u6587\u4ef6\u8def\u5f84\u548c\u540d\u79f0\nsftp = ssh.open_sftp()  # \u6253\u5f00SFTP\u4f1a\u8bdd\nsftp.get(remote_file, local_file)  # \u4ece\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0b\u8f7d\u6587\u4ef6\u5230\u672c\u5730\n\n\n# \u4e0a\u4f20\nlocal_file2 = 'D:\/code\/ssh\/local_file2.txt'  # \u672c\u5730\u6587\u4ef6\u8def\u5f84\u548c\u540d\u79f0\nremote_file2 = '\/home\/kali\/remote_file2.txt'  # \u8fdc\u7a0b\u6587\u4ef6\u8def\u5f84\u548c\u540d\u79f0\nsftp = ssh.open_sftp()  # \u6253\u5f00SFTP\u4f1a\u8bdd\nsftp.put(local_file2, remote_file2)  # \u4e0a\u4f20\u672c\u5730\u6587\u4ef6\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\nsftp.close()  # \u5173\u95edSFTP\u4f1a\u8bdd\nssh.close() # \u5173\u95ed\u8fde\u63a5\n<\/code><\/pre>\n<p>\u6765\u81ea\u2014\u2014\u2014\u2014\u2014\u534a\u4e2a\u7801\u519c<\/p>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 Python\u7684Paramiko\u5e93\uff0c\u5b83\u662f\u4e00\u4e2a\u7528\u4e8e\u5b9e\u73b0SSHv2\u534f\u8bae\u7684\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7684\u5e93\u3002\u901a\u8fc7\u4f7f\u7528Paramiko\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u7a0b\u5e8f\u4e2d\u8f7b\u677e\u5730\u5b9e\u73b0\u8fdc\u7a0b\u670d\u52a1\u5668\u7684\u7ba1\u7406\u3001\u6587\u4ef6\u4f20\u8f93\u7b49\u529f\u80fd\u3002\u7279\u522b\u505a\u667a\u80fd\u786c\u4ef6\u4ea7\u54c1\u7684\u540c\u5b66\u8981\u719f\u6089\u5b83\uff0c\u56e0\u4e3a\u5b83\u80fd\u4e3a\u4f60\u51cf\u5c11\u5f88\u591a\u9ebb\u70e6\uff0c\u5728\u6211\u4ee5\u524d\u7684\u5de5\u4f5c\u4e2d\uff0c\u5bf9 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[57,12],"tags":[54],"_links":{"self":[{"href":"http:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1924"}],"collection":[{"href":"http:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/comments?post=1924"}],"version-history":[{"count":0,"href":"http:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1924\/revisions"}],"wp:attachment":[{"href":"http:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/media?parent=1924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/categories?post=1924"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/tags?post=1924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}