import paramiko
from paramiko.ssh_exception import AuthenticationException, SSHException
host = "10.24.217.31"
port = 22
username = "root"
password = "eV&**!!2024"
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password)
stdin, stdout, stderr = ssh.exec_command('free -g')
output = stdout.read().decode()
# 分析输出并提取内存信息
lines = output.split('\n')
mem_info = {}
for line in lines:
if 'Mem' in line:
total, used, free, shared, buffers, cached = line.split()[1:][:6]
mem_info['total'] = int(total)
mem_info['used'] = int(used)
mem_info['free'] = int(free)
mem_info['buffers'] = int(buffers)
mem_info['cached'] = int(cached)
break
ssh.close()
print(mem_info)
except AuthenticationException:
print("Authentication failed.")
except SSHException as e:
print(f"SSH error occurred: {e}")
except Exception as e:
print(f"Error occurred: {e}")
07-12
1449
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-22
2362
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)