#coding: utf-8
importparamikoimportreimportosfrom time importsleep#定义一个类,表示一台远端linux主机#参考https://www.cnblogs.com/haigege/p/5517422.html wyc
classLinux(object):#通过IP, 用户名,密码,超时时间初始化一个远程Linux主机
def __init__(self, ip, username, password, timeout=3000):
self.ip=ip
self.username=username
self.password=password
self.timeout=timeout#transport和chanel
self.t = ''self.chan= ''
#链接失败的重试次数
self.try_times = 3
#调用该方法连接远程主机
defconnect(self):whileTrue:#连接过程中可能会抛出异常,比如网络不通、链接超时
try:
self.t= paramiko.Transport(sock=(self.ip, 22))
self.t.connect(username=self.username, password=self.password)
self.chan=self.t.open_session()
self.chan.settimeout(self.timeout)
self.chan.get_pty()
self.chan.invoke_shell()#如果没有抛出异常说明连接成功,直接返回
print (u'连接%s成功' %self.ip)#接收到的网络数据解码为str
print (self.chan.recv(65535).decode('utf-8'))return
#这里不对可能的异常如socket.error, socket.timeout细化,直接一网打尽
exceptException as e1:if self.try_times !=0:print (u'连接%s失败,进行重试' %self.ip)
self.try_times-= 1
else:print (u'重试3次失败,结束程序')
exit(1)#断开连接
defclose(self):
self.chan.close()
self.t.close()#发送要执行的命令
defsend(self, cmd):
cmd+= '\r'
#通过命令执行提示符来判断命令是否执行完成
p = re.compile(r':~ #')
result= ''
#发送要执行的命令
self.chan.send(cmd)#回显很长的命令可能执行较久,通过循环分批次取回回显
whileTrue:
sleep(0.5)
ret= self.chan.recv(65535)
ret= ret.decode('utf-8')
result+=retifp.search(ret):print(result)returnresult#------获取本地指定目录及其子目录下的所有文件------
def __get_all_files_in_local_dir(self, local_dir):#保存所有文件的列表
all_files =list()#获取当前指定目录下的所有目录及文件,包含属性值
files =os.listdir(local_dir)for x infiles:#local_dir目录中每一个文件或目录的完整路径
filename =os.path.join(local_dir, x)#如果是目录,则递归处理该目录
ifos.path.isdir(x):
all_files.extend(self.__get_all_files_in_local_dir(filename))else:
all_files.append(filename)returnall_filesdefsftp_put_dir(self, local_dir, remote_dir):
t= paramiko.Transport(sock=(self.ip, 22))
t.connect(username=self.username, password=self.password)
sftp=paramiko.SFTPClient.from_transport(t)#去掉路径字符穿最后的字符'/',如果有的话
if remote_dir[-1] == '/':
remote_dir= remote_dir[0:-1]#获取本地指定目录及其子目录下的所有文件
all_files = self.__get_all_files_in_local_dir(local_dir)#依次put每一个文件
for x inall_files:
filename= os.path.split(x)[-1]
remote_filename= remote_dir + '/' +filenameprint(x)print(remote_filename)print (u'Put文件%s传输到%s中...' %(filename,self.ip))
sftp.put(x, remote_filename)if __name__ == '__main__':#host = Linux('172.16.10.40', 'root', 'imlytek!40')
#host.connect()
#host.send('ls -l')
remote_path = r'/imlytek/smpt'local_path= r'D:\TSTP'
#host.sftp_put_dir(local_path, remote_path)
hostArray=[['172.16.10.40','root','iflstek!40'],['172.16.10.51','root','x1f@2013']]for x inhostArray:
host= Linux(x[0], x[1], x[2])
host.sftp_put_dir(local_path, remote_path)#host.close()