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