Completed
Push — master ( c9526d...fffeed )
by Sepand
01:04
created

ARP()   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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
def ping(i): # ping function
9
    output=str(list(sub.Popen("ping "+i,stdout=sub.PIPE,stderr=sub.PIPE,shell=True).communicate())[0])
10
    return output
11
def ip_filter(i_list): # This Function Get A List Of IPs and Split IP
12
    '''((list)->list'''
13
    temp_list=[]
14
    for i in range(len(i_list)):
15
        for j in range(len(i_list[i])):
16
            if i_list[i][j] not in dic:
17
                temp_list.append(i_list[i][:j])
18
                break
19
    return temp_list
20
def search_ip(output): # This Function Get A String As Input ( ARP Command Output) and Find For Local IPs
21
    '''(str)->list'''
22
    index=0
23
    ip_list=[]
24
    while(True):
25
        index=output.find("192",index)
26
        ip_list.append(output[index:index+16])
27
        if (index==-1):
28
            break
29
        else:
30
            index=index+16
31
    return ip_list
32
def string_conv(i):
33
    return mask+str(i)
34
def ARP(my_ip):
35
    sub.Popen("ping " + my_ip, stdout=sub.PIPE, stderr=sub.PIPE, shell=True)
36
    response = sub.Popen("arp -a", stdout=sub.PIPE, stderr=sub.PIPE, shell=True)
37
    output = str(list(response.communicate())[0])
38
    ip_list = search_ip(output)
39
    ip_list = ip_filter(ip_list)
40
    for i in ip_list:
41
        print("IP : ", i, "Is  available")
42
def Manual(range_min,range_max,file):
43
    try:
44
        ip_list = list(map(string_conv, list(range(range_min, range_max + 1))))
45
        p = mu.Pool(mu.cpu_count() + 100)  # for multiprocessing
46
        result = p.map(ping, ip_list)  # result of pings
47
        for output in result:
48
            if output.find("timed out") == -1 and output.find("unreachable") == -1:
49
                # ssh_response=sub.call("ssh "+ip_list[result.index(output)],stdout=sub.PIPE,stderr=sub.PIPE,timeout=30,shell=True)
50
                print("IP : ", ip_list[result.index(output)], "Is available")
51
                file.write("IP : "+ ip_list[result.index(output)]+ " Is available\n")
52
            else:
53
                print("IP : ", ip_list[result.index(output)], "Is not available")
54
                file.write("IP : "+ ip_list[result.index(output)]+ " Is not available\n")
55
    except Exception as e:
56
        if file.closed==False:
57
            file.close()
58
        print(e)
59
60
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
61
    log_file=open("log_file.txt","a")
62
    if mode=="manual":
63
        Manual(range_max=range_max,range_min=range_min,file=log_file)
64
        log_file.close()
65
    elif mode=="ARP":
66
        ARP(my_ip)
67
68
if __name__=="__main__":
69
    mask="192.168.166."
70
    mu.freeze_support()
71
    dic=list(string.digits+".")
72
    print("Running On Netmask: " + mask)
73
    my_ip=socket.gethostbyname(socket.gethostname())
74
    if my_ip=="127.0.0.1":
75
        print("Problem In Netwrok Connection ( Please Check )")
76
        input()
77
        sys.exit()
78
    ssh_test=sub.Popen("ssh",stdout=sub.PIPE,stderr=sub.PIPE,shell=True)
79
    ssh_result=str(list(ssh_test.communicate())[1])
80
    #print(ssh_result)
81
    inp=int(input("Please Choose ARP[1] or Linear Search[2]"))
82
    time_1=time.perf_counter()
83
    if inp==1:
84
        print("Please Wait : Scan IPs . . . ")
85
        find(mode="ARP",my_ip=my_ip)
86
        time_2=time.perf_counter()
87
    else:
88
        get_mask=input("Please Enter Mask :")
89
        try:
90
            range_max_input=int(input("Please Enter Range Max : "))
91
            range_min_input=int(input("Please Enter Range Min : "))
92
        except ValueError: # If User Ignore Input Step
93
            range_max_input=0
94
            range_min_input=0
95
        print("Please Wait : Scan IPs . . . ")
96
        if get_mask.find("192.")!=-1 and get_mask.find("168.")!=-1:
97
            if get_mask[-1]!=".":
98
                get_mask=get_mask+"."
99
            mask=get_mask
100
        if range_max_input>range_min_input and range_max_input<256:
101
            find(range_max=range_max_input,range_min=range_min_input)
102
        else:
103
            find()
104
        time_2=time.perf_counter()
105
    print("Scan Time :",str((time_2-time_1)/60)," min")
106
    input("Press Any Key To Exit")
107
                
108
        
109
        
110
        
111