随笔 - 120  文章 - 0  评论 - 35  阅读 - 85万

通过paramiko模块在远程主机上执行命令

安装paramiko模块

/usr/local/python36/bin/pip3 install paramiko

1、获取cpu使用率

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/python
#coding=utf8
#target: get the cpu's used of remote linux system
 
import paramiko
 
def getlinux(ssh):
    # get the result of executing command
    stdin, stdout, stderr = ssh.exec_command(r"sar 2 3|awk 'END{print (100-$NF)*100}'")
    # judge if there is any error
    err = stderr.readlines()
    if len(err) > 0:
        return err
    else:
        stdout_content = stdout.readlines()
    result = stdout_content
    if len(result) == 0:
        print("there is something wrong when executing sar command")
    else:
        return round(float(result[0].strip()),2)
 
if __name__ == '__main__':
    # create ssh object
    ssh = paramiko.SSHClient()
    # connect target host by ssh
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname="192.168.223.136",port=22,username="root",password="redhat")
    # get the result of target host
    result = getlinux(ssh)
    # close ssh connection
    ssh.close()
    print(str(result)+"%s used")

2、获取内存使用率

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/python
#coding=utf8
#target: get the memory's used of linux system
 
import paramiko
 
def getlinux(ssh):
    # executing command
    stdin, stdout, stderr = ssh.exec_command(r"free -m|awk 'NR==2{print (($3 - $6 - $7)/$2)*100}'")
    err = stderr.readlines()
    if len(err) > 0:
        return err
    else:
        stdout_content = stdout.readlines()
    result = stdout_content
    if len(result) == 0:
        print("there is something wrong when executing free -m")
    else:
        return round(float(result[0].strip()),2)
 
if __name__ == '__main__':
    # create ssh object
    ssh = paramiko.SSHClient()
    # connect target host by ssh
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname="192.168.223.136",port=22,username="root",password="redhat")
    result = getlinux(ssh)
    ssh.close()
    print(str(result)+"% used")

 3、获取磁盘使用率:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/python
#coding=utf8
#target: get the used of disk
 
import paramiko
 
def getlinux(ssh):
    stdin, stdout, stderr = ssh.exec_command(r"df -h|awk 'NR > 1{if($1==$NF){print $1}else{print $0}}'")
    err = stderr.readlines()
    if len(err) > 0:
        return err
    else:
        stdout_content = stdout.readlines()
    result = stdout_content
    if len(result) == 0:
        print("there is something wrong when executing command")
    else:
        return result
 
 
if __name__ == '__main__':
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname="192.168.223.136",port=22,username="root",password="redhat")
    result = getlinux(ssh)
    ssh.close()
    for i in result:
        print("the used of %s has used %s" % (i.strip().split()[5],i.strip().split()[4]))

  

posted on   wadeson  阅读(777)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示