How to configure pyftpdlib to create an 'active' only FTP server? - python

I need to create a test case for a FTP client that involves connecting to a server that only accepts 'active' FTP connections. For other cases I am using pyftpdlib, and it works like charm, but I can't see an easy way to configure it to behave just in FTP active mode, and not passive.
Thanks.

If by what you wrote in the title you mean "literaly disable PASV (passive)" mode you can just tell pyftpdlib to not interpret that command. Not tested:
from pyftpdlib.ftpserver import FTPHandler
handler = FTPHandler
del handler.proto_cmds['PASV']
del handler.proto_cmds['EPSV']
...
This way pyftpdlib will reject any PASV/EPSV request with "550 Command PASV not understood.".

Related

Uploading to an FTP server on the same wifi network (python)

I am trying to upload a file to an ftp server on my same wifi network to get a picture on to a digital picture frame. I succeeded in uploading through file explorer, but when uploading using a python script I get a 530 response.
Here is the code so far
import ftplib
ftp = ftplib.FTP()
ftp.connect("111.111.1.11", 1111) #dummy host and port
file = open('C:/path/to/file/test1.png','rb')
ftp.storbinary('test.png', file)
file.close()
ftp.quit()
The server does not requre me to log in with a username and password on file explorer, is there some sort of default I need?
530 error code means Authentication failed error so you are missing the authentication piece. Maybe you can do something like this:
ftp = FTP(source_address=("111.111.1.11", 1111))
ftp.login(user, password)
Note that if you don't provide a user and password it will login with:
user anonymous
password anonymous
as described here
Also I would recommend you reading about S-FTP (Secure FTP) because in FTP the credentials are passed in clear text in the login request.
S-FTP is a communication protocol similar to FTP but built on top of ssh.
Hope this helped you !

Downloading a file from a python ftp server to cisco ios device

At the moment, my networking team is downloading a firmware version using an FTP server (Filezilla).
We create an account on the server, upload the firmware to the server and then on the cisco device we use the command copy ftp://username:password#server_ip/file_name storage_device: and the device is downloading the firmware from the ftp server.
Now, i have a website created with Flask in python, that has some scripts that my team uses, and i want to add a script that allows the user to upload a file to the website, supply the ip of the device, and the script will connect to the device and pull the file from the server.
While creating an ftp server using pyftpdlib, i have encounterd some issues, as the device is logging in with the supplied user, but does not download the file.
If i log in to the server using an FTP client (FileZilla client) i can download the file separately.
I guess the issue is with the cisco device trying to download the firmware directly while connecting.
The code I used to create the python server:
import os
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
def main():
authorizer = DummyAuthorizer()
authorizer.add_user('username', 'password', '/ftp', perm='elradfmwMT')
handler = FTPHandler
handler.authorizer = authorizer
handler.banner = "Test FTP server"
address = ('0.0.0.0', 21)
server = FTPServer(address, handler)
server.max_cons = 256
server.max_cons_per_ip = 10
server.serve_forever()
if __name__ == '__main__':
main()
If anyone has encounterd any issue like that, help would be appreciated.
Thank you!
Two possible issues:
1. Watch the diskspace on the IOS device. Some platforms have very little space for extra images, so good image hygiene is essential.
2. In my various experiments, I've found that pushing the image to the device is generally more reliable than asking the device to pull the image from elsewhere. You may need to add a configuration entry "ip scp server enable". Then you can write your script to push the images diretly.

Python - ftplib.FTP_TLS Port

Can someone help me confirm the default port when using ftplib.FTP_TLS? We have opened port 990 and 21 but my script fails to connect.
import ftplib
session = ftplib.FTP_TLS('xxx.ftp.com','user','password')
file = open('Bkup.tar.gz','rb')
session.storbinary('STOR Bkup.tar.gz', file)
file.close()
session.quit()
Thank You!
You could try:
ftplib.FTP_TLS.port = 21
According to it's own documentation, as well as the spec, FTPS (or FTP over TLS) connects to port 21.
You appear to be missing a login clause to authenticate the session.
Try calling session.login() followed by session.prot_p() before attempting to store the binary.
This documentation can be found by using the help function or in the online documentation here.
I hope that helps.
Please don't modify the port directly. If you get a closer look at FTP_TLS super __init__ you can clearly see that if it's called with a host param connect gets automatically called with just the host as param so the port defaults to FTP_PORT which is 21.
A better approach would be to call FTP_TLS without any params and then manually call the connect method where you can specify host as well as port.
from ftplib import FTP_TLS
client = FTP_TLS()
client.connect(host="ftp.example.com", port=12121)
Oddly, the FTP_TLS object has no direct port parameter on the constructor like the FTP object does.
you can set the port prior to calling the constructor and that works.
ftplib.FTP_TLS.port=1234
ftplib.FTP_TLS( 'ftphost.domain.tld', 'user', 'password' )

pyxmpp2 connect to openfire cannot resolve NXDOMAIN

I installed pyxmpp2 https://github.com/Jajcus/pyxmpp2to my Ubuntu machine. I also installed Openfire 3.8.1 to it. I would like to use pyxmpp2 to connect to my Openfire server within the same machine.
In the Server -> Server Manager-> Server Information in my Openfire control panel, the Server Name showed in Server Properties in the panel was mymachine and the Host Name showed in the Environment section was MyMachine.
I tried the following code:
import logging
from pyxmpp2.jid import JID
from pyxmpp2.client import Client
logging.basicConfig()
client = Client(JID("admin#mymachine"),[])
client.connect()
and got the following message:
WARNING:pyxmpp2.resolver:Could not resolve '_xmpp-client._tcp.mymachine': NXDOMAIN
Did I miss configuring something?
It looks like there are no DNS SRV records for your domain and pyxmpp2 is therefore unable to resolve them. Have a look at http://wiki.xmpp.org/web/SRV_Records on how to create them.
Basically a DNS SRV record has the form
_service._proto.name TTL class SRV priority weight port target
which could look like this example
_xmpp-client._tcp.example.net. 86400 IN SRV 5 0 5222 example.net.
Maybe pyxmpp2 also provides a way to directly specify the host used for the XMPP service. This would avoid the DNS SRV lookup.
It may be using ipv6, you can force ipv4 using u"ipv4": True and specifing the server u"server": "chat.facebook.com"
handler = MyHandler(JID(target_jid), message)
settings = XMPPSettings({
u"ipv4": True,
u"server": "chat.facebook.com",
u"password": your_password,
u"starttls": True,
u"tls_verify_peer": False,
})
client = Client(JID(your_jid), [handler], settings)
client.connect()
client.run()
The full code is located on the pyxmpp2 examples folder send_message_client.py

SFTP using ftplib

I need to download a file from a host using SFTP.
Do you know if is it possible to do that using Python ftplib?
I saw an example here, but when I try to connect I receive EOFError.
I tried this code:
import ftplib
ftp = ftplib.FTP()
ftp.connect( "1.2.3.4", "22" )
This method returns with an error after long time so I cannot perform a call to login.
I cannot try the constructor FTP([host[, user[, passwd[, acct[, timeout]]]]]) because
my port is 22 but ftplib default is 21.
If I follow the example
ftp = ftplib.FTP("1.2.3.4")
ftp = ftplib.FTP("1.2.3.4","22")
I receive a connection refused so I cannot enter any username password. Can you help me? Thank you very much
As the question you linked to states, ftplib doesn't support SFTP (which is a transfer protocol over SSH and has nothing to do with FTPS, FTP over SSL). Use the recommended Paramiko instead.

Categories