1

I'm trying to verify if a Directory out of a List of Directories does exist on a FTP-Server, if so, then the Script should change working directory to the existing Directory to create a new Directory, but I simply can't verify if the Directory is existing by using following Code:

import ftplib
import sys

def check(hostlist):
    users = ["anonymous","ftp","upload"]
    passwords = open('passwords.txt','r').read().splitlines()
    hostlist = sys.argv[1]
    directories = ["incoming","upload","ftproot"]
    hosts = open(hostlist,'r').read().splitlines()
    for host in hosts:
        for user in users:
            for password in passwords:
                try:
                    connection = ftplib.FTP(host.replace(':21',''))        
                    if '230' in connection.login(user=user,passwd=password):
                        dirs = []
                        connection.dir(dirs.append)
                        if directories[0] in dirs:
                            connection.cwd(directories[0])
                            connection.mkd("newdir")
                            logfile = open('results.txt','a')
                            logfile.write(host+" User: "+user+" Password: "+password+" Writeable: "+directories[0]+" \n")
                            logfile.close()
                            connection.quit()
                        elif directories[1] in dirs:
                            print(directories[1])
                        else:
                            print("No Directory found!!!")
                except ftplib.all_errors as e:
                    print(e)

check(sys.argv[1])
StYl3z
  • 11
  • 2

1 Answers1

0

Not sure, check if this is what you want, else post your current error/output

def check(hostlist):
    users = ["anonymous","ftp","upload"]
    passwords = open('passwords.txt','r').read().splitlines()
    hostlist = sys.argv[1]
    directories = ["incoming","upload","ftproot"]
    hosts = open(hostlist,'r').read().splitlines()
    for host in hosts:
        for user in users:
            for password in passwords:
                try:
                    connection = ftplib.FTP(host.replace(':21',''))        
                    if '230' in connection.login(user=user,passwd=password):
                        dirs = []
                        connection.dir(dirs.append)
                        for dct in directories:
                            if dct in dirs:
                                connection.cwd(dct)
                                connection.mkd("newdir")
                                logfile = open('results.txt','a')
                                logfile.write(host+" User: "+user+" Password: "+password+" Writeable: "+directories[0]+" \n")
                                logfile.close()
                                connection.quit()
                        else:
                            print("No Directory found!!!")
                except ftplib.all_errors as e:
                    print(e)
Ajay Tom George
  • 1,890
  • 1
  • 14
  • 26
  • already tried it the way you posted, but it won't work either. :( ``` C:\Users\PC\Desktop>python3 pubscanner.py hosts.txt No Directory found!!! 530 User ftp cannot login. 530 User upload cannot login. ``` – StYl3z Feb 23 '20 at 16:12
  • The error is not with the code, FTP user does not have required privileges. Check this thread https://stackoverflow.com/questions/3355848/ftp-error-530-user-cannot-login – Ajay Tom George Feb 23 '20 at 16:19