paramiko sftp erro denied - python

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?

Related

Copying a file from a server

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.

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.

python ftputil strange behaviour

I have a code that looks like:
host = ftputil.FTPHost("ftp.example.com","anonymous","")
try:
os.mkdir('./ftp.example.com')
except:
traceback.print_exc()
else:
sys.stderr.write('WTF?')
for root,dirs,files in host.walk('/Path/to/a/lot/of/census/data/files/',topdown=True,onerror=None):
for filename in files:
fullpath = os.path.join(root,filename)
#print fullpath,host.lstat(fullpath)[6],int(host.lstat(fullpath)[8])
try:
host.download_if_newer(fullpath,'./ftp.example.com/'+filename,callback=None)
except:
traceback.print_exc()
After a while, exceptions like below starts flooding terminal screen:
*cmd* 'PWD'
*resp* '257 "/Path/to/a/lot/of/census/data/files/xx"'
*cmd* u'CWD /Path/to/a/lot/of/census/data/files/xx'
*resp* '257 "/Path/to/a/lot/of/census/data/files/xx"'
*cmd* u'TYPE I'
*resp* '250 Directory successfully changed.'
*cmd* 'PASV'
*resp* '200 Switching to Binary mode.'
Traceback (most recent call last):
File "bdpublicdata.py", line 56, in <module>
host.download_if_newer(fullpath,'./ftp.example.com/'+filename,callback=None)
File "/usr/local/lib/python2.7/dist-packages/ftputil/host.py", line 521, in download_if_newer
callback=callback)
File "/usr/local/lib/python2.7/dist-packages/ftputil/file_transfer.py", line 176, in copy_file
source_fobj = source_file.fobj()
File "/usr/local/lib/python2.7/dist-packages/ftputil/file_transfer.py", line 91, in fobj
return self._host.open(self.name, self.mode)
File "/usr/local/lib/python2.7/dist-packages/ftputil/host.py", line 204, in open
encoding=encoding, errors=errors, newline=newline)
File "/usr/local/lib/python2.7/dist-packages/ftputil/file.py", line 194, in _open
self._conn = self._session.transfercmd(command)
File "/usr/local/lib/python2.7/dist-packages/ftputil/error.py", line 168, in __exit__
raise FTPIOError(*exc_value.args)
FTPIOError: 200 Switching to Binary mode.
Debugging info: ftputil 3.1, Python 2.7.6 (linux2)
*cmd* 'PWD'
*resp* '227 Entering Passive Mode (126,218,41,23,59,181)'
Traceback (most recent call last):
File "bdpublicdata.py", line 56, in <module>
host.download_if_newer(fullpath,'./ftp.example.com/'+filename,callback=None)
File "/usr/local/lib/python2.7/dist-packages/ftputil/host.py", line 521, in download_if_newer
callback=callback)
File "/usr/local/lib/python2.7/dist-packages/ftputil/file_transfer.py", line 176, in copy_file
source_fobj = source_file.fobj()
File "/usr/local/lib/python2.7/dist-packages/ftputil/file_transfer.py", line 91, in fobj
return self._host.open(self.name, self.mode)
File "/usr/local/lib/python2.7/dist-packages/ftputil/host.py", line 181, in open
host = self._available_child()
File "/usr/local/lib/python2.7/dist-packages/ftputil/host.py", line 159, in _available_child
host._session.pwd()
File "/usr/lib/python2.7/ftplib.py", line 587, in pwd
return parse257(resp)
File "/usr/lib/python2.7/ftplib.py", line 855, in parse257
raise error_reply, resp
error_reply: 227 Entering Passive Mode (126,218,41,23,59,181)
And I can never retrieve the whole directory tree from host.
Any thoughts?

Categories

Resources