The SFTP object of paramiko implements the scp function of Linux, paramikosftp

Source: Internet
Author: User

The SFTP object of paramiko implements the scp function of Linux, paramikosftp

1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 4 import paramiko 5 import OS 6 import re 7 8 9 class SSHConnection (object ): 10 def _ init _ (self, host, port, username, password): 11 self. host = host12 self. port = port13 self. username = username14 self. password = password15 self. _ k = None16 17 def run (self): 18 self. connect () 19 self. sftp_put_dir ("/opt/app/appops/sbin/collectd/var", "/opt/app/") 20 # self. cmd ('ls/opt/app') 21 self. close () 22 23 def connect (self): 24 transport = paramiko. transport (self. host, self. port) 25 transport. connect (username = self. username, password = self. password) 26 self. _ transport = transport27 28 def close (self): 29 self. _ transport. close () 30 31 def upload (self, loacal_path, target_path): 32 sftp = paramiko. SFTPClient. from_transport (self. _ transport) 33 sftp. put (loacal_path, target_path) 34 35 def cmd (self, command): 36 print "commadn in cmd:", command37 ssh = paramiko. SSHClient () 38 39 ssh. _ transport = self. _ transport40 stdin, stdout, stderr = ssh.exe c_command (command) 41 42 result = stdout. read () 43 print "result: \ n", result44 return result45 46 def getList (self, path): 47 files_list = [] 48 files = sorted (filter (lambda x: re. match (R' ^ \ W', x), OS. listdir (path) 49 for tmp_file in files: 50 path_file = OS. path. join (path, tmp_file) 51 if OS. path. isdir (path_file) and not OS. path. islink (path_file): 52 files_list.append (path_file) 53 files_list + = self. getList (path_file) 54 else: 55 files_list.append (path_file) 56 return files_list57 58 def sftp_put_dir (self, local_dir, remote_dir): 59 sftp = paramiko. SFTPClient. from_transport (self. _ transport) 60 if remote_dir [-1] = '/': 61 remote_dir = remote_dir [0:-1] 62 all_files = self. getList (local_dir) 63 for x in all_files: 64 f_path = x. replace (local_dir ,""). lstrip ('/') 65 if OS. path. isdir (x): 66 r_path = OS. path. join (remote_dir, f_path) 67 try: 68 sftp. mkdir (r_path) 69 failed t Exception, e: 70 pass71 else: 72 remote_filename = OS. path. join (remote_dir, f_path) 73 print u'put file % s transmission... '% f_path74 sftp. put (x, remote_filename) 75 76 77 def main (host, port, username, password): 78 print "in main:" 79 obj = SSHConnection (host, port, username, password) 80 obj. run () 81 82 83 if _ name _ = '_ main __': 84 host = ''85 port ='' 86 username = ''87 password ='' 88 main (host, port, username, password)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.