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()


posted @ 2018-09-19 23:32  idlewith  阅读(115)  评论(0编辑  收藏  举报