is it possible to run the following steps via python?

my target is to automated sftp by python - login to (Linux machine red hat 5.3) and get from this Linux the file.txt , please advice?

  Password: password123
  cd /var/tmp
  get  file.txt 
  sftp> quit

  the full view from sftp command line

  Connecting to 
  Password: password123
  sftp> cd /var/tmp
  sftp> get  file.txt 
  Fetching /var/tmp/file.txt to info.txt
  /var/tmp/file.txt                                                                                 100%  340     0.3KB/s   00:00    
  sftp> quit

Fabric is great to do this work.

  1. install fabic, refs: http://fabfile.org/

    pip install fabric
  2. create a fabfile.py as below:

    from fabric.api import * 
    from fabric.operations import put 
    from fabric.operations import get
    env.user = "username"
    env.password = "password123"
    def do():
       put(local_path="./temp/file.txt", remote_path="/tmp/file.txt")
       get(remote_path="/tmp/file.txt", local_path="./temp/file.txt")
  3. run with

     fab do

Should work.:)


Check out the Paramiko library for Python. I've used it for SSH, but I know it does have an SFTP object implementation as well.



check out pypi repository. I suggest you to use http://pypi.python.org/pypi/ssh/1.7.9. It used in the fabric

  • The 'ssh' library on pypi is actually the latest version of Paramiko (renamed/forked). – msanders Apr 5 '12 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.