博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SFTP in Python: Really Simple SSH
阅读量:7053 次
发布时间:2019-06-28

本文共 3635 字,大约阅读时间需要 12 分钟。

provides three common SSH operations, get, put and execute. It is a high-level abstraction upon Paramiko.

I wrote it yesterday for my own needs, so it is still very much in the beta stage. Any improvements or comments gratefully accepted.

In short, it works as follows:

import sshs = ssh.Connection('')s.put('hello.txt')s.get('goodbye.txt')s.execute('du -h --max-depth=0')s.close()

That is it, in the rest of this post, I walk through this line by line.

Installation

First, we need to install paramiko, if you don't have it already.

On Gentoo Linux:

emerge paramiko

On Ubuntu/Debian and so on:

apt-get install python-paramiko

If you want to use Python's easy_install then:

easy_install paramiko

Secondly, you need to grab the ssh.py module, grab it from my , and save it as ssh.py.

Connecting to a remote server

To play with the script interactively, you need to start Python:

python

Now, import the ssh module:

import ssh

Next we need to initiate the connection. If your username is the same on both systems, and you have set up , then all you need to do is:

s = ssh.Connection('')

Connection supports the following options:

host The Hostname of the remote machine.
username Your username at the remote machine.
private_key Your private key file.
password Your password at the remote machine.
port The SSH port of the remote machine.

The host is essential of course. Port defaults to 22. The username defaults to the username you are currently using on the local machine.

You need to use one of the authentication methods, a private key or a password. If you don't specify anything, then ssh.Connection will attempt to use a private_key at ~/.ssh/id_rsa or ~/.ssh/id_dsa.

So to specify a username and password, you can do it like this:

s = ssh.Connection(host = '', username = 'warrior', password = 'lennalenna')

Of course, Python also allows you to use the order to specify the arguments, so the last example can be written as:

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

Operations

Once you have set up the connection, there are three methods you can use. Firstly, to send a file from the local machine, you can use put:

s.put('hello.txt')

The above example copies a file called hello.txt from the current local working directory to the remote server. We can also be more explicit if we want:

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

So the above example copies /home/warrior/hello.txt on the local server to /home/zombie/textfiles/report.txt on the remote server.

The second operation works in a similar way but in reverse:

s.get('hello.txt')

get takes the file from the remote server to the local server, again we can be more explicit if we want:

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

The above example copies the strange.log from the server and saves it as serverlog.txt.

The last operation is execute, this executes a command on the remote server:

s.execute('ls -l')

This returns the output as a Python list.

Closing the connection

You can do as many operations you like while the connection is open, but when you are finished, you need to close the connection between the local and remote machines. You do this with the close method:

s.close()

There we go, that is all I needed to do with SSH. Please do let me know using the comments below if you have any problems using it.

If you import my module in your program and later find that you need more power or flexibility, you should be able to swap it out for the full paramiko with a minimum of fuss.

This entry was posted in by . Bookmark the .

22 thoughts on “SFTP in Python: Really Simple SSH”

转载地址:http://jxlol.baihongyu.com/

你可能感兴趣的文章
微信公众平台完整开发教程【转】
查看>>
初学JDBC,最简单示例
查看>>
strftime 日期时间格式化
查看>>
exe4j的使用
查看>>
Java单链表、双端链表、有序链表实现
查看>>
hadoop配置
查看>>
031 分布式中,zookeeper的部署
查看>>
persits.jpeg 水印组件
查看>>
Android IntentService完全解析 当Service遇到Handler
查看>>
单例模式
查看>>
Android资源(图片)命名规范
查看>>
java 大文件上传 断点续传 完整版实例 (Socket、IO流)
查看>>
LeetCode: Merge Two Sorted Lists 解题报告
查看>>
海报:Silverlight 1.1
查看>>
[cpp] I/O操作符号返回数值问题
查看>>
你有哪些用计算机技能解决生活问题的经历?
查看>>
SpringMVC 拦截器实现分析
查看>>
從此不再談jquery,马上忘掉他开始学Mootools
查看>>
android悬浮窗语音识别demo
查看>>
Vue -- Mixin
查看>>