简体   繁体   English

通过SSH执行远程python脚本

[英]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会话(因为通常本地脚本会等待每个远程机器在继续之前完成处理)。

This might work, or something similar: 这可能有用,或类似的东西:

ssh user@remote.host nohup python scriptname.py &

Basically, have a look at the nohup command. 基本上,看看nohup命令。

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会话中,您可以运行脚本,退出终端,再次登录并在保持会话之前进行检查,直到服务器重新启动。

How to configure tmux on a cloud server 如何在云服务器上配置tmux

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

相关问题 通过SSH在远程服务器上运行Python脚本 - Running Python Script on Remote Server via SSH ssh到远程机器,脚本通过python密码 - ssh to remote machine with password in script via python 在ssh上执行受限的远程脚本,通过python paramiko与本地参数连接 - Execute restricted remote script on ssh connect with local arguments via python paramiko Amazon Web Service / Boto:通过localhost上的SSH上传和执行远程python / bash脚本 - Amazon Web Service/Boto: Upload and execute remote python/bash script via SSH on localhost 无法通过 SSH 在远程 VM 上运行 Python 脚本 - Can't run Python Script on remote VM via SSH 通过 python 在 windows 上执行 ssh - Execute ssh on windows via python 使用python脚本通过telnet在远程主机上执行命令 - Execute commands on remote host via telnet with python script 在远程节点执行 bash 脚本并从服务器通过 python 控制 - Execute bash script at remote node and controlled via python from server 如何使用SSH执行远程脚本 - How to execute a remote script using ssh 如何使用 python 脚本将 ssh 连接到远程 linux 服务器,并能够在 python 脚本中使用 ssh 时作为指定用户执行命令 - How to ssh to a remote linux server using python script and be able to execute command as the user specified while using ssh in the python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM