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 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 |
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
Similar Posts:
- Paramiko Error: Garbage packet received [How to Solve]
- Node uses SSH2 SFTP Client to upload and download FTP files
- python3 are installed through pip on Win 10, Unicode decodeerror: ‘UTF-8’ codec can’t decode byte
- [How to Solve] This service allows sftp connections only
- Tensorflow ImportError: DLL load failed with error code -1073741795
- [Solved] Django use search_fields error (in get_lookup_constraint)
- Oserror: cannot open resource (pilot error handling)
- How to Solve Python Error: “HTTP Error 403: Forbidden”
- Description of SFTP function error in jumpserver
- Python: How to Solve raise JSONDecodeError(“Expecting value”, s, err.value) from None json.decoder…