Copying a file from a server - python

I am currently trying to copy a file from a server to a local file. Below is my current code. I am currently generating the following error, [Errno 13] Permission denied. I believe this is associated with the remote path. Any ideas?
import os
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='de.ac.uk', username="hfseb", password="kjsvbkjcb")
sftp = ssh.open_sftp()
localpath = '/Users/abc/def'
remotepath = '/home/abc/def/game.log'
sftp.put(localpath, remotepath)
sftp.close()
ssh.close()
Error generated
Traceback (most recent call last):
File "C:\Users\abc\python\ssh1.py", line 15, in
sftp.put(localpath, remotepath)
File "C:\Users\abc\python\paramiko\sftp_client.py", line 759, in put
return self.putfo(fl, remotepath, file_size, callback, confirm)
File "C:\Users\abc\python\paramiko\sftp_client.py", line 714, in putfo
with self.file(remotepath, "wb") as fr:
File "C:\Users\abc\python\paramiko\sftp_client.py", line 372, in open
t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
File "C:\Users\abc\python\paramiko\sftp_client.py", line 813, in _request
return self._read_response(num)
File "C:\Users\abc\python\paramiko\sftp_client.py", line 865, in _read_response
self._convert_status(msg)
File "C:\Users\abc\python\paramiko\sftp_client.py", line 896, in _convert_status
raise IOError(errno.EACCES, text)

Probably the remote def directory have wrong permissions.

Related

paramiko sftp erro denied

I got this code, it encrypt the file passwords.txt and send to my kali linux machine:
...snippet....
host = '192.168.15.18'
user = 'kali'
passwd = 'kali'
path = '/home/kali/password'
# sending Descrypt
#creating object SSHClient
try:
ssh = paramiko.SSHClient()
#credentials
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
socket.gethostbyname(host)
ssh.connect(hostname=host, username=user, password=passwd)
pass
except:
print("Fail.")
exit()
#Criando um canal de conexão
sftp = ssh.open_sftp()
print('Sending passowrd and decryptor.py')
#Sending password e decrpyter to host
sftp.put("decryptor.py", f"{path}/decryptor.py")
sftp.put("password_crypto.txt", f"{path}/password_crypto.txt")
#closing
sftp.close()
But i got this error:
Traceback (most recent call last):
File "C:\Users\User\Desktop\password security crypter\crypter.py", line 114, in <module>
sftp.put("decryptor.py", f"{path}/decryptor.py")
File "C:\Program Files\Python38\lib\site-packages\paramiko\sftp_client.py", line 759, in put
return self.putfo(fl, remotepath, file_size, callback, confirm)
File "C:\Program Files\Python38\lib\site-packages\paramiko\sftp_client.py", line 714, in putfo
with self.file(remotepath, "wb") as fr:
File "C:\Program Files\Python38\lib\site-packages\paramiko\sftp_client.py", line 372, in open
t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
File "C:\Program Files\Python38\lib\site-packages\paramiko\sftp_client.py", line 822, in _request
return self._read_response(num)
File "C:\Program Files\Python38\lib\site-packages\paramiko\sftp_client.py", line 874, in _read_response
self._convert_status(msg)
File "C:\Program Files\Python38\lib\site-packages\paramiko\sftp_client.py", line 905, in _convert_status
raise IOError(errno.EACCES, text)
PermissionError: [Errno 13] Permission denied
My code was working 1 month ago. The encrpyt and desccrypt works, but it not sending to my host...
Someone knows what can i do?

permission errors while using pysftp and paramiko

I have a use case to transfer file over sftp, planning to use paramiko or pysftp modules of python3.7 and having troubles with one of the destinations/targets. File transfer works fine when winSCP is used. Any suggestions to resolve this issue or what could be the specific reason for this target.
Approach#1: using paramiko and open_sftp
import paramiko
import os
ssh_client =paramiko.SSHClient()
ssh_client.load_system_host_keys()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='abc.com', username='username', password='pwd')
sftp_client=ssh_client.open_sftp()
print(sftp_client.stat(path='/receive/file))
sftp_client.file("createfile","w+")
print(sftp_client.listdir(path='/receive/file'))
Approach#1 - Error
drwxrwxrwx 1 0 0 0 12 Mar 13:18 ?
Traceback (most recent call last):
File "sftp_linux.py", line 32, in <module>
sftp_client.file("createfile","w+")
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 372, in open
t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 813, in _request
return self._read_response(num)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 865, in _read_response
self._convert_status(msg)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 896, in _convert_status
raise IOError(errno.EACCES, text)
PermissionError: [Errno 13] Can not create createfile
Approach#2: using pysftp
import os
import sys
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
sftp_client = pysftp.Connection(host='abc.com', username='username', password='pwd', cnopts=cnopts,port=22)
print(sftp_client)
sftp_client.chdir ('/receive/file')
print(sftp_client.pwd)
print(sftp_client.listdir())
sftp_client.put('testingfile',preserve_mtime=True)
sftp_client.close()
Approach#2: using pysftp - error
[]
Traceback (most recent call last):
File "sftp_linux.py", line 15, in <module>
sftp_client.put('testingfile',preserve_mtime=True)
File "/usr/local/lib/python3.7/site-packages/pysftp/__init__.py", line 364, in put
confirm=confirm)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 759, in put
return self.putfo(fl, remotepath, file_size, callback, confirm)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 720, in putfo
s = self.stat(remotepath)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 493, in stat
t, msg = self._request(CMD_STAT, path)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 813, in _request
return self._read_response(num)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 865, in _read_response
self._convert_status(msg)
File "/usr/local/lib/python3.7/site-packages/paramiko/sftp_client.py", line 894, in _convert_status
raise IOError(errno.ENOENT, text)
FileNotFoundError: [Errno 2] /receive/file/testingfile

Unable to download file with pysftp get

I am able to connect the sftp with pysftp successfully, but getting error while downloading file as FileNotFoundError: [Errno 2] No such file. I also observed that the file is just creating at local path adding '?' along with extension. Below are more details.
File at ftp as Test_03132018080105.csv.
File creating at Local path as Test_03132018080105.csv? with zero bytes
Code:
def get_move_on_ftp(ftpsource,localsource):
if (os.stat(envvar.validfiles).st_size == 0) and (os.stat(envvar.invalidfiles).st_size == 0):
print("There are no Source files on FTP.")
else:
srcinfo={'host':envvar.src_ftphost,'port':envvar.src_ftpport,'username':envvar.src_uname,'password':envvar.src_passwd}
sftp = pysftp.Connection(**srcinfo)
sftp.cwd(ftpsource)
''' Downloading Files '''
avail_files=open(envvar.validfiles,'r')
for filename in avail_files:
print(sftp.getcwd())
#sftp.get(filename, preserve_mtime=True)
print(filename) # for debug
sftp.get(filename)
sftp.close()
Error:
Traceback (most recent call last):
File "my.py", line 96, in <module>
main()
File "my.py", line 92, in main
config_file_read(config_file)
File "my.py", line 85, in config_file_read
get_move_on_ftp(ftpsource,localsource)
File "my.py", line 61, in get_move_on_ftp
sftp.get(filename)
File "/home/username/miniconda3/lib/python3.6/site-packages/pysftp/__init__.py", line 249, in get
self._sftp.get(remotepath, localpath, callback=callback)
File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 770, in get
size = self.getfo(remotepath, fl, callback)
File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 746, in getfo
file_size = self.stat(remotepath).st_size
File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 460, in stat
t, msg = self._request(CMD_STAT, path)
File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 780, in _request
return self._read_response(num)
File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 832, in _read_response
self._convert_status(msg)
File "/home/username/miniconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 861, in _convert_status
raise IOError(errno.ENOENT, text)
FileNotFoundError: [Errno 2] No such file
Just add "/" in front of ftpsource. i.e. "/2020/Jan/10/". It should work

FTP not running on PC

I was trying this FTP file transfer code. The thing is that when I had run this code on an online environment(a workspace that I created on Cloud 9) then it worked fine and uploaded the file but when I run this on my PC I get an ERROR. How can I resolve it?
from ftplib import FTP
ftp=FTP('**domain**')
ftp.login(user='username',passwd='password')
ftp.cwd('/')
def grabFile():
filename='fileName.txt'
localfile=open(filename, 'wb')
ftp.retrbinary('RETR '+filename, localfile.write, 1024)
ftp.quit()
localfile.close()
def placeFile():
filename= 'Strinzy.txt'
ftp.storbinary('STOR '+filename, open(filename,'rb'))
ftp.quit()
placeFile()
ERROR:
Traceback (most recent call last):
File "ftp_trial.py", line 19, in <module>
placeFile()
File "ftp_trial.py", line 16, in placeFile
ftp.storbinary('STOR '+filename, open(filename,'rb'))
File "/usr/lib/python2.7/ftplib.py", line 468, in storbinary
conn = self.transfercmd(cmd, rest)
File "/usr/lib/python2.7/ftplib.py", line 373, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "/usr/lib/python2.7/ftplib.py", line 332, in ntransfercmd
conn = socket.create_connection((host, port), self.timeout)
File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 113] No route to host
If you are using vsftpd then make sure your vsftpd service is running in your machine and /etc/vsftpd.userlist has username in it.

Download a file from ftp using ftplib

I am looking for a specific file within a folder on FTP. There are several folders and my code loops through each folder to find the file. Once, I find the file I would like to download it.
import ftplib
import os, sys, os.path
ftp = ftplib.FTP("xxxxxxxx")
ftp.login("dbaxyz", "password123")
files = []
try:
files = ftp.nlst()
except ftplib.error_perm, resp:
if str(resp) == "550 No files found":
print "No files in this directory"
else:
raise
for f in files:
filess = [],
filess=ftp.nlst(f)
for i in filess:
if len(i)==34 and i[8:11]=="ACO" and i[12:17]=="CCLF9" and i[3:7]=="1263" :
print i
local_filename = os.path.join('C:\\Users\\zubair.zahiruddin\\Desktop\\test', i)
file = open(local_filename, 'wb')
ftp.retrbinary('RETR '+ i, file.write)
file.close()
ftp.close()
#ftp.quit()
However, I get an error: perm file not found.
Traceback (most recent call last):
File "ftp.py", line 28, in <module>
ftp.retrbinary('RETR '+ i, file.write)
File "C:\Python27\lib\ftplib.py", line 414, in retrbinary
conn = self.transfercmd(cmd, rest)
File "C:\Python27\lib\ftplib.py", line 376, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python27\lib\ftplib.py", line 339, in ntransfercmd
resp = self.sendcmd(cmd)
File "C:\Python27\lib\ftplib.py", line 249, in sendcmd
return self.getresp()
File "C:\Python27\lib\ftplib.py", line 224, in getresp
raise error_perm, resp
ftplib.error_perm: 550 The system cannot find the file specified.
PS C:\Users\zubair.zahiruddin\Desktop\test>

Categories

Resources