python运行nmon使用说明
第一步:
安装:paramiko
窗口安装方法:pip安装paramiko
参考:http://www.jb51.net/article/97655.htm
第二步:
以下代码包含发送命令与下载单个文件与目录文件下的所有;
缺点是没有写出多进程方式执行;
# utf - 8编码=
进口paramiko、时间、线程
导入操作系统,系统,shutil从统计进口*
def get_year_mon_day_hour_min_sec ():
time_array=time.localtime ()
结果=? s: % s: % s: % s: % s: % s"% (time_array.tm_year、time_array.tm_mon time_array.tm_mday、time_array.tm_hour time_array.tm_min, time_array.tm_sec)
返回结果
def get_year_mon_day ():
time_array=time.localtime ()
结果=u" % s年% s月% s日“% (time_array.tm_year time_array.tm_mon time_array.tm_mday)
返回结果
def get_hour_min_sec ():
time_array=time.localtime ()
结果=u" % s: % s: % s“;% (time_array.tm_hour time_array.tm_min time_array.tm_sec)
返回结果
def get_y_m_d_h_m s ():
进口datetime
结果=datetime.datetime.now () .strftime (“% Y % m % d % H % m % S_")
返回结果
定义一个类,表示一台远端linux主机
类Linux(对象):
通过IP,用户名,密码,超时时间初始化一个远程Linux主机
<代码> def __init__(自我、ip、用户名、密码、港口=22日超时=30): 自我。ip=ip 自我。用户名=用户名 自我。密码=密码 自我。端口=端口 自我。timeout=超时 #运输和香奈儿 自我。t=" 自我。陈=" #链接失败的重试次数 自我。try_times=3 #调用该方法连接远程主机 def连接(自我): 运输=paramiko.Transport((自我。ip, self.port)) transport.connect(用户名=自我。用户名、密码=self.password) 自我。__transport=运输 #断开连接 def关闭(自我): self.__transport.close () #发送要执行的命令 def发送(自我,命令): self.connect () ssh=paramiko.SSHClient () ssh。_transport=self.__transport #执行命令 stdin、stdout stderr=ssh.exec_command(命令) #获取命令结果 结果=stdout.read () 打印结果 self.close () #获取单个文件 def sftp_get(自我、remotefile localfile): t=paramiko.Transport(袜子=(自我。ip, self.port)) t.connect(用户名=自我。用户名、密码=self.password) sftp=paramiko.SFTPClient.from_transport (t) sftp。get (remotefile localfile) 打印“下载完成” t.close () #把单个文件 def sftp_put(自我、localfile remotefile): t=paramiko.Transport(袜子=(自我。ip, self.port)) t.connect(用户名=自我。用户名、密码=self.password) sftp=paramiko.SFTPClient.from_transport (t) sftp。put (localfile remotefile) 打印”上传成功” t.close () # - - - - - -获取远端linux主机上指定目录及其子目录下的所有文件- - - - - - def __get_all_files_in_remote_dir(自我、sftp remote_dir): #保存所有文件的列的表 all_files=列表() #去掉路径字符串最后的字符“/?如果有的话 如果remote_dir [1]==?? remote_dir=remote_dir [0, 1] #获取当前指定目录下的所有目录及文件,包含属性值 文件=sftp.listdir_attr (remote_dir) x的文件: # remote_dir目录中每一个文件或目录的完整路径 文件名=remote_dir + + x.filename“/? #如果是目录,则递归处理该目录,这里用到了统计库中的S_ISDIR方法,与linux中的宏的名字完全一致 如果S_ISDIR (x.st_mode): all_files.extend(自我。__get_all_files_in_remote_dir (sftp、文件名)) 其他: all_files.append(文件名) 返回all_files # - - - - - -获取本地指定目录及其子目录下的所有文件- - - - - - local_dir def __get_all_files_in_local_dir(自我): #保存所有文件的列的表 all_files=列表() #获取当前指定目录下的所有目录及文件,包含属性值 文件=os.listdir (local_dir) x的文件: # local_dir目录中每一个文件或目录的完整路径 文件名=os.path。加入(local_dir x) #如果是目录,则递归处理该目录 如果os.path.isdir (x): all_files.extend (self.__get_all_files_in_local_dir(文件名) 其他: all_files.append(文件名) 返回all_files #获取本地指定目录及其子目录下的所有文件 def sftp_put_dir(自我、local_dir remote_dir): t=paramiko.Transport(袜子=(自我。ip, self.port)) t.connect(用户名=自我。用户名、密码=self.password) sftp=paramiko.SFTPClient.from_transport (t) #去掉路径字符穿最后的字符“/?如果有的话 如果remote_dir [1]==?? remote_dir=remote_dir [0, 1] #获取本地指定目录及其子目录下的所有文件 all_files=self.__get_all_files_in_local_dir (local_dir) #依次把每一个文件 在all_files x: 文件名=os.path.split (x) [1] remote_filename=remote_dir +‘/?文件名 打印u 'Put文件% s传输中……' %文件名 sftp。put (x, remote_filename) #获取远端linux主机上指定目录及其子目录下的所有文件 def sftp_get_dir(自我、remote_dir local_dir): t=paramiko.Transport(袜子=(自我。ip, self.port)) t.connect(用户名=自我。用户名、密码=self.password) sftp=paramiko.SFTPClient.from_transport (t) #获取远端linux主机上指定目录及其子目录下的所有文件 all_files=自我。remote_dir __get_all_files_in_remote_dir (sftp) #依次得到每一个文件 在all_files x: 文件名=x.split (“/? [1] local_filename=os.path。加入(local_dir文件名) 打印u 'Get文件% s传输中……' %文件名 sftp。get (x, local_filename) #文件分离 def copy_file(自我,base_file_path): file_list=os.listdir (base_file_path) s=组() 因为我在file_list: #取文件名中的年月日,存集去重 data=https://www.yisu.com/zixun/i.split (“_”) [3] d=" j的数据: d=d + j +“_” s.add (d) base_path=os.path.dirname (base_file_path) #取基础文件的父目录 我的学生:=os.path路径。加入(base_path i) #拼路径(基础文件的父目录+以年月日命名的文件名) 如果不是os.path.exists(路径): 操作系统。mkdir (r ' % s \ % s % (base_path,我))#新建文件,以年月日命名 在file_list j: 如果我在j: new_path=os.path。加入(路径,j) file_path=os.path。加入(base_file_path j) shutil。拷贝文件(file_path new_path) #复制文件 打印(“复制完成! !”)python运行nmon使用说明