千家信息网

python windows下如何通过SSH获取linux系统cpu、内存、网络使用情况

发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,今天就跟大家聊聊有关python windows下如何通过SSH获取linux系统cpu、内存、网络使用情况,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可
千家信息网最后更新 2025年12月03日python windows下如何通过SSH获取linux系统cpu、内存、网络使用情况

今天就跟大家聊聊有关python windows下如何通过SSH获取linux系统cpu、内存、网络使用情况,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

做个程序,需要用到系统的cpu、内存、网络的使用情况。于是乎就自己写一个。

一、计算cpu的利用率

要读取cpu的使用情况,首先要了解/proc/stat文件的内容,下图是/proc/stat文件的一个例子:

cpu、cpu0、cpu1……每行数字的意思相同,从左到右分别表示user、nice、system、idle、iowait、irq、softirq。根据系统的不同,输出的列数也会不同,比如ubuntu 12.04会输出10列数据,centos 6.4会输出9列数据,后边几列的含义不太清楚,每个系统都是输出至少7列。没一列的具体含义如下:

user:用户态的cpu时间(不包含nice值为负的进程所占用的cpu时间)

nice:nice值为负的进程所占用的cpu时间

system:内核态所占用的cpu时间

idle:cpu空闲时间

iowait:等待IO的时间

irq:系统中的硬中断时间

softirq:系统中的软中断时间

以上各值的单位都是jiffies,jiffies是内核中的一个全局变量,用来记录自系统启动一来产生的节拍数,在这里把它当做单位时间就行。

intr行中包含的是自系统启动以来的终端信息,第一列表示中断的总此数,其后每个数对应某一类型的中断所发生的次数

ctxt行中包含了cpu切换上下文的次数

btime行中包含了系统运行的时间,以秒为单位

processes/total_forks行中包含了从系统启动开始所建立的任务个数

procs_running行中包含了目前正在运行的任务的个数

procs_blocked行中包含了目前被阻塞的任务的个数

由于计算cpu的利用率用不到太多的值,所以截图中并未包含/proc/stat文件的所有内容。

知道了/proc文件的内容之后就可以计算cpu的利用率了,具体方法是:先在t1时刻读取文件内容,获得此时cpu的运行情况,然后等待一段时间在t2时刻再次读取文件内容,获取cpu的运行情况,然后根据两个时刻的数据通过以下方式计算cpu的利用率:100 - (idle2 - idle1)*100/(total2 - total1),其中total = user + system + nice + idle + iowait + irq + softirq。python代码实现如下:

#-*- encoding: utf-8 -*-import paramikoimport timedef getSSHOutput(host,userName,password,port):    sshClient = paramiko.SSHClient()    sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())    sshClient.connect(host,port,userName,password)    command = r"cat /proc/stat"    stdin,stdout,stderr = sshClient.exec_command(command)    outs = stdout.readlines()    sshClient.close()    return outsdef getCpuInfo(outs):      for line in outs:        line = line.lstrip()          counters = line.split()        if len(counters) < 5:            continue        if counters[0].startswith('cpu'):            break    total = 0    for i in range(1, len(counters)):        total = total + long(counters[i])    idle = long(counters[4])    return {'total':total, 'idle':idle}           def calcCpuUsage(counters1, counters2):    idle = counters2['idle'] - counters1['idle']    total = counters2['total'] - counters1['total']    return 100 - (idle*100/total)           if __name__ == '__main__':    while True:        SSHOutput = getSSHOutput("192.168.144.128","root","******",22)        counters1 = getCpuInfo(SSHOutput)          time.sleep(1)        SSHOutput = getSSHOutput("192.168.144.128","root","******",22)        counters2 = getCpuInfo(SSHOutput)            print (calcCpuUsage(counters1, counters2))

二、计算内存的利用率

计算内存的利用率需要读取的是/proc/meminfo文件,该文件的结构比较清晰。

需要知道的是内存的使用总量为used = total - free - buffers - cached,python代码实现如下:

#-*- encoding: utf-8 -*-import paramikoimport timedef getSSHOutput(host,userName,password,port):    sshClient = paramiko.SSHClient()    sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())    sshClient.connect(host,port,userName,password)    command = r"cat /proc/meminfo"    stdin,stdout,stderr = sshClient.exec_command(command)    outs = stdout.readlines()    sshClient.close()    return outs    def getMemInfo(outs):    res = {'total':0, 'free':0, 'buffers':0, 'cached':0}    index = 0    for line in outs:        if(index == 4):            break        line = line.lstrip()                 memItem = line.lower().split()        if memItem[0] == 'memtotal:':                     res['total'] = long(memItem[1])            index = index + 1            continue        elif memItem[0] == 'memfree:':            res['memfree'] = long(memItem[1])            index = index + 1            continue        elif memItem[0] == 'buffers:':            res['buffers'] = long(memItem[1])            index = index + 1            continue        elif memItem[0] == 'cached:':            res['cached'] = long(memItem[1])            index = index + 1            continue    return res           def calcMemUsage(counters):    used = counters['total'] - counters['free'] - counters['buffers'] - counters['cached']    total = counters['total']    return used*100/total               if __name__ == '__main__':    while True:        SSHOutput = getSSHOutput("192.168.144.128","root","******",22)        counters = getMemInfo(SSHOutput)          time.sleep(1)        print (calcMemUsage(counters))

三、获取网络的使用情况

获取网络使用情况需要读取的是/proc/net/dev文件,如下图所示,里边的内容也很清晰,不做过多的介绍,直接上python代码,取的是eth0的发送和收取的总字节数:

#-*- encoding: utf-8 -*-import paramikoimport timedef getSSHOutput(host,userName,password,port):      sshClient = paramiko.SSHClient()    sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())    sshClient.connect(host,port,userName,password)    command = r"cat /proc/net/dev"    stdin,stdout,stderr = sshClient.exec_command(command)    outs = stdout.readlines()    sshClient.close()    return outsdef getNetInfo(dev,outs):    res = {'total':0, 'in':0, 'out':0}    for line in outs:        if line.lstrip().startswith(dev):            line = line.replace(':', ' ')            items = line.split()            res['in'] = long(items[1])            res['out'] = long(items[len(items)/2 + 1])            res['total'] = res['in'] + res['out']    return resif __name__ == '__main__':    SSHOutput = getSSHOutput("192.168.144.128","root","******",22)    print getNetInfo('eth0',SSHOutput)

看完上述内容,你们对python windows下如何通过SSH获取linux系统cpu、内存、网络使用情况有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

系统 时间 内容 文件 情况 内存 利用率 中包 网络 输出 运行 个数 代码 任务 单位 数据 时刻 utf-8 不同 内核 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 网络安全建设含义 中国药学考研大数据库 为了让两台wins服务器 创建和管理数据库的实验分析 网络安全需要学什么 关于网络安全的电影 网络安全中职技能竞赛承办 北京pdu服务器电源订购 网络安全涉及的方面有哪些 通用数据库模型有哪些并举例说明 倾动互联网科技有限公司 中原工学院网络安全 广州市睿讯软件开发有限公司 数据库与程序设计原理 e 联盟小游戏服务器 如何打开我的世界里面的服务器 灵武政务软件开发公司好不好 电视服务器无响应是啥情况呢 部队网络安全观后感800 网络安全与法治课题 网络安全经典 为了让两台wins服务器 腾讯云购买香港服务器需要实名吗 倾动互联网科技有限公司 公共人力资源管理相关论文数据库 闵行区电话数据库服务电话 销售软件开发报价 香港 站群 服务器 网络安全破坏主题手抄报大全 梦想灵谷宝可梦服务器下载
0