溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python 運(yùn)行nmon使用說(shuō)明

發(fā)布時(shí)間:2020-07-05 08:33:53 來(lái)源:網(wǎng)絡(luò) 閱讀:5720 作者:知止內(nèi)明 欄目:軟件技術(shù)

python 運(yùn)行nmon使用說(shuō)明

第一步:
安裝:paramiko
window安裝方法:pip install paramiko

參考:http://www.jb51.net/article/97655.htm
第二步:
以下代碼包含發(fā)送命令與下載單個(gè)文件與目錄文件下的所有;
缺點(diǎn)是沒(méi)有寫出多進(jìn)程方式執(zhí)行;
#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年 %s月%s日" %(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

定義一個(gè)類,表示一臺(tái)遠(yuǎn)端linux主機(jī)

class Linux(object):

通過(guò)IP, 用戶名,密碼,超時(shí)時(shí)間初始化一個(gè)遠(yuǎn)程Linux主機(jī)

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和chanel
    self.t = ''
    self.chan = ''
    # 鏈接失敗的重試次數(shù)
    self.try_times = 3

# 調(diào)用該方法連接遠(yuǎn)程主機(jī)
def connect(self):
    transport = paramiko.Transport((self.ip, self.port))
    transport.connect(username=self.username, password=self.password)
    self.__transport = transport

# 斷開連接
def close(self):
    self.__transport.close()

# 發(fā)送要執(zhí)行的命令
def send(self, command):
    self.connect()
    ssh = paramiko.SSHClient()
    ssh._transport = self.__transport
    # 執(zhí)行命令
    stdin, stdout, stderr = ssh.exec_command(command)
    # 獲取命令結(jié)果
    result = stdout.read()
    print result
    self.close()

# get單個(gè)文件
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 '下載完成'
    t.close()

# put單個(gè)文件
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 "上傳成功"
    t.close()

# ------獲取遠(yuǎn)端linux主機(jī)上指定目錄及其子目錄下的所有文件------
def __get_all_files_in_remote_dir(self, sftp, remote_dir):
    # 保存所有文件的列表
    all_files = list()

    # 去掉路徑字符串最后的字符'/',如果有的話
    if remote_dir[-1] == '/':
        remote_dir = remote_dir[0:-1]

    # 獲取當(dāng)前指定目錄下的所有目錄及文件,包含屬性值
    files = sftp.listdir_attr(remote_dir)
    for x in files:
        # remote_dir目錄中每一個(gè)文件或目錄的完整路徑
        filename = remote_dir + '/' + x.filename
        # 如果是目錄,則遞歸處理該目錄,這里用到了stat庫(kù)中的S_ISDIR方法,與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

# ------獲取本地指定目錄及其子目錄下的所有文件------
def __get_all_files_in_local_dir(self, local_dir):
    # 保存所有文件的列表
    all_files = list()

    # 獲取當(dāng)前指定目錄下的所有目錄及文件,包含屬性值
    files = os.listdir(local_dir)
    for x in files:
        # local_dir目錄中每一個(gè)文件或目錄的完整路徑
        filename = os.path.join(local_dir, x)
        # 如果是目錄,則遞歸處理該目錄
        if os.path.isdir(x):
            all_files.extend(self.__get_all_files_in_local_dir(filename))
        else:
            all_files.append(filename)
    return all_files

#獲取本地指定目錄及其子目錄下的所有文件
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)

    # 去掉路徑字符穿最后的字符'/',如果有的話
    if remote_dir[-1] == '/':
        remote_dir = remote_dir[0:-1]

    # 獲取本地指定目錄及其子目錄下的所有文件
    all_files = self.__get_all_files_in_local_dir(local_dir)
    # 依次put每一個(gè)文件
    for x in all_files:
        filename = os.path.split(x)[-1]
        remote_filename = remote_dir + '/' + filename
        print u'Put文件%s傳輸中...' % filename
        sftp.put(x, remote_filename)

# 獲取遠(yuǎn)端linux主機(jī)上指定目錄及其子目錄下的所有文件
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)

    # 獲取遠(yuǎn)端linux主機(jī)上指定目錄及其子目錄下的所有文件
    all_files = self.__get_all_files_in_remote_dir(sftp, remote_dir)
    # 依次get每一個(gè)文件
    for x in all_files:
        filename = x.split('/')[-1]
        local_filename = os.path.join(local_dir, filename)
        print u'Get文件%s傳輸中...' % 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:  # 取文件名中的年月日,存set去重
        data = i.split('_')[:3]
        d = ''
        for j in data:
            d = d + j + "_"
        s.add(d)
    base_path = os.path.dirname(base_file_path)  # 取基礎(chǔ)文件的父目錄
    for i in s:
        path = os.path.join(base_path, i)  # 拼路徑(基礎(chǔ)文件的父目錄+以年月日命名的文件名)
        if not os.path.exists(path):
            os.mkdir(r'%s\%s' % (base_path, i))  # 新建文件,以年月日命名
        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)  # 復(fù)制文件
    print('復(fù)制完成??!')

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(':')[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)

            # 將遠(yuǎn)端的xxoo.txt get到本地,并保存為ooxx.txt
            #host.sftp_get(remote_path, local_path)
            #host.sftp_get_dir(remote_path, local_path)

except:
    print u"請(qǐng)查看數(shù)據(jù)是否下載完成??!"
end_time = time.time()

# 將本地的xxoo.txt put到遠(yuǎn)端,并保持為xxoo.txt
# host.sftp_put(localfile, remotefile)
# 將遠(yuǎn)端remote_path目錄中的所有文件get到本地local_path目錄
# host.sftp_get_dir(remote_path, local_path)
# 將本地local_path目錄中的所有文件put到遠(yuǎn)端remote_path目錄
# host.sftp_put_dir(remote_path, local_path)

ip.list文件文件格式:

運(yùn)行結(jié)果:

補(bǔ)充說(shuō)明
用戶名需要有可執(zhí)行權(quán)限,否則失敗。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI