import paramiko
def get_high_memory_usage_processes(hostname, port, username, password):
# 创建SSH客户端实例
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接到远程Linux服务器
ssh.connect(hostname, port, username, password)
# 执行ps命令获取内存使用量较高的进程
stdin, stdout, stderr = ssh.exec_command("ps aux --sort=-%mem | head -n 6")
output = stdout.read().decode()
# 解析命令输出结果,提取进程信息
processes = []
for line in output.splitlines():
fields = line.split()
pid, mem, command = int(fields[1]), float(fields[3]), " ".join(fields[10:])
processes.append((pid, mem, command))
# 关闭SSH连接
ssh.close()
return processes
if __name__ == "__main__":
hostname = "your_remote_server_ip"
port = 22
username = "your_username"
password = "your_password"
high_memory_usage_processes = get_high_memory_usage_processes(hostname, port, username, password)
for process in high_memory_usage_processes:
print("PID: {}, Memory Usage: {}%, Command: {}".format(*process))