Instructions for using python to run nmon
Instructions for using python to run nmon
Step one:
Installation: paramiko
Window installation method: pip install paramiko
Reference: http://www.jb51.net/article/97655.htm
Step 2:
The following code contains sending commands and downloading all files under a single file and directory file
The disadvantage is that it does not write out the multi-process mode of execution.
# coding=utf-8
Import paramiko,time,threading
Import os, sys,shutil
From stat import *
Def get_year_mon_day_hour_min_sec ():
Time_array = time.localtime ()
Result= "% 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)
Return result
Def get_year_mon_day ():
Time_array = time.localtime ()
Result= u "% s year% s% s day"% (time_array.tm_year,time_array.tm_mon,time_array.tm_mday)
Return result
Def get_hour_min_sec ():
Time_array = time.localtime ()
Result= u "% s:% s"% (time_array.tm_hour,time_array.tm_min,time_array.tm_sec)
Return result
Def get_y_m_d_h_ms ():
Import datetime
Result = datetime.datetime.now () .strftime ("% Y% m% d% H% M% S _")
Return result
Define a class that represents a remote linux host
Class Linux (object):
Through IP, user name, password Timeout initializes a remote Linux host def _ _ init__ (self, ip, username, password, port=22) Timeout=30): self.ip = ip self.username = username self.password = password self.port = port self.timeout = timeout # transport and chanel self.t =''self.chan =''# the number of failed retries self.try_times = calling this method to connect to the remote host def connect (self): transport = paramiko.Transport ((self.ip, self.port)) transport.connect (username=self.username Password=self.password) self.__transport = transport# disconnect def close (self): self.__transport.close () # send the command to execute def send (self, command): self.connect () ssh = paramiko.SSHClient () ssh._transport = self.__transport # execute the command stdin, stdout Stderr = ssh.exec_command (command) # get command result result = stdout.read () print result self.close () # get single file def sftp_get (self, remotefile, localfile): t = paramiko.Transport (sock= (self.ip, self.port)) t.connect (username=self.username, password=self.password) sftp = paramiko.SFTPClient.from_transport (t) sftp.get (remotefile Localfile) print 'download complete' t.close () # put single file def sftp_put (self, localfile, remotefile): t = paramiko.Transport (sock= (self.ip, self.port)) t.connect (username=self.username, password=self.password) sftp = paramiko.SFTPClient.from_transport (t) sftp.put (localfile) Remotefile) print "uploaded successfully" t.close () #-get all files in the specified directory and its subdirectories on the remote linux host-def _ _ get_all_files_in_remote_dir (self, sftp, remote_dir): # Save all files list all_files = list () # remove the last character'/'of the path string If there is, if remote_dir [- 1] ='/': remote_dir = remote_ dirt [0:-1] # get all directories and files under the currently specified directory Contains the attribute value files = sftp.listdir_attr (remote_dir) for x in files: # the full path of each file or directory in the remote_dir directory filename = remote_dir +'/'+ x.filename # if it is a directory, the directory is processed recursively. Here the S_ISDIR method in the stat library is used Exactly the same as the name of the macro in linux if S_ISDIR (x.st_mode): all_files.extend (self.__get_all_files_in_remote_dir (sftp) Filename) else: all_files.append (filename) return all_files#-get all files in the local specified directory and its subdirectories-def _ _ get_all_files_in_local_dir (self, local_dir): # Save all files list all_files = list () # get all directories and files under the currently specified directory Contains the attribute value files = os.listdir (local_dir) for x in files: # the full path of each file or directory in the local_dir directory filename = os.path.join (local_dir, x) # if it is a directory Then recursively process the directory if os.path.isdir (x): all_files.extend (self.__get_all_files_in_local_dir (filename)) else: all_files.append (filename) return all_files# to get all the files in the local specified directory and its subdirectories def sftp_put_dir (self, local_dir, remote_dir): t = paramiko.Transport (sock= (self.ip) Self.port)) t.connect (username=self.username, password=self.password) sftp = paramiko.SFTPClient.from_transport (t) # remove the path character and wear the last character'/' If there is, if remote_dir [- 1] = ='/': remote_dir = remote_ dirt [0:-1] # get all the files in the local specified directory and its subdirectories all_files = self.__get_all_files_in_local_dir (local_dir) # put each file for x in all_files: filename = os.path.split (x ) [- 1] remote_filename = remote_dir +'/'+ filename print u'Put file% s transferring...'% filename sftp.put (x Remote_filename) # get all the files in the specified directory and its subdirectories on the remote linux host def sftp_get_dir (self, remote_dir, local_dir): t = paramiko.Transport (sock= (self.ip, self.port)) t.connect (username=self.username Password=self.password) sftp = paramiko.SFTPClient.from_transport (t) # get all files in the specified directory and its subdirectories on the remote linux host all_files = self.__get_all_files_in_remote_dir (sftp, remote_dir) # get each file for x in all_files: filename = x.split ('/') [- 1] local_filename = os.path.join (local_dir Filename) print u'Get file% s transfer...'% filename sftp.get (x, local_filename) # File separation def copy_file (self, base_file_path): file_list = os.listdir (base_file_path) s = set () for i in file_list: # take the year, month and day in the file name Set data = i.split ('_') [: 3] d =''for j in data: d = d + j + "_" s.add (d) base_path = os.path.dirname (base_file_path) # take the parent directory of the basic file for i in s: path = os.path.join (base_path I) # spell path (parent directory of the base file + file name named after year, month and day) if not os.path.exists (path): os.mkdir (ringing% s\% s'% (base_path, I)) # create a new file Name for j in file_list: if i in j: new_path = os.path.join (path, j) file_path = os.path.join (base_file_path, j) shutil.copyfile (file_path, new_path) # copy file print ('copy complete!')
If name=='main':
Try: with open ('ip.list', 'r') as file: for line in file.readlines (): ip = str (line.split (':') [0]) host_address = ip.replace ('.' '_') username = str (line.split (':') [1]) password = str (line.split (':') [2]) cmds = (line.split (':') [3get_year_mon_day ()) print "+ ip+" # "+ get_year_mon_day () +" + get_hour_min_sec () + "#" host = Linux (ip Username, password) host.send ("cd / home/nmon" . / nmon-f-t-r-test_3_19-s 5-c 10-F "+ get_y_m_d_h_m_s () +" + host_address+ ".nmon-m. / nmon_results;cd. / nmon_results;ls-l") # 735 # Delete # host.send ("cd sysinfo_nmon;rm-rf *; ls-l") # cd sysinfo_nmon;rm-rf * Ls-l # host.sftp_get_dir (remote_path, local_path) # send the remote xxoo.txt get locally and save it as ooxx.txt # host.sftp_get (remote_path, local_path) # host.sftp_get_dir (remote_path, local_path) except: print u "Please check whether the data has been downloaded!" End_time = time.time () # send the local xxoo.txt put to the remote and keep it as xxoo.txt# host.sftp_put (localfile, remotefile) # get all files in the remote remote_path directory to the local local_path directory # host.sftp_get_dir (remote_path, local_path) # put all files in the local local_path directory to the remote remote_path directory # host.sftp_put_dir (remote_path, local_path)
Ip.list file format:
Running result:
Supplementary explanation
The user name needs to have executable permission, otherwise it fails.