-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipypass.py
115 lines (89 loc) · 3.04 KB
/
ipypass.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
"""Take IPv4 address and return 8- or 12-bit passwords."""
import argparse
import ipaddress
import logging
import tkinter as tk
def parse_cli_args(arguments=None):
"""
Command line parser for IPv4 address of interest, with validation.
Returns:
IPv4 address as an instance of ipaddress.IPv4Address.
Raises:
argparse.ArgumentTypeError: If the IP address is invalid.
"""
parser = argparse.ArgumentParser(description="IPv4 address of interest.")
parser.add_argument("--ip", action="store", type=ipaddress.IPv4Address,
required=False, help="IP address of interest, e.g. 0.0.0.0")
arguments = parser.parse_args(arguments)
return arguments.ip if hasattr(arguments, 'ip') else None
def eight_bit_passwd(split_address):
"""
Transform IP address to 8-bit password.
Args:
split_address: List containing the four octets of the IP address.
Returns:
8-bit password.
"""
return (split_address[2] + '*' +
str(int(split_address[3]) + 8)).ljust(8, '*')
def twelve_bit_passwd(split_address):
"""
Transform IP address to 12-bit password.
Args:
split_address: List containing the four octets of the IP address.
Returns:
12-bit password.
"""
return (split_address[2] + '*' +
str(int(split_address[3]) + 12) + '*' +
split_address[1]).ljust(12, '*')
def create_table(ip_address):
"""
Create a crude table.
Args:
ip_address: The IP address.
Prints:
Table with transformed output.
"""
split_address = str(ip_address).split('.')
print('8-bit ', '12-bit ')
print('--------', '------------')
print(eight_bit_passwd(split_address), twelve_bit_passwd(split_address))
def ipypass():
"""
Transform IPv4 address to 8- or 12-bit password (GUI).
Returns:
8- and 12-bit passwords.
"""
def on_convert():
ip_address = ip_address_entry.get()
try:
address = ipaddress.IPv4Address(ip_address)
split_address = str(address).split('.')
eight_bit = eight_bit_passwd(split_address)
twelve_bit = twelve_bit_passwd(split_address)
except ValueError as exception:
logging.exception(exception)
eight_bit = 'Invalid input.'
twelve_bit = 'Try again.'
eight_bit_label.config(text=eight_bit)
twelve_bit_label.config(text=twelve_bit)
root = tk.Tk()
root.title("IPyPass")
tk.Label(root, text="Enter IP address:").pack()
ip_address_entry = tk.Entry(root)
ip_address_entry.pack()
convert_button = tk.Button(root, text="Convert", command=on_convert)
convert_button.pack()
eight_bit_label = tk.Label(root, text="", width=16)
eight_bit_label.pack()
twelve_bit_label = tk.Label(root, text="", width=16)
twelve_bit_label.pack()
root.mainloop()
if __name__ == '__main__':
ARGS = parse_cli_args()
if ARGS is not None:
create_table(ARGS)
else:
ipypass()