首先使用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