{"id":202,"date":"2023-02-22T22:19:42","date_gmt":"2023-02-22T14:19:42","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=202"},"modified":"2023-04-30T15:29:42","modified_gmt":"2023-04-30T07:29:42","slug":"python-simple-implementation-of-ssh","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/22\/python-simple-implementation-of-ssh\/","title":{"rendered":"Python\u4e0bSSH\u7684\u7b80\u5355\u5b9e\u73b0"},"content":{"rendered":"<h2>Python\u5b9e\u73b0SSH<\/h2>\n<h3>Linux\u4e0b\u7684ssh\u767b\u5f55<\/h3>\n<p><!-- more --><\/p>\n<pre><code class=\"language-shell\">root@ubuntu:~# ssh root@192.168.1.88\nThe authenticity of host &#039;192.168.1.88 (192.168.1.88)&#039; can&#039;t be established.\nECDSA key fingerprint is SHA256:\/ufx+\/OLtdsYy7vsdk4KDu9xJsBp6zHonRAf2jjT0GI.\nAre you sure you want to continue connecting (yes\/no)? y\nPlease type &#039;yes&#039; or &#039;no&#039;: yes\nWarning: Permanently added &#039;192.168.1.88&#039; (ECDSA) to the list of known hosts.\nPassword:\nLast login: Fri Dec  2 14:15:36 2016\nlocalhost:~ root$ <\/code><\/pre>\n<p>\u67e5\u770bknown_hosts\u6587\u4ef6<\/p>\n<pre><code class=\"language-shell\">root@ubuntu:~# cat .ssh\/known_hosts \n|1|ohKgSMq+1NLbr37anPqNZKQxh\/8=|83NZfZYMUwLGaH32oLhCW\/2PsXk= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMTpJEzRyK6Bn96JT9+8IMK57ouWZspbs+oVuc7lD+aAwDB6C9Qgoy8P7cGjRaOA5ImDiBTLSQgHT+cZeGIJbI4=<\/code><\/pre>\n<h3>Python\u5b9e\u73b0ssh<\/h3>\n<pre><code class=\"language-python\">import paramiko\n\n#\u521b\u5efaSSH\u5bf9\u8c61\nssh = paramiko.SSHClient()\n\n#\u628a\u8981\u8fde\u63a5\u7684\u673a\u5668\u6dfb\u52a0\u5230known_hosts\u6587\u4ef6\u4e2d\nssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n\n#\u8fde\u63a5\u670d\u52a1\u5668\nssh.connect(hostname=&#039;192.168.1.88&#039;, port=22, username=&#039;root&#039;, password=&#039;123456&#039;)\n\ncmd = &#039;ps&#039;\n#cmd = &#039;ls -l;ifconfig&#039;       #\u591a\u4e2a\u547d\u4ee4\u7528;\u9694\u5f00\nstdin, stdout, stderr = ssh.exec_command(cmd)\n\nresult = stdout.read()\n\nif not result:\n    result = stderr.read()\nssh.close()\n\nprint(result.decode())<\/code><\/pre>\n<h3>Python\u5b9e\u73b0sftp<\/h3>\n<pre><code class=\"language-python\">import paramiko\n\ntransport = paramiko.Transport((&#039;192.168.1.88&#039;, 22))\n\ntransport.connect(username=&#039;root&#039;, password=&#039;123456&#039;)\n\nsftp = paramiko.SFTPClient.from_transport(transport)\n\nsftp.put(&#039;123.py&#039;, &#039;\/tmp\/test.py&#039;)  # \u5c06123.py \u4e0a\u4f20\u81f3\u670d\u52a1\u5668 \/tmp\u4e0b\u5e76\u6539\u540d\u4e3atest.py\n\nsftp.get(&#039;remove_path&#039;, &#039;local_path&#039;)  # \u5c06remove_path \u4e0b\u8f7d\u5230\u672c\u5730 local_path\n\ntransport.close()<\/code><\/pre>\n<h2>\u4f20\u7edf\u7684\u514d\u5bc6\u767b\u5f55<\/h2>\n<h3>\u751f\u6210\u516c\u94a5<\/h3>\n<p>\u4f7f\u7528ssh-keygen\u751f\u4ea7\u516c\u94a5\u548c\u79c1\u94a5<\/p>\n<pre><code class=\"language-shell\">root@ubuntu:~$ ssh-keygen\nGenerating public\/private rsa key pair.\nEnter file in which to save the key (\/home\/root\/.ssh\/id_rsa): \nCreated directory &#039;\/home\/root\/.ssh&#039;.\nEnter passphrase (empty for no passphrase): \nEnter same passphrase again: \nYour identification has been saved in \/home\/root\/.ssh\/id_rsa.\nYour public key has been saved in \/home\/root\/.ssh\/id_rsa.pub.\nThe key fingerprint is:\nSHA256:8IVM56bc\/2deLZcDXwVs2XDNCuTTbvvlCKmCnUxiHuw root@ubuntu\nThe key&#039;s randomart image is:\n+---[RSA 2048]----+\n|        . ....o=o|\n|       o + ...+o+|\n|      . o + oo...|\n|       + =   o. .|\n|     .  S .  .o .|\n|      = .  . oo.+|\n|     + B .  + o+=|\n|      E =  . o **|\n|         ..   o++|\n+----[SHA256]-----+\n\n#\u751f\u6210\u7684\u516c\u94a5\u79c1\u94a5\u5b58\u653e\u5728.ssh\u76ee\u5f55\u4e0b\uff0cid_rsa.pub\u5c31\u662f\u672c\u673a\u7684\u516c\u94a5\nroot@ubuntu:~$ cd .ssh\/\nroot@ubuntu:~\/.ssh$ ls\nid_rsa  id_rsa.pub<\/code><\/pre>\n<h3>\u590d\u5236\u516c\u94a5<\/h3>\n<p>\u6700\u540e\u628a\u672c\u673a\u7684id_rsa.pub\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9copy\u5230\u76ee\u6807\u673a\u7684.ssh\/authorized_keys\u4e0b\u5373\u53ef\uff0c\u5982\u679c\u6ca1\u6709authorized_keys\uff0c\u81ea\u5df1\u521b\u5efa\u3002\u4f46\u662f\u8981\u6ce8\u610fauthorized_keys\u7684\u6743\u9650\u4e00\u822c\u662f600<\/p>\n<pre><code class=\"language-shell\">localhost:.ssh root$ ls -l authorized_keys \n-rw-r--r--  1 root  staff  393 Dec  6 16:33 authorized_keys<\/code><\/pre>\n<p>\u6216\u8005\u76f4\u63a5\u4f7f\u7528\u4e00\u6761\u547d\u4ee4\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u516c\u94a5\u7684\u590d\u5236\uff0cssh-copy-id\u540e\u9762\u63a5\u5165\u7684\u7528\u6237\u5c31\u662f\u8981\u652f\u6301\u514d\u5bc6\u767b\u5f55\u7684\u7528\u6237\u3002<\/p>\n<pre><code class=\"language-shell\">root@ubuntu:~\/.ssh$ ssh-copy-id &quot;root@192.168.1.88&quot;\n\/usr\/bin\/ssh-copy-id: INFO: Source of key(s) to be installed: &quot;\/home\/root\/.ssh\/id_rsa.pub&quot;\nThe authenticity of host &#039;192.168.1.88 (192.168.1.88)&#039; can&#039;t be established.\nECDSA key fingerprint is SHA256:\/ufx+\/OLtdsYy7vsdk4KDu9xJsBp6zHonRAf2jjT0GI.\nAre you sure you want to continue connecting (yes\/no)? n^H\nPlease type &#039;yes&#039; or &#039;no&#039;: yes\n\/usr\/bin\/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys\nPassword:\n\nNumber of key(s) added: 1\n\nNow try logging into the machine, with:   &quot;ssh &#039;root@192.168.1.88&#039;&quot;   and check to make sure that only the key(s) you wanted were added.\n\n#\u53bb\u76ee\u6807\u673a\u5668\u4e0b\uff0c\u68c0\u67e5authorized_keys\u6587\u4ef6\nlocalhost:.ssh root$ cat authorized_keys \nssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDB\/mkvSx1mo\/l\/huMiOSqQjTb3KSB+D67qDRe1VgbjeZpwtodpSNueqGMj3mGDtf8tfmfwygKHuMtkVCQuD+1trLZ\/yn8dKK9JV2pM4iwmaEvSda\/qbQR7lQ37lsmvGnwCloJ49h8MpsF9UsDVXnyo4kauOj+0HYlur1E5Y7dzBuuzBiKAnwS66ZVTtHpIzubfXYanyBEinClpHNr6B7DVWp0J4ubZ8k\/AgFMyD8NyJmugoObD3+wry0Dk2LG\/WadOaY1luooQN4m55WyfZE2w0Kzi4F7W6v8\/GRnpxVKOmNUNk6\/c6hr\/CDWnthS1abMjI9u\/bGru6X2kiyymq3wR root@ubuntu<\/code><\/pre>\n<h3>\u767b\u5f55\u6d4b\u8bd5<\/h3>\n<pre><code class=\"language-shell\">root@ubuntu:~\/.ssh$ ssh &#039;root@192.168.1.88&#039;\nLast login: Tue Dec  6 16:28:07 2016 from 192.168.1.69\nlocalhost:~ root$ <\/code><\/pre>\n<p>\u767b\u5f55\u65b9\u5f0f\u6ce8\u610f\u533a\u522b<\/p>\n<pre><code class=\"language-shell\">$ ssh root@192.168.1.88    #\u7528\u6237\u540d\u5bc6\u7801\u767b\u5f55\n$ ssh &#039;root@192.168.1.88&#039;  #\u516c\u94a5\u514d\u5bc6\u767b\u5f55<\/code><\/pre>\n<h2>\u4f7f\u7528pythonssh\u514d\u5bc6\u5b9e\u73b0<\/h2>\n<h3>\u514d\u5bc6ssh<\/h3>\n<pre><code class=\"language-python\">import paramiko\n\nprivate_key = paramiko.RSAKey.from_private_key_file(&#039;id_rsa96&#039;)  #\u4f7f\u7528\u76ee\u6807\u7684\u79c1\u94a5\u6765\u767b\u5f55\n\nssh = paramiko.SSHClient()\nssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n\nssh.connect(hostname=&#039;192.168.1.96&#039;,port=22,username=&#039;root&#039;,pkey=private_key)\n\ncmd = &#039;ps&#039;\nstdin, stdout, stderr = ssh.exec_command(cmd)\n\nresult = stdout.read()\n\nif not result:\n    result = stderr.read()\nssh.close()\n\nprint(result.decode())<\/code><\/pre>\n<h3>\u514d\u5bc6sftp<\/h3>\n<pre><code class=\"language-python\">import paramiko\n\nprivate_key = paramiko.RSAKey.from_private_key_file(&#039;\/home\/auto\/.ssh\/id_rsa&#039;)\n\ntransport = paramiko.Transport((&#039;hostname&#039;, 22))\ntransport.connect(username=&#039;wupeiqi&#039;, pkey=private_key )\n\nsftp = paramiko.SFTPClient.from_transport(transport)\n\nsftp.put(&#039;\/tmp\/location.py&#039;, &#039;\/tmp\/test.py&#039;)    \n# \u5c06location.py \u4e0a\u4f20\u81f3\u670d\u52a1\u5668 \/tmp\/test.py\nsftp.get(&#039;remove_path&#039;, &#039;local_path&#039;)        \n# \u5c06remove_path \u4e0b\u8f7d\u5230\u672c\u5730 local_path\n\ntransport.close()<\/code><\/pre>\n<h2>\u6848\u4f8b<\/h2>\n<pre><code class=\"language-python\">import paramiko\nimport uuid\n\nclass Haproxy(object):\n\n    def __init__(self):\n        self.host = &#039;192.168.1.88&#039;\n        self.port = 22\n        self.username = &#039;root&#039;\n        self.pwd = &#039;123456&#039;\n        self.__k = None\n\n    def create_file(self):\n        file_name = str(uuid.uuid4())\n        with open(file_name,&#039;w&#039;) as f:\n            f.write(&#039;sb&#039;)\n        return file_name\n\n    def run(self):\n        self.connect()\n        self.upload()\n        self.rename()\n        self.close()\n\n    def connect(self):\n        transport = paramiko.Transport((self.host,self.port))\n        transport.connect(username=self.username,password=self.pwd)\n        self.__transport = transport\n\n    def close(self):\n\n        self.__transport.close()\n\n    def upload(self):\n        # \u8fde\u63a5\uff0c\u4e0a\u4f20\n        file_name = self.create_file()\n\n        sftp = paramiko.SFTPClient.from_transport(self.__transport)\n        sftp.put(file_name, &#039;\/home\/test.py&#039;)  # \u5c06location.py \u4e0a\u4f20\u81f3\u670d\u52a1\u5668 \/tmp\/test.pydef rename(self):\n\n        ssh = paramiko.SSHClient()\n        ssh._transport = self.__transport  # \u6267\u884c\u547d\u4ee4\n        stdin, stdout, stderr = ssh.exec_command(&#039;mv \/home\/test.py \/home\/test2.py&#039;) \n        result = stdout.read()\n\nha = Haproxy()\nha.run()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python\u5b9e\u73b0SSH Linux\u4e0b\u7684ssh\u767b\u5f55 root@ubuntu:~# ssh root@192.16 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-202","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}