Skip to content

Instantly share code, notes, and snippets.

@RajivCodeLab
Created April 4, 2024 13:06
Show Gist options
  • Save RajivCodeLab/1ec04ddd5be339587d1e53e60e4bffaf to your computer and use it in GitHub Desktop.
Save RajivCodeLab/1ec04ddd5be339587d1e53e60e4bffaf to your computer and use it in GitHub Desktop.
IP Scanner (Used to find IP address of Raspberry Pi)
import tkinter as tk
from tkinter import messagebox
from scapy.all import ARP, Ether, srp
def get_connected_devices():
arp_request = Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst="192.168.1.0/24")
result = srp(arp_request, timeout=3, verbose=False)[0]
connected_devices = [{'ip': received.psrc, 'mac': received.hwsrc, 'hostname': received.getlayer(ARP).psrc} for sent, received in result]
return connected_devices
def search_devices():
search_label.config(text="Searching...")
global all_devices_set
all_devices = get_connected_devices()
all_devices_set = set(device['ip'] for device in all_devices)
update_device_list(all_devices)
search_new_button.config(state=tk.NORMAL)
search_label.config(text="")
def search_new_devices():
search_label.config(text="Searching...")
global all_devices_set
new_devices = get_connected_devices()
new_ips = {device['ip'] for device in new_devices}
added_ips = new_ips - all_devices_set
if added_ips:
messagebox.showinfo("New IPs Found", f"New IPs found: {', '.join(added_ips)}")
else:
messagebox.showinfo("No New IPs Found", "No new IPs were found.")
all_devices_set.update(new_ips)
update_device_list(new_devices)
search_label.config(text="")
def update_device_list(devices):
result_text.config(state=tk.NORMAL)
result_text.delete(1.0, tk.END)
for device in devices:
result_text.insert(tk.END, f"IP: {device['ip']}, MAC: {device['mac']}\n")
result_text.config(state=tk.DISABLED)
root = tk.Tk()
root.title("IP Scanner")
button_frame = tk.Frame(root)
button_frame.pack(pady=10)
search_button = tk.Button(button_frame, text="Search for Devices", command=search_devices)
search_button.grid(row=0, column=0, padx=5)
search_new_button = tk.Button(button_frame, text="Search for New Devices", command=search_new_devices, state=tk.DISABLED)
search_new_button.grid(row=0, column=1, padx=5)
search_label = tk.Label(root, text="")
search_label.pack()
result_text = tk.Text(root, height=10, width=50)
result_text.pack(pady=10)
result_text.config(state=tk.DISABLED)
all_devices_set = set()
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment