1. 连接 linux
import paramiko
def main(ip, port, username, password):
res_data = []
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port, username, password)
shell_in = "df -Tk"
stdin, stdout, stderr = ssh.exec_command(shell_in)
if stdout:
data = stdout.readlines()
for line_num, line in enumerate(data):
if line_num > 0:
temp = line.rstrip('\n').split()
if temp[1] in ['ext4']:
res_data.append({
'file_system': temp[0],
'file_system_type': temp[1],
'total_size': round(int(temp[2])/1024/1024, 2),
'used_size': round(int(temp[3])/1024/1024, 2),
'available_size': round(int(temp[4])/1024/1024, 2),
'used_percent': temp[5],
'mount_point': temp[6]
})
if stderr:
data = stdout.readlines()
if data:
ssh.close()
raise ValueError
ssh.close()
return res_data
2. 连接 windows
import winrm
"""
wmic LogicalDisk get Caption,Description,FileSystem,FreeSpace,Size
Caption Description FileSystem FreeSpace Size
C: 本地固定磁盘 NTFS 8858968064 157286395904
D: 本地固定磁盘 NTFS 65570955264 209714147328
E: 本地固定磁盘 NTFS 39923621888 144040783872
"""
def main(ip, port, username, password):
res_data = []
shell_in = 'wmic LogicalDisk get Description,FileSystem,FreeSpace,Name,Size'
win = winrm.Session(ip, auth=(username, password), transport='ntlm')
data = win.run_ps(shell_in)
if data.status_code == 0:
std_out = str(data.std_out, 'gbk')
if std_out:
for line_num, data in enumerate(std_out.split('\n')):
if line_num > 0:
temp = data.rstrip('\r').split()
if temp:
res_data.append({
'name': temp[3],
'description': temp[0],
'file_system': temp[1],
'available_size': round(int(temp[2]) / 1024 / 1024 / 1024, 2),
'total_size': round(int(temp[4]) / 1024 / 1024 / 1024, 2),
'used_size': round((int(temp[4]) - int(temp[2])) / 1024 / 1024 / 1024, 2),
'used_percent': str(100 - round(int(temp[2]) / int(temp[4]) * 100, 2)) + '%',
})
return res_data
else:
raise ValueError(data.status_code)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!