Python moving file on SFTP server to another folder [duplicate] - python

This question already has answers here:
Rename file on remote server on Python
(2 answers)
Closed 2 years ago.
I wrote this script to save a file from an SFTP remote folder to a local folder. It then removes the file from the SFTP. I want to change it so it stops removing files and instead saves them to a backup folder on the SFTP. How do I do that in pysftp? I cant find any documentation regarding it...
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
myHostname = "123"
myUsername = "456"
myPassword = "789"
with pysftp.Connection(host=myHostname, username=myUsername, password="789", cnopts=cnopts) as sftp:
sftp.cwd("/Production/In/")
directory_structure = sftp.listdir_attr()
local_dir= "D:/"
remote_dir = "/Production/"
remote_backup_dir = "/Production/Backup/"
for attr in directory_structure:
if attr.filename.endswith(".xml"):
file = attr.filename
sftp.get(remote_dir + file, local_dir + file)
print("Moved " + file + " to " + local_dir)
sftp.remove(remote_dir + file)
Don't worry about my no hostkey or the password in plain. I'm not keeping it that way once i get the script working :)

Use Connection.rename:
sftp.rename(remote_dir + file, remote_backup_dir + file)
Obligatory warnings:
Do not set cnopts.hostkeys = None, unless you do not care about security. For the correct solution see Verify host key with pysftp.
Do not use pysftp. It's dead. Use Paramiko. See pysftp vs. Paramiko.

Related

Download files from SFTP server to specific local directory using Python pysftp

I'm attempting to download a set of files from an SFTP server. I can download the files successfully, but now I want to change the script so that as the files download, they go to a specified directory on my local server.
I attempted doing this with the sftp.getfo() command, but am getting an error:
getfo() got an unexpected keyword argument 'fl'
#SET LOCAL DIRECTORY
directory1 = r'C:\Users\Me\Documents\test sftp'
#CONNECT TO SFTP
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
sftp = pysftp.Connection(host = hostname, username = usereu, password = passeu, cnopts = cnopts)
print("Connection successfully established ... ")
#DOWNLOAD ALL FILES IN FOLDER
files = sftp.listdir()
for file in files:
sftp.getfo(remotepath = file, fl = directory1)
print(file,' downloaded successfully ')
Am I using the right command here or is there a better way to do this?
Thanks!
To download a file to local path, use Connection.get (not getfo):
sftp.get(remotepath=file, localpath=os.path.join(directory1, file))
Some notes:
remotepath takes path to the remote file, not just filename. Your code (which passes filename only) works only because you are downloading from the home folder.
The pysftp is dead. Do not use it. Use Paramiko. See pysftp vs. Paramiko
For an example, see Download file from SFTP server in Python Paramiko
Do not set cnopts.hostkeys = None, unless you do not care about security. For the correct solution see Verify host key with pysftp.

sFTP with SSH Key & Password in Python [duplicate]

This question already has answers here:
Multi-factor authentication (password and key) with Paramiko
(3 answers)
Closed last year.
Trying to use pysftp to pull files from an sFTP server that requires both ssh key & password for authentication without much luck. Can use pysftp with just key and just password, but it breaks when I attempt to use both. Hoping someone has some experience with this. Open to using a different library if that works better.
Error output is:
paramiko.ssh_exception.BadAuthenticationType: Bad authentication type; allowed types: ['publickey']
import pysftp
connection_host = '1.1.1.1'
connection_user = 'username'
connection_password = 'password'
connection_private_key = '/path/to/key'
connection_dir='/dir/on/remote/host'
with pysftp.Connection(host=connection_host, username=connection_user, password=connection_password, private_key=connection_private_key) as sftp:
files = sftp.listdir(remotepath=connection_dir)
for file in files:
print("found the following file: {}".format(file))
with sftp.cd(connection_dir):
sftp.get(file)
Working code if anyone runs into the same issue:
connection_host = '0.0.0.0'
connection_user = 'username'
connection_password = 'password'
connection_private_key = '/path/to/key.pem'
connection_dir='/remote/path'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
ssh.connect(connection_host, username=connection_user, password=connection_password, key_filename=connection_private_key)
sftp_client = ssh.open_sftp()
files = sftp_client.listdir(connection_dir)
sftp_client.chdir(connection_dir)
for file in files:
print("found the following file {}".format(file))
sftp_client.get(file,file)
if sftp_client:
sftp_client.close()
if ssh:
ssh.close()

Download the latest file according to timestamp in file name from SFTP server

I'm trying to get latest new file in a directory of remote Linux server. The file in SFTP server is created every 4 hours and the file have specific name start with filegen_date_hour.json as per example below. In this case latest file 'filegen_20200101_0800.json' need to be transferred to my local directory.
filegen_20200101_0000.json
filegen_20200101_0400.json
filegen_20200101_0800.json
I use Python 3 code below, but got error
latestFile = max(listFile, key=os.path.getctime)
ValueError: max() arg is an empty sequence
SFTP code below
myHostname = "192.168.100.10"
myUsername = "user"
myPassword = "password"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
with sftp.cd('/home/operation/genfiles/'):
fileDir = '/home/operation/genfiles/filegen_*.json'
**#file have specific pattern with filegen_*.json**
listFile = glob.glob(fileDir)
latestFile = max(listFile, key=os.path.getctime)
sftp.get(latestFile)
Appreciate help on this matter. Thank you for your response and help.
First, you cannot use glob to list files on an SFTP server. The glob won't magically start querying SFTP server only because you have opened an SFTP connection before. It will still query local file system.
Use pysftp Connection.listdir. Though it does not support wildcards, so you will have to filter the files you want locally. Like here:
List files on SFTP server matching wildcard in Python using Paramiko
Only then you can try finding the latest file.
In general, you may use file modification time, as here:
How to download only the latest file from SFTP server with Paramiko?
The code is for Paramiko SFTPClient.listdir_attr, but it's the same with pysftp Connection.listdir_attr.
But in your case, I'm not sure if you can rely on the modification timestamp. It seems that you actually want to use the timestamp in the filename. With your file name format, you can simply pick the last file lexicographically.
import fnmatch
...
with sftp.cd('/home/operation/genfiles'):
files = []
for filename in sftp.listdir():
if fnmatch.fnmatch(filename, "filegen_*.json"):
files.append(filename)
latestFile = max(files)
Obligatory warning: Do not set cnopts.hostkeys = None, unless you do not care about security. For the correct solution see Verify host key with pysftp.

unable to download files from sftp server using python

i am writing a python script that helps me download files from sftp server to my local folder.
when i run the script it just downloads the blank document
i keep on trying but i am failing
i made a sftp server to test the code and the path specified in remote path is server root directory
i giving code below
import pysftp
myHostname = "192.168.56.1"
myUsername = "new45"
myPassword = "146515"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword,cnopts=cnopts) as sftp:
# Define the file that you want to download from the remote directory
remoteFilePath = 'C:\\Users\\Simon\\Desktop\\ftp\\testfile.txt'
# Define the local path where the file will be saved
# or absolute "C:\Users\sdkca\Desktop\TUTORIAL.txt"
localFilePath = 'C:\\Users\\Simon\\Desktop\\ftp2\\textfile.txt'
sftp.get(remoteFilePath, localFilePath)
# connection closed automatically at the end of the with-block'''
please tell me whats the error why is blank file is being downloaded
Login to that server using any GUI SFTP client. And observe what path syntax your server is using. It's definitely not C:\Users\Simon\Desktop\ftp\testfile.txt. SFTP uses forward slashes. So it can be like /C:/Users/Simon/Desktop/ftp/testfile.txt. But even something completely different, particularly if the server or your account are chrooted.
You are probably trying to use the path syntax, which you would have used, were you directly on that target Windows system. But your SFTP server presents the files using a different path syntax. You need to adhere to that syntax.

Python script to upload a file to a remote server

I am working on a project that requires us to upload a vile via SFTP to a remote server, and we are having troubles doing this. We tried following this youtube guide, but we are having some issues.
We are getting a "no such file" error when we run the script, and we know for sure that the file exists and that we have given the python script the right name and location for the file.
This is the script as we have it right now:
import pysftp as sftp
def sftpTry():
try:
s = sftp.Connection(host='babbage.cs.missouri.edu', username ='<username>', password = '<password>')
remotepath = '~it3001s14grp1/videos/newVideo/new.avi'
#localpath = '/etc/motion/capture/hello.txt'
localpath = '/etc/motion/capture/06--2014-05-15---16-16-25.avi'
s.put(localpath, remotepath)
s.close()
except Exception, e:
print str(e)
sftpTry();
You should begin your remote path with a forward slash "/". Also, check the directory you are specifying in the remotepath. You should try to do a pwd in the directory when you login into the server (say using ssh). The remote-path should be specified exactly like that.
Although you do have the filename name in the remote path, it would throw an error if you specify just the folder's name.
Another tip would be to use getpass instead of hard-coding the password:
passwd = getpass.getpass()
s = sftp.Connection(host='<host>', username = '<username>', password = passwd)

Categories

Resources