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