简体   繁体   English

在Python中创建和维护几个ssh会话

[英]Create and maintain several ssh sessions in Python

Once my program has been launched, it opens any number of ssh sessions (user defined) and runs specific commands on the servers indefinitely (while true loop) or until the user quits. 一旦我的程序启动,它将打开任意数量的ssh会话(用户定义)并无限期地在服务器上运行特定命令(同时为真循环)或直到用户退出。 For efficiency reasons I want to create each session only once, then be able to run the commands until the user quits. 出于效率原因,我想只创建一次会话,然后能够运行命令直到用户退出。

How can I do this in python? 我怎么能在python中这样做? I ran across a cool method in another post which used subprocess to run a command and capture its STDOUT. 我在另一个帖子中遇到了一个很酷的方法,它使用subprocess来运行命令并捕获它的STDOUT。 How can I first initiate the session, then run looped commands? 如何首先启动会话,然后运行循环命令?

Any links to process-like stuff in Python would also be appreciated. 任何链接到Python中的类似过程的东西也将受到赞赏。 This is my first python application. 这是我的第一个python应用程序。

Ignoring Python for the moment, you can multiplex ssh sessions by adding this 暂时忽略Python,您可以通过添加它来复用ssh会话

ControlMaster auto
ControlPath /tmp/ssh_mux_%h_%p_%r
ControlPersist 1h

to your ~/.ssh/config file. 到你的~/.ssh/config文件。 After connecting to a machine once, the ssh session to this machine will stay open, and subsequent commands will execute on this machine near-instantaneously thereafter. 连接到机器一次后,该机器的ssh会话将保持打开状态,此后的命令将在此机器上几乎立即执行。 You could then use Python subprocess to call ssh and execute commands on that machine as-needed, and the session will be reused without having to do anything special. 然后,您可以根据需要使用Python子进程调用ssh并在该计算机上执行命令,并且可以重用会话而无需执行任何特殊操作。

You could also call ssh with the -F flag pointing to an alternate config file, if you'd rather not make the session multiplexing the default behavior (or you're deploying for other users who might not have it as their default). 如果您不想使会话多路复用成为默认行为(或者您正在为可能没有将其作为默认行为的其他用户进行部署),您也可以使用指向备用配置文件的-F标志调用ssh

OPTION 1: You can re-use a ssh process by redirecting input using a PIPE. 选项1:您可以通过使用PIPE重定向输入来重新使用ssh进程。

Here is a basic example: 这是一个基本的例子:

[(Z) </tmp> ]% touch input_file
[(Z) </tmp> ]% tailf input_file | ssh <remote_host>

Now try writing something into the file 现在尝试在文件中写入内容

[(Z) </tmp> ]% echo "date" >> /tmp/input_file

Here is a way to make use of this in Python using subprocess module. 这是一种使用子进程模块在Python中使用它的方法。

import subprocess
SSH_CMD = "cat -| /usr/bin/ssh -o PasswordAuthentication=no -T -x %s "
HOSTNAME = "127.0.0.1"
s = subprocess.Popen(SSH_CMD%HOSTNAME , shell=True, close_fds=True, 
stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)

This starts a subprocess which can be re-used. 这启动了一个可以重复使用的子进程。 Please note that close_fds=True is required because of a known bug ( http://bugs.python.org/issue2320 ). 请注意,由于已知错误( http://bugs.python.org/issue2320 ),因此需要close_fds=True

>>> REMOTE_CMD = "date"
>>> s.stdin.write( REMOTE_CMD +
... "\necho 'remote command completed with exit code = '$?\n")
>>> s.stdout.readline()
'Thu Feb 16 20:01:36 PST 2012\n'
>>> s.stdout.readline()
'remote command completed with exit code = 0\n'

echo 'remote command completed with exit code = '$?\\n line is used to know that the remote command finished and it is done writing to s.stdout. echo 'remote command completed with exit code = '$?\\n line用于知道远程命令已完成,并且已完成写入s.stdout。 This is also useful to know the exit code of the remote command. 这对于了解远程命令的退出代码也很有用。

To use the same subprocess for executing another remote command: 要使用相同的子进程执行另一个远程命令:

>>> REMOTE_CMD = "uptime"
>>> s.stdin.write( REMOTE_CMD +
... "\necho 'remote command completed with exit code = '$?\n")
>>> s.stdout.readline()
' 20:02:17 up 28 days,  9:15, 48 users,  load average: 0.01, 0.02, 0.05\n'
>>> s.stdout.readline()
'remote command completed with exit code = 0\n'

Coming back to your question, once you create a ssh subprocess, you can keep sending the remote commands. 回到你的问题,一旦你创建了一个ssh子进程,你就可以继续发送远程命令了。 Once the user is quits, you can kill the subprocess. 用户退出后,您可以终止子流程。

>>> s.kill()

OPTION 2: I have never used this, but ssh has a ControlMaster option for re-using ssh. 选项2:我从未使用过这个,但是ssh有一个ControlMaster选项可以重新使用ssh。 Check man page for ssh_config(5) 检查ssh_config的手册页(5)

Try using pexpect module. 尝试使用pexpect模块。 It allows opening and maintaining ssh sessions, which you can reuse to send in multiple commands. 它允许打开和维护ssh会话,您可以重复使用它来发送多个命令。 You can send in any commands and expect particular outputs based on which you can perform other logical operations. 您可以发送任何命令并期望基于您可以执行其他逻辑操作的特定输出。

尝试将它与多线程混合起来?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM