import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
pkey = '当前目录下自己远端ssh设备的key'
key = paramiko.RSAKey.from_private_key_file(pkey)
ssh.connect('设备IP', username='设备用户名', pkey=key)
stdin, stdout, stderr = ssh.exec_command("free -h | sed -n '2p'|awk '{print $2}'")
totalmem = float(stdout.read().decode().split('G')[0])
print(totalmem)
stdin, stdout, stderr = ssh.exec_command("free -h | sed -n '3p'|awk '{print $3}'")
freemem = float(stdout.read().decode().split('G')[0])
use = round(float(freemem) / float(totalmem), 2)
print('当前内存使用率为:' + str(use*100)+'%')
#--------------------------------------------------------------------------------------------------
stdin, stdout, stderr = ssh.exec_command("top -b -n1 | sed -n '3p'|awk '{print $2}'")
cpu_usage = stdout.read().decode()
#print(cpu_usage)
print('当前CPU使用率为:' + cpu_usage.strip() + '%')
ssh.close()
paramiko获取远端设备的内存使用率和CPU使用率
最新推荐文章于 2024-03-15 18:07:05 发布