Today received a small demand, in the windows environment just want to upload the compressed file to the specified directory linux position and extract them, then I thought for a moment, that you can use to try to write python.
Environment:
1.linux operating system a
2.windows environment installation Python3.x
3.Pycharm IDE environment
if we are not familiar to paramiko module, you can get to know ha.
Upload file used SFTPCLient, SFTPCLient as a sftp client object, according to the ssh sftp transport protocol session, remote file operations, such as upload, download, privilege, status.
code show as below
. 1 # ! / Usr / bin / Python the env 2 Import paramiko . 3 Import SYS . 4 . 5 in SRC_FILE of the sys.argv = [. 1 ] in the source file #windows . 6 dsc_path = the sys.argv [2 ] directory path on #linux . 7 key_path = 'private path ' . 8 password = 'password ' . 9 SSH = paramiko.SSHClient () Create SSH objects # 10 the private_key = paramiko.RSAKey.from_private_key_file (key_path) # specify the private key . 11 ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) # know_hosts not allowed to connect the host file 12 is Transport paramiko.Transport = ((' IP ' , port number )) 13 is transport.connect (username = ' the root ' , = PKey the private_key) connected to the server # 14 15 DEF Upload (): 16 SFTP = paramiko.SFTPClient.from_transport (Transport) . 17 the try : 18 is SFTP. PUT (in SRC_FILE of, dsc_path + ' / ' + in SRC_FILE of) . 19 Print ( ' successful upload ' ) 20 is the except Exception AS E: 21 is Print (E) 22 is ssh.close () 23 24 25 def unzip(): 26 ssh._transport = transport 27 stdin, stdout, stderr = ssh.exec_command('cd ' + dsc_path + '&&' + 'unzip -o ' + src_file) 28 result = stdout.read().decode() 29 if result.strip() != '': 30 print('解压成功') 31 else: 32 print('Error message ' + stderr.read().decode()) 33 ssh.close() 34 35 36 if __name__ == '__main__': 37 upload() 38 unzip()
Test scripts can be run separately or by Pycharm cmd, the following is pycharm test, upload apps.zip file to the / tmp directory Linux server and unzip
1 Python upload_file.py apps.zip / tmp 2 successfully uploaded 3 decompression success
Then log into the / tmp directory Linux server to see if there is a file compression and decompression
Summary : This is just a simple upload decompression, please exhibitions.