方法一:
python使用paramiko实现远程拷贝文件的方法
首先是安装paramiko库(其实现了SSH2安全协议),ubuntu下可直接通过源安装:
pip instal paramiko
版本1
1 # -*- coding:utf-8 -*- 2 import paramiko,os,sys,time 3 4 port = 22 5 user = 'root' 6 def ssh_scp_put(ip,port,user,password,local_file,remote_file): 7 ssh = paramiko.SSHClient() 8 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 9 ssh.connect(ip, 22, 'root', password) 10 a = ssh.exec_command('date') 11 stdin, stdout, stderr = a 12 print stdout.read() 13 sftp = paramiko.SFTPClient.from_transport(ssh.get_transport()) 14 sftp = ssh.open_sftp() 15 sftp.put(local_file, remote_file) 16 17 def ssh_scp_get(ip, port, user, password, remote_file, local_file): 18 ssh = paramiko.SSHClient() 19 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 20 ssh.connect(ip, 22, 'root', password) 21 a = ssh.exec_command('date') 22 stdin, stdout, stderr = a 23 print stdout.read() 24 sftp = paramiko.SFTPClient.from_transport(ssh.get_transport()) 25 sftp = ssh.open_sftp() 26 sftp.get(remote_file, local_file) 27 28 29 30 ip = raw_input("请输入远端主机的IP地址:") 31 password = raw_input("请输入远端主机的密码:") 32 33 while True: 34 print ''' 35 -------欢迎使用 scp software-------- 36 上传文件请输入 [ 1 ]: 37 下载文件请输入 [ 2 ]: 38 退出SCP请输入 [ q ]: 39 ------------------------------------ 40 ''' 41 choice = raw_input("请输入 [ ]") 42 if choice == "1": 43 local_file = raw_input("请输入本地文件的绝对路径:") 44 remote_file = raw_input("请输入文件上传的绝对路径:") 45 ssh_scp_put(ip,port,user,password,local_file,remote_file) 46 elif choice == "2": 47 remote_file = raw_input("请输入远端文件的绝对路径:") 48 local_file = raw_input("请输入要放到本地的绝对路径:") 49 ssh_scp_get(ip,port,user,password,remote_file,local_file) 50 elif choice == "q": 51 print "感谢使用,再见" 52 exit() 53 else: 54 print "输入错误,请重新输入:"版本2
1 import paramiko 2 import os 3 import sys 4 import getpass 5 print("****开始配置目标机器信息*****") 6 #ips = input("主机IP:") 7 #user = input("主机账号:") 8 #password = getpass.getpass("主机密码:") 9 #port = 22 10 user = "root" 11 ips = "192.168.1.100" 12 password = "123456" 13 port = 22 14 class Tools(object): 15 def __init__(self, user, password, port, ips): 16 self.user = user 17 self.password = password 18 self.port = port 19 self.ip = ips 20 def connect(self): 21 try: 22 self.ssh = paramiko.SSHClient() 23 self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 24 self.ssh.connect(self.ip, self.port, self.user, self.password) 25 print("连接已建立") 26 except Exception as e: 27 print("未能连接到主机") 28 def cmd(self): 29 cmd = input("请输入要执行的命令:>>") 30 stdout, stdin, stderr = self.ssh.exec_command(cmd) 31 #print(sys.stdout.read()) 32 def input(self): 33 self.local_file_abs = input("本地文件的绝对路径:>>") 34 self.remote_file_abs = input("远程文件的绝对路径:>>") 35 def put(self): 36 sftp = paramiko.SFTPClient.from_transport(self.ssh.get_transport()) 37 sftp = self.ssh.open_sftp() 38 self.input() 39 sftp.put(self.local_file_abs,self.remote_file_abs) 40 def get(self): 41 sftp = paramiko.SFTPClient.from_transport(self.ssh.get_transport()) 42 sftp = self.ssh.open_sftp() 43 self.input() 44 sftp.get(self.remote_file_abs,self.local_file_abs) 45 def close(self): 46 self.ssh.close() 47 print("连接关闭") 48 obj = Tools(user, password, port, ips) 49 if __name__ == "__main__": 50 msg = ''' 51 执行命令 >>输入cmd 52 上传文件 >>输入put 53 下载文件 >>输入get 54 退出 >>输入 55 ''' 56 getattr(obj, "connect")() 57 while True: 58 print(msg) 59 inp = input("action:>>") 60 if hasattr(obj,inp): 61 getattr(obj,inp)() 62 if inp == "q": 63 getattr(obj,"close")() 64 exit()
else:print("没有该选项,请重新输入:>>")
方法二:
python使用pexpect实现远程拷贝文件的方法使用
import os import sys import pexpect
remoteFile = "test.jpg" localPath = "/home/" remote_password = "123456" try: # 要执行的命令或者脚本 child = pexpect.spawn("scp root@192.168.1.100:/home/"+remoteFile+" "+localPath) # 将日志指向标准输出 child.logfile = sys.stdout # 执行命令后需要匹配的结果 child.expect("root@192.168.1.100's password:") # 匹配成功后,发送命令 child.sendline(remote_password) # 执行上条命令,需要等待很久才会得到结果的,需要使用while否则报pexpect.TIMEOUT异常 while 1: index = child.expect(['.* result file generated.*',pexpect.EOF, pexpect.TIMEOUT]) if index == 0: # 匹配到项后,跳出循环 break elif index == 1: # continue to wait pass elif index == 2: # continue to wait pass child.sendline('exit') child.sendcontrol('c') child.interact() except OSError: sys.exit(0)