Python Run Nmon usage instructions

Source: Internet
Author: User

Python Run Nmon usage instructions

The first step:
Installation: Paramiko
Window Installation method: Pip Install Paramiko

Reference: http://www.jb51.net/article/97655.htm
Step Two:
The following code contains all of the commands that are sent with the download of a single file and directory file;
The disadvantage is that there is no way to write multi-process 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 month%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:%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

Defines a class that represents a remote Linux host

Class Linux (object):

Initialize a remote Linux host by IP, username, password, time-out
def __init__ (self, IP, username, password, port=22,timeout=30): Self.ip = IP Self.username = Username Self.passwo RD = Password Self.port = Port Self.timeout = Timeout # transport and Chanel self.t = ' Self.chan = ' # link Number of failed retries Self.try_times = 3# Call this method to connect 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 command stdin, stdout, stderr = Ssh.exec_command (command) # get command knot result = Stdout.read () print result self.close () # Get Single file Def sftp_get (self, RemoteFile, localfile): t = para Miko. 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 "Upload successful" t.close () #------Get the specified directory and its subdirectories on the remote Linux host All Files------def __get_all_files_in_remote_dir (self, SFTP, Remote_dir): # Save a list of all files All_files = list () # Remove the end of the path string Character '/', if any, if remote_dir[-1] = = '/': Remote_dir = remote_dir[0:-1] # gets all directories and files under the current specified directory, including attribute values files = sftp . Listdir_attr (Remote_dir) for x in Files: # Remote_dir The full path of each file or directory in the directory filename = remote_dir + '/' + x.fi Lename # If it is a directory, the directory is recursively processed, the S_isdir method in the Stat library is used, and the name of the macro in Linux is exactly the same if S_isdir (X.st_mode): ALL_FILES.E Xtend (Self.__get_all_files_in_remote_dir (SFTP, filename)) else:all_files.append (filename) return all _files#------Gets all the files under the local directory and its subdirectories------def__get_all_files_in_local_dir (self, Local_dir): # Save a list of all files All_files = list () # Gets all directories and files under the currently specified directory, including the attribute values files         = Os.listdir (Local_dir) for x in Files: # Local_dir The full path of each file or directory in the directory filename = os.path.join (local_dir, X) # If it is a directory, recursively process the directory if Os.path.isdir (x): All_files.extend (Self.__get_all_files_in_local_dir (Filenam e) else:all_files.append (filename) return all_files# gets all files under 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 to wear the last character '/', if any if remote_dir[-1] = = '/': Remote_dir = remote_dir[0:-1] # gets all files under the local specified directory and its subdirectories all_files = Self.__get_all_files_in_local_dir (local_dir) # put each file in turn for x in All_files : filename = os.path.split (x) [-1] remote_filename = Remote_dir + '/' + filename print u ' put file%sIn transit ... '% filename sftp.put (x, Remote_filename) # gets all files under the specified directory and its subdirectories on the remote Linux host Def sftp_get_dir (self, remote_dir, Loca L_dir): t = Paramiko. Transport (sock= (Self.ip, Self.port)) T.connect (Username=self.username, password=self.password) sftp = Paramiko. Sftpclient.from_transport (t) # gets all files under the specified directory and its subdirectories on the remote Linux host all_files = Self.__get_all_files_in_remote_dir (SFTP, Remo Te_dir) # Get each file in turn for x in all_files:filename = X.split ('/') [-1] local_filename = Os.path.join (loc Al_dir, filename) print u ' get file%s in transit ... '% filename sftp.get (x, Local_filename) #文件分离def copy_file (self, base  _file_path): File_list = Os.listdir (base_file_path) s = set () for I in File_list: # Take the date in the file name, save the set to go back to the data = I.split ('_') [: 3] D = "for j in data:d = d + j +" _ "S.add (d) Base_path = Os.path.       DirName (Base_file_path) # takes the parent directory of the underlying file for i in S:path = Os.path.join (Base_path, i) # path (parent directory of the underlying file + file name named after month and date) If not os.path.exists (path): Os.mkdir (R '%s\%s '% (Base_path, i)) # New file, named for J in File_list by month Date:  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 done!! ‘)

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.s Plit (': ') [2]) Cmds = (Line.split (': ') [3:-1]) 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 #删除 #host. Send (" CD SYSINFO_NMON;RM-RF *;ls-l ") #cd SYSINFO_NMON;RM-RF *;ls-l #host. Sftp_get_dir (Remote_path, Local_path) # Place the remote Xxoo.txt get locally and save it as Ooxx.txt #h Ost.sftp_get (Remote_path, Local_path) #host. Sftp_get_dir (Remote_path, Local_path) except:print U "Please see if the data download is complete !! "End_time = Time.time () # puts the local xxoo.txt put to the far end and remains asxxoo.txt# host.sftp_put (LocalFile, remotefile) # Get all files from the remote Remote_path directory to the local Local_path directory # HOST.SFTP_GET_DIR (remote _path, Local_path) # put all the files in the local Local_path directory to the remote Remote_path directory # Host.sftp_put_dir (Remote_path, Local_path)

Ip.list File File format:

Operation Result:

Additional Information
The user name needs to have executable permissions, otherwise it fails.

Python Run Nmon usage instructions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.