zoukankan      html  css  js  c++  java
  • SFTP in Python: Really Simple SSH

    Post SFTP in Python: Really Simple SSH | The best of Zeth

    SFTP in Python: Really Simple SSH

    ssh.py 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 ssh
    s = ssh.Connection('example.com')
    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 code-page, 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 ssh-keys, then all you need to do is:

    s = ssh.Connection('example.com')
    

    Connection supports the following options:

    hostThe Hostname of the remote machine.
    usernameYour username at the remote machine.
    private_keyYour private key file.
    passwordYour password at the remote machine.
    portThe 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 = 'example.com', 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('example.com', '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 Python by zeth. Bookmark the permalink.

    22 thoughts on “SFTP in Python: Really Simple SSH

  • 相关阅读:
    service bound()
    使用StructureMap扩展ASP.NET MVC三层结构框架系列文章总结篇(附源码下载)
    微软编程之美的资格赛,这么简单的题目害我编了一个多小时——传话游戏
    Yii framwork crud 命名范围NAMED SCOPE 笔记四
    C/C++中的序点
    使用Ruby On Rails15分钟打造一个博客系统
    paip.提升性能---.net listbox 使用BeginUpdate与EndUpdate
    大话设计模式总结4—总图和一些经典定义
    TestNG 6 发布
    NetBeans 时事通讯(刊号 # 140 Mar 22, 2011)
  • 原文地址:https://www.cnblogs.com/lexus/p/2355202.html
Copyright © 2011-2022 走看看