Python Learning-Implementing secure Copy Functionality

Source: Internet
Author: User
Tags file copy scp command secure copy

The SCP command for Linux enables the file copy function between two hosts.

Implement SCP functionality with Python.

defRUN_SCP (from1, to, passwd, log_file): cmd="SCP%s%s"%(From1, to) p=pexpect.spawn (cmd)ifLog_file isNone:log_file= sys.__stdout__P.logfile=open_log_file (log_file) basename=os.path.basename (from1) Index= P.expect (["[Pp]assword:","(yes/no)", Basename,pexpect. EOF])ifindex = =0:p.sendline (passwd)elifindex = = 1: P.sendline ("Yes") P.expect ("[Pp]assword:") P.sendline (passwd)elifindex = = 2:        Pass    Else:        RaiseException ("Fail to SCP")defScp_to_server (Local_file, Server_ip, Server_file, user='Root', passwd='XXX', log_file=None):if  notos.path.exists (local_file):RaiseException ("File%s is not exist."%local_file)if  notRUN_SCP (Local_file,"%[email protected]%s:%s"%(User,server_ip,server_file), passwd,log_file):RaiseException ("wrong password to server!")defScp_from_server (server_ip, Server_file, Local_file, user='Root', passwd='XXX', log_file=None):if  notRUN_SCP ("%[email protected]%s:%s"%(User,server,path), local_file,passed,log_file):RaiseException ("wrong password to server!")

Python Learning-Implementing secure Copy Functionality

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.