iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python 运行nmon使用说明
  • 447
分享到

python 运行nmon使用说明

使用说明pythonnmon 2023-01-31 06:01:40 447人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

python 运行nmon使用说明 第一步:安装:paramikowindow安装方法:pip install paramiko 参考:Http://www.jb51.net/article/97655.htm第二步:以下代码包含发送命令与

python 运行nmon使用说明

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

参考:Http://www.jb51.net/article/97655.htm
第二步:
以下代码包含发送命令与下载单个文件与目录文件下的所有;
缺点是没有写出多进程方式执行;
#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

class linux(object):

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 = ''
    # 链接失败的重试次数
    self.try_times = 3

# 调用该方法连接远程主机
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()

# 发送要执行的命令
def send(self, command):
    self.connect()
    ssh = paramiko.SSHClient()
    ssh._transport = self.__transport
    # 执行命令
    stdin, stdout, stderr = ssh.exec_command(command)
    # 获取命令结果
    result = stdout.read()
    print result
    self.close()

# get单个文件
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单个文件
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()

# ------获取远端linux主机上指定目录及其子目录下的所有文件------
def __get_all_files_in_remote_dir(self, sftp, remote_dir):
    # 保存所有文件的列表
    all_files = list()

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

    # 获取当前指定目录下的所有目录及文件,包含属性值
    files = sftp.listdir_attr(remote_dir)
    for x in files:
        # remote_dir目录中每一个文件或目录的完整路径
        filename = remote_dir + '/' + x.filename
        # 如果是目录,则递归处理该目录,这里用到了stat库中的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()

    # 获取当前指定目录下的所有目录及文件,包含属性值
    files = os.listdir(local_dir)
    for x in files:
        # local_dir目录中每一个文件或目录的完整路径
        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每一个文件
    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)

# 获取远端linux主机上指定目录及其子目录下的所有文件
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)

    # 获取远端linux主机上指定目录及其子目录下的所有文件
    all_files = self.__get_all_files_in_remote_dir(sftp, remote_dir)
    # 依次get每一个文件
    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)  # 取基础文件的父目录
    for i in s:
        path = os.path.join(base_path, i)  # 拼路径(基础文件的父目录+以年月日命名的文件名)
        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)  # 复制文件
    print('复制完成!!')

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)

            # 将远端的xxoo.txt get到本地,并保存为ooxx.txt
            #host.sftp_get(remote_path, local_path)
            #host.sftp_get_dir(remote_path, local_path)

except:
    print u"请查看数据是否下载完成!!"
end_time = time.time()

# 将本地的xxoo.txt put到远端,并保持为xxoo.txt
# host.sftp_put(localfile, remotefile)
# 将远端remote_path目录中的所有文件get到本地local_path目录
# host.sftp_get_dir(remote_path, local_path)
# 将本地local_path目录中的所有文件put到远端remote_path目录
# host.sftp_put_dir(remote_path, local_path)

ip.list文件文件格式:

运行结果:

补充说明
用户名需要有可执行权限,否则失败。

--结束END--

本文标题: python 运行nmon使用说明

本文链接: https://www.lsjlt.com/news/190396.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python 运行nmon使用说明
    python 运行nmon使用说明 第一步:安装:paramikowindow安装方法:pip install paramiko 参考:http://www.jb51.net/article/97655.htm第二步:以下代码包含发送命令与...
    99+
    2023-01-31
    使用说明 python nmon
  • Python运算符说明
    Python运算符说明运算符说明lambdalambda表达式or布尔“或”and布尔“与”not x布尔“非”in,not in成员测试is,is not同一性测试<,<=,>,>=,!=,==比较运算符|按位或^...
    99+
    2023-01-31
    运算符 Python
  • Python sys 使用说明
    获取linux下python的路径,以及执行python时使用的参数。 代码如下:   #!/usr/bin/python # -*- encoding:utf-8 -*- # time:2012-07-06 import sys;   p...
    99+
    2023-01-31
    使用说明 Python sys
  • Python numpy.power()函数使用说明
    power(x, y) 函数,计算 x 的 y 次方。 示例: x 和 y 为单个数字: import numpy as np print(np.power(2, 3)) 8...
    99+
    2022-11-11
  • 如何进行bapi_acc_document_post使用说明
    如何进行bapi_acc_document_post使用说明,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。bapi_acc_document_post使用说明bapi_ac...
    99+
    2023-06-04
  • Python中turtle.write方法使用说明
    目录turtle.write方法使用说明例子绘制一朵小花的例子如何使用turtle.write方法将文字显示为一个圆圈?总结turtle.write方法使用说明 关于turtle可参...
    99+
    2022-11-13
  • Python-apply(lambdax:)的使用及说明
    目录Python-apply(lambda x: )使用python的lambda函数匿名函数的定义匿名函数的应用总结Python-apply(lambda x: )使用 def i...
    99+
    2023-02-01
    Python apply apply(lambda x: ) apply lambda x:
  • z3py使用说明
    http://z3prover.github.io/api/html/z3.html http://www.cs.tau.ac.il/~msagiv/courses/asv/z3py/guide-examples.htm 学习...
    99+
    2023-01-31
    使用说明 z3py
  • window.dialogArguments 使用说明
    f1.php页面JS代码,第二个传的参数必须是self不能是别的 复制代码 代码如下: <script type="text/javascript"> function ...
    99+
    2022-11-21
    dialogArguments
  • python的partial()用法说明
    在functools模块中有一个工具partial(),可以用来"冻结"一个函数的参数,并返回"冻结"参数后的新函数。 很简单的解释,也是官方手册给的示例。对于int()函数,它可以将给定的数值转换成十进制整数,转换时可以指定以几进制的方...
    99+
    2023-01-30
    python partial
  • Python的functools模块使用及说明
    目录partialupdate_wrapperwrapsreducecmp_to_keylru_cachesingledispatchpartial 用于创建一个偏函数,将...
    99+
    2022-11-11
  • python numpy.linalg.norm函数的使用及说明
    目录numpy.linalg.norm函数的使用np.linalg.norm()函数用法总结numpy.linalg.norm函数的使用 1、linalg = linear(线性)+...
    99+
    2023-02-05
    python函数 numpy.linalg.norm函数 numpy.linalg.norm
  • ZLibrary使用说明-Zlirbrary
    ZLibrary使用说明 如果您是一位书虫,那么ZLibrary是一个值得一试的网站。该网站提供了大量的免费电子书籍,涵盖了各种不同的主题和类别。下面是一些有关如何使用ZLibrary的详细说明: 第1步:访问ZLibrary网站 要...
    99+
    2023-09-15
    服务器 运维 Powered by 金山文档
  • Android ContentResolver使用说明
    Android是如何实现应用程序之间数据共享的?一个应用程序可以将自己的数据完全暴露出去,外界更本看不到,也不用看到这个应用程序暴露的数据是如何存储的,或者是使用数据库还是使用...
    99+
    2022-06-06
    Android
  • Xilinx URAM使用说明
    Xilinx URAM(Ultra RAM)是一种高性能、低延迟的存储器资源,用于在Xilinx FPGA器件中实现大容量的存储和高...
    99+
    2023-09-23
    Xilinx
  • mysql8.0JSON_CONTAINS的使用说明
    目录JSON_CONTAINS的使用语法案例JSON_CONTAINS函数问题结构如下JSON_CONTAINS的使用 语法 JSON_CONTAINS(json_doc, val[...
    99+
    2022-11-13
  • RCMD的使用说明
    这篇文章主要介绍“RCMD的使用说明”,在日常操作中,相信很多人在RCMD的使用说明问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”RCMD的使用说明”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!*****...
    99+
    2023-06-08
  • 【pytorch】torch.cdist使用说明
    使用说明 torch.cdist的使用介绍如官网所示, 它是批量计算两个向量集合的距离。 其中, x1和x2是输入的两个向量集合。 p 默认为2,为欧几里德距离。 它的功能上等同于 scipy.sp...
    99+
    2023-09-02
    pytorch 深度学习 python
  • 使用Python脚本对GiteePages进行一键部署的使用说明
    本次系统环境 os: Deepin(Linux) Python: 3.7 lib: PyYAML=5.3.1 | selenium=3.141.0 extend_driver: chromedriver 使用说明...
    99+
    2022-06-02
    Python GiteePages一键部署 Python 一键部署
  • Python NumPy中diag函数的使用说明
    NumPy包中的内置diag函数很有意思。 假设创建一个1维数组a,和一个3*3数组b: import numpy as np a = np.arange(1, 4) b = np.arange(1, 10).r...
    99+
    2022-06-02
    Python NumPy diag函数
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作