forked from tasha06/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_scanner.py
50 lines (41 loc) · 1.6 KB
/
ip_scanner.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
#!usr/bin/env python3
# scans wlan and returns all connected ip's
#improvement ideas: create way of breaking down ips's to return device makes as well
import re
import subprocess as sub
import scapy.all as scapy
# import optparse
def get_current_ip_range():
ip_get = sub.check_output(["ip", "route"])
ip_search = re.search(r"([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/24", ip_get)
return ip_search.group(0)
# def get_arguments():
# parser = optparse.OptionParser()
# parser.add_option("-t", "--target",
# dest="target",
# type="string",
# help="Target IP address")
# (options, arguments) = parser.parse_args()
# if options.target is None:
# print("Please enter -t option with valid argument")
# exit()
# else:
# return options
def scan(ip):
arp_request = scapy.ARP(pdst=ip)
broadcast_signal = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
arp_request_broadcast = broadcast_signal/arp_request
answered_list = scapy.srp(arp_request_broadcast, timeout=3, verbose=False)[0]
clients_list = []
for element in answered_list:
client_dict = {"ip": element[1].psrc, "mac": element[1].hwsrc}
clients_list.append(client_dict)
return clients_list
def print_result(result_list):
print("IP\t\t\tMAC Address\n-----------------------------------------")
for client in result_list:
print(client["ip"] + "\t\t" + client["mac"])
# option = get_arguments()
gathered_ip_range = get_current_ip_range()
ip_and_mac = scan(gathered_ip_range)
print_result(ip_and_mac)