Python script that checks if an FTP login account is valid:
import ftplib
def check_ftp_login(host, username, password):
try:
# Attempt to establish an FTP connection
ftp = ftplib.FTP(host)
# Try to login with the provided credentials
ftp.login(username, password)
# If login is successful, close the connection and return True
ftp.quit()
return True
except ftplib.error_perm as e:
# If there's a permission error (e.g., invalid credentials), return False
print(f"Login failed: {str(e)}")
return False
except Exception as e:
# If there's any other error, print it and return False
print(f"An error occurred: {str(e)}")
return False
# Example usage
if __name__ == "__main__":
ftp_host = "ftp.example.com"
ftp_username = "your_username"
ftp_password = "your_password"
if check_ftp_login(ftp_host, ftp_username, ftp_password):
print("Login successful!")
else:
print("Login failed.")
Load From File
modified the script to load accounts from a file, save valid accounts to another file, and add a timeout for server connections. Here's the updated version
import ftplib
import socket
def check_ftp_login(host, username, password, timeout=10):
try:
# Attempt to establish an FTP connection with timeout
ftp = ftplib.FTP()
ftp.connect(host, timeout=timeout)
# Try to login with the provided credentials
ftp.login(username, password)
# If login is successful, close the connection and return True
ftp.quit()
return True
except ftplib.error_perm as e:
# If there's a permission error (e.g., invalid credentials), return False
print(f"Login failed for {username}@{host}: {str(e)}")
return False
except (socket.timeout, socket.error) as e:
# If there's a timeout or connection error, print it and return False
print(f"Connection error for {host}: {str(e)}")
return False
except Exception as e:
# If there's any other error, print it and return False
print(f"An error occurred for {username}@{host}: {str(e)}")
return False
def load_accounts(filename):
accounts = []
with open(filename, 'r') as f:
for line in f:
host, username, password = line.strip().split(',')
accounts.append((host, username, password))
return accounts
def save_valid_account(filename, host, username, password):
with open(filename, 'a') as f:
f.write(f"{host},{username},{password}\n")
# Main script
if __name__ == "__main__":
accounts = load_accounts('accounts.txt')
for host, username, password in accounts:
if check_ftp_login(host, username, password):
print(f"Login successful for {username}@{host}")
save_valid_account('valid.txt', host, username, password)
else:
print(f"Login failed for {username}@{host}")