1. 程式人生 > 其它 >paramiko模組上傳檔案失敗:paramiko.ssh_exception.SSHException: Channel closed.

paramiko模組上傳檔案失敗:paramiko.ssh_exception.SSHException: Channel closed.

問題:

paramiko模組上傳檔案失敗,提示paramiko.ssh_exception.SSHException: Channel closed. 使用python3 paramiko庫實現向遠端伺服器上傳檔案,指令碼如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import paramiko
import uuid
import os

class Haproxy(object):

    def __init__(self):
        self.host = '10.224.96.87'
        self.port = 22
        self.username = 'root'
        self.pwd = 'tahoe.webex'
        self.__k = None

    def create_file(self):
        file_name = str(uuid.uuid4())
        with open(file_name,'w') as f:
            f.write('sb')
        return file_name

    def run(self):
        self.connect()
        self.upload()
       # self.rename()
        #self.close()

    def connect(self):
        transport = paramiko.Transport((self.host,self.port))
        transport.connect(username=self.username,password=self.pwd)
        self.__transport = transport
        print("Connected to %s as %s" % (self.host, self.username))

    def close(self):

        self.__transport.close()

    def upload(self):
        # 連線,上傳
        #file_name = self.create_file()
        localpath= r'C:\PF\cpu_memory.py'
        remotepath = r'/home/test.py'
        sftp = paramiko.SFTPClient.from_transport(self.__transport)
        # 將location.py 上傳至伺服器 /tmp/test.py
        sftp.put(localpath, remotepath)
        print("File %s has uploaded to %s" % (localpath, remotepath))
    def rename(self):

        ssh = paramiko.SSHClient()
        ssh._transport = self.__transport
        # 執行命令
        stdin, stdout, stderr = ssh.exec_command('mv /home/root/tttttttttttt.py /home/root/ooooooooo.py')
        # 獲取命令結果
        result = stdout.read()


ha = Haproxy()
ha.run()

  

但執行時報錯如下:
但執行時報錯如下:
C:\Users\yunliu3\AppData\Local\Programs\Python\Python39\python.exe C:/PF/test_bak.py
Connected to 10.224.96.87 as root
Traceback (most recent call last):
  File "C:\PF\test_bak.py", line 77, in <module>
    ha.run()
  File "C:\PF\test_bak.py", line 43, in run
    self.upload()
  File "C:\PF\test_bak.py", line 62, in upload
    sftp = paramiko.SFTPClient.from_transport(self.__transport)
  File "C:\Users\yunliu3\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\sftp_client.py", line 169, in from_transport
    chan.invoke_subsystem("sftp")
  File "C:\Users\yunliu3\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\channel.py", line 72, in _check
    return func(self, *args, **kwds)
  File "C:\Users\yunliu3\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\channel.py", line 283, in invoke_subsystem
    self._wait_for_event()
  File "C:\Users\yunliu3\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\channel.py", line 1226, in _wait_for_event
    raise e
paramiko.ssh_exception.SSHException: Channel closed.
 
Process finished with exit code 1

 

原因:因為伺服器sftp的服務未開啟 解決: 1.開啟/etc/ssh/sshd_config 2.加入下面的程式碼(首先查詢一下sftp-server的路徑 [root@frdsa002 ~]# find / -name sftp-server /usr/libexec/openssh/sftp-server) Subsystem sftp /usr/libexec/openssh/sftp-server