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
Related
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?
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.
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
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.
I'm trying to access s3 files from the mrjob module.
Here's the code that's failing:
from mrjob.emr import S3Filesystem
fs = S3Filesystem("<aws_access_key_id>", "<aws_secret_access_key>", "us-west-2")
fs.get_s3_key("s3n://<bucket>/<folder>/file.txt")
(but with the actual aws_access_key_id, aws_secret_access_key, bucket, and folder) and the error I'm getting is:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/mrjob/fs/s3.py", line 235, in get_s3_key
bucket = s3_conn.get_bucket(bucket_name)
File "/usr/local/lib/python2.7/dist-packages/mrjob/retry.py", line 80, in call_and_maybe_retry
return f(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 431, in get_bucket
bucket.get_all_keys(headers, maxkeys=0)
File "/usr/local/lib/python2.7/dist-packages/boto/s3/bucket.py", line 375, in get_all_keys
'', headers, **params)
File "/usr/local/lib/python2.7/dist-packages/boto/s3/bucket.py", line 332, in _get_all
query_args=query_args)
File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 547, in make_request
retry_handler=retry_handler
File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 947, in make_request
retry_handler=retry_handler)
File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 908, in _mexe
raise e
gaierror: [Errno -2] Name or service not known
Any suggestions?
Thanks!
This (s3n://)
fs.get_s3_key("s3n://<bucket>/<folder>/file.txt")
should be this (s3://) :
fs.get_s3_key("s3://<bucket>/<folder>/file.txt")