简体   繁体   English

如何通过python或jython建立ssh连接并远程运行脚本?

[英]How to establish a ssh connection through python or jython and run a script remotly?

Hi I want to know how i can establish a ssh connection through python or jython and run a script file remotly on another machine. 嗨,我想知道如何通过python或jython建立ssh连接,并在另一台计算机上远程运行脚本文件。 I am not sure about the platform of the machine where i want to run the script? 我不确定要在其中运行脚本的计算机的平台吗? Please provide some sample code as well. 请提供一些示例代码。 I wont mind going for some other scripting language as well. 我也不会介意使用其他脚本语言。

The simplest/quickest way would be to use Python's os.system() to call the ssh command of the system: http://docs.python.org/library/os.html?highlight=system#os.system 最简单/最快捷的方法是使用Python的os.system()调用系统的ssh命令: http ://docs.python.org/library/os.html?highlight=system#os.system

Eg this works on Ubuntu 例如,这适用于Ubuntu

import os
os.system('/usr/bin/ssh localhost ls -la /')

runs 'ls -la /' through ssh on localhost. 在本地主机上通过ssh运行'ls -la /'。

You will need to provide the proper command-line options to ssh, and you can set up the ssh keys to connect securely but without a password to a remote host for executing command(s): check this out: http://www.g-loaded.eu/2005/11/10/ssh-with-keys/ or google for more info on ssh keys. 您将需要为ssh提供适当的命令行选项,并且可以设置ssh密钥以安全地连接,但没有密码即可连接到远程主机来执行命令:签出: http:// www。 g-loaded.eu/2005/11/10/ssh-with-keys/或google,以获取有关ssh键的更多信息。

You might want to take a look at Fabric : 您可能想看看Fabric

Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks. Fabric是一个Python库和命令行工具,用于简化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 )以及上载/下载文件的基本操作套件,以及诸如提示正在运行的用户进行输入或中止执行等辅助功能。

You should try execnet , it provide API to execute arbitrary Python code on remote machine. 您应该尝试execnet ,它提供API以在远程计算机上执行任意Python代码。 I use it on Linux machines but it work both in Windows and Linux. 我在Linux机器上使用它,但是在Windows和Linux上都可以使用。

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

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