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?
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 have problem to download files from my private chat.
I've added a bot as administrator to my private chat. To upload on telegram with a python script I have no problems, for example I use successfully this code
import os
import telegram
# Enter your bot's token here
TOKEN = "58819**********************....."
# Enter your private chat ID here
CHAT_ID = -100****...
# Enter the path to the document here
DOCUMENT_PATH = "C:\\temp\\telegram\\snow.epub"
# Create a Bot object using the token
bot = telegram.Bot(token=TOKEN)
# Open the document in binary mode
with open(DOCUMENT_PATH, "rb") as f:
# Send the document to the specified chat
bot.send_document(chat_id=CHAT_ID, document=f)
print("Document sent successfully!")
Problem is download this file (an epub file) from my private chat
i try with this code but nothing happens, powershell returns me this message
PS C:\temp\telegram> python scaricare2.py
The last sent message is not a document.
I test with this code
import os
import os.path
import telegram
# Enter your bot's token here
TOKEN = "588199*****************..."
# Enter your private chat ID here
CHAT_ID = -1001******...
# Create a Bot object using the token
bot = telegram.Bot(token=TOKEN)
# Download the latest file sent in the chat
updates = bot.get_updates(offset=-1, limit=1)
if updates:
# Checks if the Message object contains a document attribute
if hasattr(updates[0].message, "document"):
file_id = updates[0].message.document.file_id
else:
print("The last sent message is not a document.")
exit()
file_info = bot.get_file(file_id)
file = bot.download_file(file_info.file_path)
# Create the path to the directory where to save the file
download_path = "C:/temp/telegram/filexx/"
# Create the full path to the file using the file name
file_path = os.path.join(download_path, file_info.file_name)
# Open the file in binary write mode
with open(file_path, "wb") as f:
# Write the contents of the file to the specified path
f.write(file)
print("File downloaded successfully!")
else:
print("The last sent message is not a document.")
Any idea to solve ?
Download from bot returns this error
PS C:\temp\telegram> python scaricare2.py
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 402, in _make_request
six.raise_from(e, None)
File "<string>", line 2, in raise_from
File "C:\Program Files\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 398, in _make_request
httplib_response = conn.getresponse()
File "C:\Program Files\Python310\lib\http\client.py", line 1374, in getresponse
response.begin()
File "C:\Program Files\Python310\lib\http\client.py", line 318, in begin
version, status, reason = self._read_status()
File "C:\Program Files\Python310\lib\http\client.py", line 279, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "C:\Program Files\Python310\lib\socket.py", line 705, in readinto
return self._sock.recv_into(b)
File "C:\Program Files\Python310\lib\ssl.py", line 1273, in recv_into
return self.read(nbytes, buffer)
File "C:\Program Files\Python310\lib\ssl.py", line 1129, in read
return self._sslobj.read(len, buffer)
TimeoutError: The read operation timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\site-packages\telegram\utils\request.py", line 259, in _request_wrapper
resp = self._con_pool.request(*args, **kwargs)
File "C:\Program Files\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\request.py", line 68, in request
return self.request_encode_body(method, url, fields=fields,
File "C:\Program Files\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\request.py", line 148, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "C:\Program Files\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\poolmanager.py", line 244, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "C:\Program Files\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 665, in urlopen
retries = retries.increment(method, url, error=e, _pool=self,
File "C:\Program Files\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\util\retry.py", line 347, in increment
raise six.reraise(type(error), error, _stacktrace)
File "C:\Program Files\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\packages\six.py", line 686, in reraise
raise value
File "C:\Program Files\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 614, in urlopen
httplib_response = self._make_request(conn, method, url,
File "C:\Program Files\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 404, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=read_timeout,
File "C:\Program Files\Python310\lib\site-packages\telegram\vendor\ptb_urllib3\urllib3\connectionpool.py", line 321, in _raise_timeout
raise exc_cls(*args)
telegram.vendor.ptb_urllib3.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='api.telegram.org', port=443): Read timed out. (read timeout=5.0)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\temp\telegram\scaricare2.py", line 24, in <module>
file_info = bot.get_file(file_id)
File "C:\Program Files\Python310\lib\site-packages\telegram\bot.py", line 134, in decorator
result = func(*args, **kwargs)
File "C:\Program Files\Python310\lib\site-packages\telegram\bot.py", line 2510, in get_file
result = self._post('getFile', data, timeout=timeout, api_kwargs=api_kwargs)
File "C:\Program Files\Python310\lib\site-packages\telegram\bot.py", line 299, in _post
return self.request.post(
File "C:\Program Files\Python310\lib\site-packages\telegram\utils\request.py", line 361, in post
result = self._request_wrapper(
File "C:\Program Files\Python310\lib\site-packages\telegram\utils\request.py", line 261, in _request_wrapper
raise TimedOut() from error
telegram.error.TimedOut: Timed out
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 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
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