python ssh库_用于ssh处理的python库

I'm going to write first code for handling ssh commands on python and I did search over the stackoverflow and can see that there are several python libraries that can be used for handling commands passed through ssh, like paramiko, pexpect and perhaps some others.

Particularly, I will need to read content of the files from the remote server, copy files through ssh/scp, get output from remote server after starting the script on remote server.

Perhaps some experts could advice what library is better and specify advantages or disadvantages?

解决方案

Libraries, Wrappers:

#!/usr/bin/env python

import paramiko

from contextlib import contextmanager

host = '192.168.10.142'

username = 'slacker'

password = 'password'

def create_ssh(host=host, username=username, password=password):

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:

print "creating connection"

ssh.connect(host, username=username, password=password)

print "connected"

yield ssh

finally:

print "closing connection"

ssh.close()

print "closed"

1) utilizes the 2) and provides some higher level functions. If the latter suit your requirements, I'd suggest trying out 1)

Update: 1) is gone now (2017-09-12)

s = ssh.Connection('example.com', 'warrior', password = 'lennalenna')

s.put('/home/warrior/hello.txt', '/home/zombie/textfiles/report.txt')

s.get('/var/log/strange.log', '/home/warrior/serverlog.txt')

s.execute('ls -l')

s.close()

Note: The code examples above are provided just for getting an impression; the code is not tested.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值