Tag Archives: paramiko.ssh_exception.SSHException: Channel closed

[Solved] The paramiko module failed to upload the file: paramiko.ssh_exception.SSHException: Channel closed.

problem:

The paramiko module failed to upload the file, and it prompts paramiko.ssh_exception.SSHException: Channel closed.
Use the python3 paramiko library to upload files to the remote server, the script is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
twenty two
twenty three
twenty four
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/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):
        # connect and upload
        #file_name = self.create_file()
        localpath= r'C:\PF\cpu_memory.py'
        remotepath = r'/home/test.py'
        sftp = paramiko.SFTPClient.from_transport(self.__transport)
        # Upload the location.py to the server /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
        # Execution of commands
        stdin, stdout, stderr = ssh.exec_command('mv /home/root/tttttttttttt.py /home/root/ooooooooo.py')
        # Get command results
        result = stdout.read()
ha = Haproxy()
ha.run()

 

But the error is reported as follows during runtime:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
However, it runs with the following error:
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 77in <module>
    ha.run()
  File "C:\PF\test_bak.py", line 43in run
    self.upload()
  File "C:\PF\test_bak.py", line 62in 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 169in from_transport
    chan.invoke_subsystem("sftp")
  File "C:\Users\yunliu3\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\channel.py", line 72in _check
    return func(self*args, **kwds)
  File "C:\Users\yunliu3\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\channel.py", line 283in invoke_subsystem
    self._wait_for_event()
  File "C:\Users\yunliu3\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\channel.py", line 1226in _wait_for_event
    raise e
paramiko.ssh_exception.SSHException: Channel closed.
 
Process finished with exit code 1

 

Reason: because the server sftp service is not turned on
solve:
1. Open /etc/ssh/sshd_config
2. Add the following code (first check the path of sftp-server
[root@frdsa002 ~]# find / -name sftp-server
/usr/libexec/openssh/sftp-server)
Subsystem sftp /usr/libexec/openssh/sftp-server