python PYTHON / SSH:连接到远程服务器并运行命令

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python PYTHON / SSH:连接到远程服务器并运行命令相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python

import sys
from subprocess import call

ssh_commands = {
    'instance1': 'ssh -i "my.pem" user@fqdn',
    'instance2': 'ssh user@fqdn -i ~/.ssh/my.pem -o ProxyCommand="ssh -i ~/.ssh/my.pem -W %h:%p -q user@fqdn"',
    'instance3': 'ssh user@fqdn',
    'instance4': 'sshpass -p my_password_here ssh -o PubkeyAuthentication=no -o StrictHostKeyChecking=no -o ServerAliveInterval=60 user@fqdn'
}

arguments = len(sys.argv)

if arguments == 1:
    print "Posibilities: "
    for title in ssh_commands:
        print "  - " + title
elif arguments == 2:
    call([ssh_commands[sys.argv[1]]], shell=True)
else:
    command = ssh_commands[sys.argv[1]].split(" ")
    command.append("-t")
    command += sys.argv[2:]
    call([' '.join(command)], shell=True)

以上是关于python PYTHON / SSH:连接到远程服务器并运行命令的主要内容,如果未能解决你的问题,请参考以下文章

python PYTHON / SSH:连接到远程服务器并运行命令

通过 ssh 隧道访问远程数据库(Python 3)

将Python连接到远程DB2服务器

Pycharm 无法连接到控制台进程退出代码 -1 远程解释器

python 3 paramiko ssh代理在第三台主机上使用远程命令转发跳转主机

如何使用 python 打开 SSH 隧道?