2009-08-05 96 views

回答

38

我還沒有嘗試過,但這個pysftp模塊可能會有所幫助,然後使用paramiko。我相信一切都是客戶端。

有趣的命令可能是.execute(),它可以在遠程機器上執行任意命令。 (該模塊還具有.get().put方法,這些方法暗指更多的FTP字符)。

UPDATE:

我重新寫的博客文章我原來掛到後,答案不再可用。引用這個答案的舊版本的一些評論現在看起來很奇怪。

+0

好找!只要你不關心定製錯誤響應,這個額外的抽象就會非常有用。 – Cascabel 2009-08-05 14:52:31

+0

ssh模塊取得了訣竅。簡單,工作正常。沒有通過Paramiko API進行搜索。 – 2009-08-06 15:20:11

+2

鏈接到您提供的鏈接中的ssh.py文件已損壞:/ – dgorissen 2011-06-29 13:15:11

8

您對「最簡單」的定義在這裏很重要 - 簡單的代碼意味着使用一個模塊(儘管「大型外部庫」是誇張的)。

我相信最新的(積極開發的)模塊是paramiko。它附帶下載的演示腳本,並提供詳細的在線API文檔。您也可以嘗試PxSSH,它包含在pexpect中。第一個鏈接中有一個簡短的示例以及文檔。

再次,爲了簡單起見,請注意,良好的錯誤檢測總是會讓您的代碼看起來更加複雜,但您應該能夠重複使用示例腳本中的大量代碼,然後將其忽略。

59

您可以使用Paramiko自己編碼,如上所示。或者,你可以看看面料,做所有的事情Python應用程序,你問:

面料是一個Python庫和 命令行工具,旨在 流線型部署應用程序或 執行系統管理任務 通過SSH協議。它提供了 工具,用於運行任意shell 命令(作爲普通登錄 用戶或通過sudo),上傳和 下載文件等等。

我認爲這符合您的需求。它也不是一個大型的庫,並且不需要安裝服務器,雖然它依賴於需要在客戶端上安裝的paramiko和pycrypt。

該應用曾經是here。現在可以找到here

* The official, canonical repository is git.fabfile.org 
* The official Github mirror is GitHub/bitprophet/fabric 

有它幾篇好文章,但因爲它在過去六個月已經改變了你要小心:

Deploying Django with Fabric

Tools of the Modern Python Hacker: Virtualenv, Fabric and Pip

Simple & Easy Deployment with Fabric and Virtualenv


後期:面料不再需要的paramiko安裝:

$ pip install fabric 
Downloading/unpacking fabric 
    Downloading Fabric-1.4.2.tar.gz (182Kb): 182Kb downloaded 
    Running setup.py egg_info for package fabric 
    warning: no previously-included files matching '*' found under directory 'docs/_build' 
    warning: no files found matching 'fabfile.py' 
Downloading/unpacking ssh>=1.7.14 (from fabric) 
    Downloading ssh-1.7.14.tar.gz (794Kb): 794Kb downloaded 
    Running setup.py egg_info for package ssh 
Downloading/unpacking pycrypto>=2.1,!=2.4 (from ssh>=1.7.14->fabric) 
    Downloading pycrypto-2.6.tar.gz (443Kb): 443Kb downloaded 
    Running setup.py egg_info for package pycrypto 
Installing collected packages: fabric, ssh, pycrypto 
    Running setup.py install for fabric 
    warning: no previously-included files matching '*' found under directory 'docs/_build' 
    warning: no files found matching 'fabfile.py' 
    Installing fab script to /home/hbrown/.virtualenvs/fabric-test/bin 
    Running setup.py install for ssh 
    Running setup.py install for pycrypto 
... 
Successfully installed fabric ssh pycrypto 
Cleaning up... 

這主要是化妝品,但是:SSH是的paramiko的一個分支,這兩個的維護者是相同的(傑夫Forcier,面料也是筆者),和the maintainer has plans to reunite paramiko and ssh under the name paramiko 。 (通過pbanka該修正。)

+0

由於這似乎是一個有趣的鏈接,我想更新它,因爲你的網站現在已經壞了。請使用:http://www.clemesha.org/blog/modern-python-hacker-tools-virtualenv-fabric-pip/ – dlewin 2012-02-15 15:00:28

+0

謝謝。修復了404鏈接。 – hughdbrown 2012-02-15 16:22:08

+0

提問者是否指定他不想使用「大型外部庫」?當作者真正要求的是一個簡單的一次性ssh配方時,Paramiko和Fabric都是矯枉過正的。 – 2012-08-15 14:18:50

22

如果你想避免任何額外的模塊,你可以使用子模塊運行

ssh [host] [command] 

並捕獲輸出。

嘗試類似:

process = subprocess.Popen("ssh example.com ls", shell=True, 
    stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 
output,stderr = process.communicate() 
status = process.poll() 
print output 

爲了應對用戶名和密碼,你可以用子與SSH流程交互,或者你可以在服務器上安裝一個公共密鑰以避免密碼提示。

+5

但是如果客戶端在Windows上呢? – Nathan 2010-07-08 14:05:33

+0

可能很難通過管道爲'ssh'子進程提供密碼。請參閱[爲什麼不使用管道(popen())?](http://pexpect.readthedocs.org/en/latest/FAQ.html#whynotpipe)。你可能需要'pty','pexpect'模塊來解決它。 – jfs 2014-02-19 11:58:31

+0

似乎不適用於字符串'ssh somecomputer; python -c「import numpy; print numpy .__ version__」'它說它不知道命令「import」 – usethedeathstar 2014-04-18 07:50:29

6

和hughdbrown一樣,我喜歡Fabric。請注意,雖然它實現了自己的聲明性腳本(用於進行部署等),但它也可以作爲Python模塊導入,並在您的程序中使用,而無需編寫Fabric腳本。

面料有一個新的維護者,正在重寫過程中;這意味着您在網上找到的大多數教程都不適用於當前版本。而且,Google仍然會將舊的Fabric頁面顯示爲第一個結果。

對於最新的文檔,你可以檢查:http://docs.fabfile.org

+0

織物使用paramiko的分支http://pypi.python.org/pypi/ssh/所有ssh的東西。 – Damien 2012-02-03 18:15:04

16

我已經寫Python bindings for libssh2。 Libssh2是一個實現SSH2協議的客戶端庫。

import socket 
import libssh2 

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
sock.connect(('exmaple.com', 22)) 

session = libssh2.Session() 
session.startup(sock) 
session.userauth_password('john', '******') 

channel = session.channel() 
channel.execute('ls -l') 

print channel.read(1024) 
+1

看起來很低級。例如(你自己的例子),你必須明確地說你使用IPv4或IPv6(你不需要做OpenSSH命令行客戶端)。 此外,我沒有找到如何使它與ssh-agent一起工作。 – bortzmeyer 2011-10-13 13:24:06

+2

關於pylibssh2的好處是,它傳輸文件的方式比任何本地python如paramiko的ssh實現都快。 – Damien 2012-02-03 18:13:09

6

我發現的paramiko是有點太低級,和Fabric不是特別適合於用作圖書館,所以我把我的自己的庫調用spur使用的paramiko實現略微更好接口:

import spur 

shell = spur.SshShell(hostname="localhost", username="bob", password="password1") 
result = shell.run(["echo", "-n", "hello"]) 
print result.output # prints hello 

您也可以選擇打印,因爲它的運行程序,如果你想看到長期運行的命令的輸出在退出前,這是有用的輸出:

result = shell.run(["echo", "-n", "hello"], stdout=sys.stdout) 
+0

不支持運行非標準命令,例如在某些路由器(MikroTik)命令前加'/'前綴,該庫會引發錯誤。對於標準的Linux主機,它看起來不錯。 – 2014-07-31 03:33:58

+0

當我將IP地址傳遞給主機名時,它會拋出一個錯誤,指出在known_hosts中找不到IP ... – rexbelia 2016-11-28 19:58:14

+0

@rexbelia這是SSH的正常行爲:爲了確保您正在與正確的服務器交談,SSH只會接受來自主機的密鑰,如果它已知的話。解決方案是將相關密鑰添加到known_hosts,或將missing_host_key參數設置爲適當的值,如文檔中所述。 – 2016-12-01 22:37:50

1

這爲我工作

import subprocess 
import sys 
HOST="IP" 
COMMAND="ifconfig" 

def passwordless_ssh(HOST): 
     ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND], 
         shell=False, 
         stdout=subprocess.PIPE, 
         stderr=subprocess.PIPE) 
     result = ssh.stdout.readlines() 
     if result == []: 
       error = ssh.stderr.readlines() 
       print >>sys.stderr, "ERROR: %s" % error 
       return "error" 
     else: 
       return result 
0

只需使用ssh_decorate

from ssh_decorate import ssh_connect 
ssh=ssh_connect('user','password','server') 

#Run a python function 
@ssh 
def python_pwd(): 
    import os 
    return os.getcwd() 

print (python_pwd()) 
相關問題