use paramiko to connect remote server and execute command

#!/usr/bin/env python

import sys, paramiko

hostname = ''
password = ''
command = 'ls'

username = ""
port = 22

try:
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.WarningPolicy)

    client.connect(hostname, port=port, username=username, password=password)

    stdin, stdout, stderr = client.exec_command(command)
    print(stdout.read(), end='')

finally:
    client.close()


原文地址:https://www.cnblogs.com/otfsenter/p/9678354.html