Once in a while I need a way to execute something on remote machine as part of scripting I'm currently working on.

Linux

With Linux distros (it works fine with other systems that are hosting ssh server, including Windows) I usually use paramiko - it's quite easy to use and supports all what one might need (including key-based auth, which inter alia gives more granular control which makes it far more reasonable solution than using passwords). Here's simple script to check how much of space we're using on /tmp

import paramiko, re

hostname = '192.168.1.110'
username = 'piotr'
password = 'piotr'

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname, username=username, password=password)
stdin, stdout, stderr = ssh.exec_command('df /tmp')
print(re.findall(r'\s(\d+)%', stdout.read().decode('ascii'))[0])
ssh.close()

Windows

For Windows-based machines I prefer pypsexec - it doesn't require too much setup on remote machine. There's great manual at Python Package Index so I don't think there's a point in replicating it's contents here. Lets check how much of storage is used on drive C:

import pypsexec.client

hostname = '192.168.1.80'
username = 'Piotr'
password = 'Piotr'

c = pypsexec.client.Client(hostname, username=username, password=password, encrypt=False)
c.connect()
try:
	c.create_service()
	stdout = c.run_executable("powershell.exe", arguments="-", stdin=
"""$disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'"
[math]::round(($disk.Size-$disk.FreeSpace)/$disk.Size*100)
exit 0
""".encode('utf-8'), timeout_seconds=30)
	print(stdout[0].decode('utf-8').strip())
finally:
	c.remove_service()
	c.disconnect()