简体   繁体   中英

Batch SFTP commands with Python and Paramiko

I am porting some batch jobs from Window to Linux. These are.bat scripts on Windows and I am rewriting them in Python to run on Linux

On Windows, we currently use putty to SFTP files and run commands like this:

psftp user@host -i privatekey.ppk -batch -b script.txt

This executes the putty SFTP commands found in script.txt in batch. Example below:

SCRIPT.TXT example

 cd mydir
 lcd outbox
 get myremotefile.txt mylocalfile.txt
 get myotherremotefile.txt myotherlocalfile.txt
 bye

I am trying to find a way to use the same batch script (script.txt) in my python script to minimise the amount of changes overall. I've been looking into paramiko but so far I have not been able to find a way to execute the sftp commands in bulk via a script file.

An alternative would be to spawn a sub-process to execute the sftp command with the -b option but would prefer a python native solution if possible.

What do you think? Are there other options out there to solve this?

The commands in you script are psftp commands. Not "SFTP commands" (there are no SFTP commands). In general, no other application nor library understands them. Except for OpenSSH sftp , as PuTTY psftp was made to be somewhat compatible.

If you want to use a native Python SFTP library, like Paramiko or pysftp, you will need to use its API. And that means rewriting your code. It's not difficult.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM