[英]Execute remote python script via SSH
I want to execute a Python script on several (15+) remote machine using SSH. 我想使用SSH在几台(15+)远程机器上执行Python脚本。 After invoking the script/command I need to disconnect ssh session and keep the processes running in background for as long as they are required to. 在调用脚本/命令之后,我需要断开ssh会话并保持进程在后台运行,只要它们是必需的。
I have used Paramiko and PySSH in past so have no problems using them again. 我过去使用过Paramiko和PySSH所以再次使用它们没有问题。 Only thing I need to know is how to disconnect a ssh session in python (since normally local script would wait for each remote machine to complete processing before moving on). 我唯一需要知道的是如何在python中断开ssh会话(因为通常本地脚本会等待每个远程机器在继续之前完成处理)。
On Linux machines, you can run the script with 'at'. 在Linux机器上,您可以使用'at'运行脚本。
echo "python scriptname.py" ¦ at now echo“python scriptname.py”|现在
If you are going to perform repetitive tasks on many hosts, like for example deploying software and running setup scripts, you should consider using something like Fabric 如果要在许多主机上执行重复任务,例如部署软件和运行安装脚本,则应考虑使用Fabric之类的东西
Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. Fabric是一个Python(2.5或更高版本)库和命令行工具,用于简化SSH在应用程序部署或系统管理任务中的使用。
It provides a basic suite of operations for executing local or remote shell commands (normally or via sudo) and uploading/downloading files, as well as auxiliary functionality such as prompting the running user for input, or aborting execution. 它提供了一套基本的操作套件,用于执行本地或远程shell命令(通常或通过sudo)和上传/下载文件,以及辅助功能,如提示正在运行的用户输入或中止执行。
Typical use involves creating a Python module containing one or more functions, then executing them via the fab command-line tool. 典型用法包括创建包含一个或多个函数的Python模块,然后通过fab命令行工具执行它们。
You can even use tmux in this scenario. 您甚至可以在此方案中使用tmux 。
As per the tmux documentation: 根据tmux文档:
tmux is a terminal multiplexer. tmux是终端多路复用器。 It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal. 它允许您在一个终端中的多个程序之间轻松切换,分离它们(它们在后台继续运行)并将它们重新连接到不同的终端。 And do a lot more 并做更多
From a tmux session, you can run a script, quit the terminal, log in again and check back as it keeps the session until the server restart. 在tmux会话中,您可以运行脚本,退出终端,再次登录并在保持会话之前进行检查,直到服务器重新启动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.