I need to check the available hard disk space for a remote linux host in Python.
Currently i am using shutil to check the same on my host.
import shutil total, used, free = shutil.disk_usage("D:/")
CodePudding user response:
Since I can't comment, here as answer: If you are on a linux system you can use simply an os call, no need to include a new library.
import os
stream = os.popen('ssh host@target "df -h"')
output = stream.read()
CodePudding user response:
You may use ssh to execute commands on the remote machine. paramiko
is one library that provides ssh functions. Sample code:
from paramiko import SSHClient
ssh = SSHClient()
ssh.load_system_host_keys()
# more options found at https://docs.paramiko.org/en/stable/api/client.html
# ssh.connect(ip,port,username,password)
ssh.connect('serverip', username='youruser') # using ssh keys
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('df -h /')
outlines = ssh_stdout.readlines()
resp = ''.join(outlines)
print(resp)
Alternatively you can setup some monitor service in the remote server, and use some convenient webpages or api to get the information. but that requires more resources.