-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp_check.py
executable file
·83 lines (73 loc) · 2.72 KB
/
ftp_check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python3
""" Script for validating credentials against an ftp server
"""
import argparse
from ftplib import FTP, Error as FTPError
from io import StringIO
import sys
SUCCESS_CODE = '230'
TMP_FILENAME = '.tmp1234.txt'
def check_credentials(ftp_server, username, password, verbose):
""" Function used to check a set of credentials against an FTP server
"""
valid = False
write = False
with FTP(ftp_server) as ftp:
try:
resp = ftp.login(username, password)
if verbose:
print(resp)
except FTPError as err:
if verbose:
print(err)
else:
valid = True
if valid:
# validate ability to write
try:
resp = ftp.storlines("STOR %s" % TMP_FILENAME, StringIO(""))
if verbose:
print(resp)
ftp.delete(TMP_FILENAME)
if verbose:
print(resp)
except FTPError as err:
if verbose:
print(err)
else:
write = True
# print result of check
if valid:
print("PASS (%s)" % ("W" if write else "R"), end='')
else:
print("FAIL", end='')
print(" - %s:%s" % (username, password))
def check_stdin(ftp_server, verbose):
""" Function used to read credentials from STDIN and check them
"""
(username, password) = (None, None)
for line in sys.stdin.readlines():
line = line.strip()
if username is None:
username = line
continue
elif password is None:
password = line
check_credentials(ftp_server, username, password, verbose)
(username, password) = (None, None)
def main():
""" Main function for handling user arguments
"""
parser = argparse.ArgumentParser(description='Script for validating FTP credentials')
parser.add_argument('ftp_server', help='Hostname/IP of the FTP server top validate agains')
parser.add_argument('username', default='anonymous', nargs='?', help='Username to validate')
parser.add_argument('password', default='', nargs='?', help='Username to validate')
parser.add_argument('--stdin', dest='STDIN', action='store_const', const=True, default=False, help='Read credentials from STDIN; username and passwd on zeperate lines')
parser.add_argument('-v', dest='verbose', action='store_const', const=True, default=False, help='Verbose output')
args = parser.parse_args()
if args.STDIN:
check_stdin(args.ftp_server, args.verbose)
else:
check_credentials(args.ftp_server, args.username, args.password, args.verbose)
if __name__ == "__main__":
main()