使用python连接到linux服务器读取出目录使用情况

本文介绍了如何使用Python的paramiko模块连接到Linux服务器并读取出指定目录的使用情况。通过创建方法,传入服务器IP和目录路径,利用df命令获取信息,并将结果以字典形式返回。示例中展示了当目录被falloc占用90%以上空间的情况。
摘要由CSDN通过智能技术生成

首先使用paramiko模块,封装连接服务器,输入命令方法,使用read()读取命令输入后的返回值,并返回为字符串

def server_conn(ip,command):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=ip,username='', password="")
    stdin, stdout, stderr = ssh.exec_command(command,get_pty=True)
    result = stdout.read()
    ssh.close()
    return result

创建方法,参数1填入linux服务器IP,参数2填入目录绝对地址

方法中使用df命令读取出linux服务器目录使用情况,返回为一个字典

# df查找目录使用情况,返回为一个列表
def document_used(ip,doc):
    read = server_conn(ip, 'df')
    dic = {}
    for i in read.splitlines():
        if doc in i:
            dic['total'] = i.split()[1]
            dic['used'] = i.split()[2]
            dic['percent'] = i.split()[4]
            return
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值