-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAB_ip_sort.py
127 lines (121 loc) · 5.93 KB
/
AB_ip_sort.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
116
117
118
119
120
121
122
123
124
125
126
127
'''
NAME Abdul Baasit
DATE 3rd November 2020
DESCRIPTION File handling and Binary to decimal conversion
-------------------------------------------------------------------------'''
#Variables--------------------------------------------------------------
shuffledList = "ipaddress_shuffled_list.txt"//list of shuffled ip address
shuffledBinList = "ipaddress_shuffled_list_bin.txt"//list of shuffled ip addresses in binary
sorted_List = "AB_ipaddress_sorted.txt"//final file
address_list = []
description = "File created by Abdul Baasit\n"
star_format = "*"*50
#---------------------------------------------------------------------
"""---------------------------------------------------------------------------------------------------
Function: get_file_lines_in_list
date : 3rd November 2020
Author: Fanshawe College
modified : Abdul Baasit
Input arguement: Text(.txt) file
output: list
Description : removes the next line('\n') escape character from the text file and appends it to a list
---------------------------------------------------------------------------------------------------"""
def get_file_lines_in_list(file):
line_list=[]
with open(file,"r") as f:
for line in f:
line_wo_nline = line[:-1]
line_list.append(line_wo_nline)
print("Next line escape character has been removed from ip_address_shuffled_list.txt\n")
return line_list
#------end-of-get_file_lines_in_list------------------------------------------------------------------------------------------------
"""---------------------------------------------------------------------------------------------------
Function: bin_to_decimal
date : 3rd November 2020
Input argument: Text(.txt) file
output: list
Description : 1.removes the next line('\n') escape character from the text file
2.converts the binary values to decimal
3.appends it to the list and returns list
---------------------------------------------------------------------------------------------------"""
def bin_to_decimal(file):
line_list=[]
temp_str=""
with open(file,"r") as f:
for line in f:
line_wo_nline = line[:-1]
line_wo_nline=line_wo_nline.split(".")
temp_str = "{0}.{1}.{2}.{3}".format(int(line_wo_nline[0],2),int(line_wo_nline[1],2),int(line_wo_nline[2],2),int(line_wo_nline[3],2))
line_list.append(temp_str)
print(star_format)
print("Next line escape character has been removed from ip_address_shuffled_list_Bin.txt\n")
print(star_format)
print("Binary to Decimal Conversion Done\n")
print(star_format)
return line_list
#------end-of-bin_to_decimal------------------------------------------------------------------------------------------------
"""---------------------------------------------------------------------------------------------------
Function: write_to_file
date : 3rd November 2020
Input argument: Text(.txt) file,string
output: none
Description : writes the file input string to text file
---------------------------------------------------------------------------------------------------"""
def write_to_file(file,data_to_be_added):
with open(file, "w") as f:
f.write(data_to_be_added)
print("Done Writing:",description,"\nTo file :",file)
print(star_format)
#------end-of-write_to_file------------------------------------------------------------------------------------------------
"""---------------------------------------------------------------------------------------------------
Function: append_to_file
date : 3rd November 2020
Author: Fanshawe College
modified : Abdul Baasit
Input argument: Text(.txt), list
output: none
Description : Appends the data from list to text file
---------------------------------------------------------------------------------------------------"""
def append_to_file(file,data_to_append):
with open(file,"a") as f:
for line in data_to_append:
f.write(line)
f.write("\n")
print("\nDone appending",len(address_list),"ip addresses\nTo file :",file)
print(star_format)
#------end-of-get_file_lines_in_list------------------------------------------------------------------------------------------------
"""---------------------------------------------------------------------------------------------------
Function: get_file_lines_in_list
date : 3rd November 2020
Author: Abdul Baasit
modified : none
Input argument: Text(.txt) file
output: list
Description : removes the next line('\n') escape character from the text file and appends it to a list
---------------------------------------------------------------------------------------------------"""
def sort_ipaddress(data_to_sort):
for i in range(len(data_to_sort)):
data = data_to_sort[i]
if data[3] == '1':
with open("switch_ipaddress.txt","a") as f:
f.write(data)
f.write("\n")
elif data[3] == '2':
with open("server_ipaddress.txt","a") as f:
f.write(data)
f.write("\n")
else:
with open("host_ipaddress.txt","a") as f:
f.write(data)
f.write("\n")
print("\nSorting Completed, Please check your Folder for files")
print(star_format)
#------end-of-get_file_lines_in_list------------------------------------------------------------------------------------------------
#--------CODE--EXECUTION-- BEGINS------------------------------------------
address_list= get_file_lines_in_list(shuffledList)
address_list= address_list + (bin_to_decimal(shuffledBinList))#-combining all ip address into a single list
address_list.sort()#-sorting the addresses
write_to_file(sorted_List,description)
append_to_file(sorted_List,address_list)
sort_ipaddress(address_list)
#----------end--of--code---------------------------------------------------