1
|
|
|
import socket # Added For Finding Host IP |
2
|
|
|
import subprocess as sub # Added For Run Command By CMD |
3
|
|
|
import string |
4
|
|
|
import datetime |
5
|
|
|
import sys # For Exit |
6
|
|
|
import multiprocessing as mu # for multtiprocessing |
7
|
|
|
import time |
8
|
|
|
import os |
9
|
|
|
mask = "192.168.1." |
10
|
|
|
def logo_handler(): |
11
|
|
|
if "logo.txt" in os.listdir(): |
12
|
|
|
with open("logo.txt","r") as logo: |
13
|
|
|
for char_line in logo: |
14
|
|
|
print(char_line.rstrip()) |
15
|
|
|
print(line(70,"*")) |
16
|
|
|
print("Visit : "+"http://github.com/sepandhaghighi/findip") |
17
|
|
|
print(line(70, "*")) |
18
|
|
|
def line(number,char="-"): |
19
|
|
|
response="" |
20
|
|
|
i=0 |
21
|
|
|
while(i<number): |
22
|
|
|
response+=char |
23
|
|
|
i+=1 |
24
|
|
|
return response |
25
|
|
|
def ping(i): # ping function |
26
|
|
|
output=str(list(sub.Popen("ping "+i,stdout=sub.PIPE,stderr=sub.PIPE,shell=True).communicate())[0]) |
27
|
|
|
return output |
28
|
|
|
def ip_filter(i_list): # This Function Get A List Of IPs and Split IP |
29
|
|
|
'''((list)->list''' |
30
|
|
|
dic = list(string.digits + ".") |
31
|
|
|
temp_list=[] |
32
|
|
|
for i in range(len(i_list)): |
33
|
|
|
for j in range(len(i_list[i])): |
34
|
|
|
if i_list[i][j] not in dic: |
35
|
|
|
temp_list.append(i_list[i][:j]) |
36
|
|
|
break |
37
|
|
|
return temp_list |
38
|
|
|
def search_ip(output): # This Function Get A String As Input ( ARP Command Output) and Find For Local IPs |
39
|
|
|
'''(str)->list''' |
40
|
|
|
index=0 |
41
|
|
|
ip_list=[] |
42
|
|
|
while(True): |
43
|
|
|
index=output.find("192",index) |
44
|
|
|
ip_list.append(output[index:index+16]) |
45
|
|
|
if (index==-1): |
46
|
|
|
break |
47
|
|
|
else: |
48
|
|
|
index=index+16 |
49
|
|
|
return ip_list |
50
|
|
|
def string_conv(i): |
51
|
|
|
return mask+str(i) |
52
|
|
|
def ARP(my_ip,file): |
53
|
|
|
try: |
54
|
|
|
sub.Popen("ping " + my_ip, stdout=sub.PIPE, stderr=sub.PIPE, shell=True) |
55
|
|
|
response = sub.Popen("arp -a", stdout=sub.PIPE, stderr=sub.PIPE, shell=True) |
56
|
|
|
output = str(list(response.communicate())[0]) |
57
|
|
|
ip_list = search_ip(output) |
58
|
|
|
ip_list = ip_filter(ip_list) |
59
|
|
|
for i in ip_list: |
60
|
|
|
print("IP : ", i, "Is available") |
61
|
|
|
file.write("IP : "+ i+ "Is available\n") |
62
|
|
|
except Exception as e: |
63
|
|
|
if file.closed==False: |
64
|
|
|
file.close() |
65
|
|
|
print(e) |
66
|
|
|
|
67
|
|
|
def Manual(range_min,range_max,file): |
68
|
|
|
try: |
69
|
|
|
ip_list = list(map(string_conv, list(range(range_min, range_max + 1)))) |
70
|
|
|
p = mu.Pool(mu.cpu_count() + 100) # for multiprocessing |
71
|
|
|
result = p.map(ping, ip_list) # result of pings |
72
|
|
|
for output in result: |
73
|
|
|
if output.find("timed out") == -1 and output.find("unreachable") == -1: |
74
|
|
|
# ssh_response=sub.call("ssh "+ip_list[result.index(output)],stdout=sub.PIPE,stderr=sub.PIPE,timeout=30,shell=True) |
75
|
|
|
print("IP : ", ip_list[result.index(output)], "Is available") |
76
|
|
|
file.write("IP : "+ ip_list[result.index(output)]+ " Is available\n") |
77
|
|
|
else: |
78
|
|
|
print("IP : ", ip_list[result.index(output)], "Is not available") |
79
|
|
|
file.write("IP : "+ ip_list[result.index(output)]+ " Is not available\n") |
80
|
|
|
except Exception as e: |
81
|
|
|
if file.closed==False: |
82
|
|
|
file.close() |
83
|
|
|
print(e) |
84
|
|
|
|
85
|
|
|
def find(mode="manual",my_ip="0.0.0.0",range_min=0,range_max=254): # This Function Ping And SSH IPs to find SSH Server |
86
|
|
|
log_file=open("log_file.txt","a") |
87
|
|
|
log_file.write(str(datetime.datetime.now())+"\n") |
88
|
|
|
log_file.write(line(30,"%")+"\n") |
89
|
|
|
if mode=="manual": |
90
|
|
|
Manual(range_max=range_max,range_min=range_min,file=log_file) |
91
|
|
|
elif mode=="ARP": |
92
|
|
|
ARP(my_ip,log_file) |
93
|
|
|
log_file.write(line(30, "*") + "\n") |
94
|
|
|
log_file.close() |
95
|
|
|
def set_mask(): |
96
|
|
|
get_mask = input("Please Enter Mask :") |
97
|
|
|
if get_mask.find("192.") != -1 and get_mask.find("168.") != -1: |
98
|
|
|
if get_mask[-1] != ".": |
99
|
|
|
get_mask = get_mask + "." |
100
|
|
|
return get_mask |
101
|
|
|
def set_range(): |
102
|
|
|
try: |
103
|
|
|
range_max_input = int(input("Please Enter Range Max : ")) |
104
|
|
|
range_min_input = int(input("Please Enter Range Min : ")) |
105
|
|
|
except ValueError: # If User Ignore Input Step |
106
|
|
|
range_max_input = 0 |
107
|
|
|
range_min_input = 0 |
108
|
|
|
print("Please Wait : Scan IPs . . . ") |
109
|
|
|
if range_max_input > range_min_input and range_max_input < 256: |
110
|
|
|
find(range_max=range_max_input, range_min=range_min_input) |
111
|
|
|
else: |
112
|
|
|
find() |
113
|
|
|
def main(): |
114
|
|
|
logo_handler() |
115
|
|
|
mu.freeze_support() |
116
|
|
|
global mask |
117
|
|
|
print("Running On Netmask: " + mask) |
118
|
|
|
my_ip = socket.gethostbyname(socket.gethostname()) |
119
|
|
|
if my_ip == "127.0.0.1": |
120
|
|
|
print("Problem In Netwrok Connection ( Please Check )") |
121
|
|
|
input() |
122
|
|
|
sys.exit() |
123
|
|
|
inp = int(input("Please Choose ARP[1] or Linear Search[2]")) |
124
|
|
|
time_1 = time.perf_counter() |
125
|
|
|
if inp == 1: |
126
|
|
|
print("Please Wait : Scan IPs . . . ") |
127
|
|
|
find(mode="ARP", my_ip=my_ip) |
128
|
|
|
time_2 = time.perf_counter() |
129
|
|
|
else: |
130
|
|
|
mask = set_mask() |
131
|
|
|
set_range() |
132
|
|
|
time_2 = time.perf_counter() |
133
|
|
|
print("Scan Time :", str((time_2 - time_1) / 60), " min") |
134
|
|
|
input("Press Any Key To Exit") |