Introduction
In this project, you will learn how to build a network scanner using Python. The network scanner will utilize the nmap command-line tool to scan a specified IP address and display the results in a graphical user interface (GUI) using the Tkinter library.

🎯 Tasks
In this project, you will learn:
- How to set up the project and install necessary libraries
- How to import necessary libraries for GUI and executing the
nmapcommand-line tool - How to define a scanning function to retrieve the user-entered IP address and scan the network
- How to create the main window of the GUI
- How to add an input field for the IP address and a "Scan Network" button
- How to create a text area to display the scan results
- How to run the project and perform network scanning
🏆 Achievements
After completing this project, you will be able to:
- Set up a Python project and install libraries
- Import libraries and use them in a Python script
- Create a GUI using Tkinter
- Handle user input and trigger functions
- Use the
nmapcommand-line tool for network scanning
Set up the project
Create a new Python file and name it network_scanner.py. Open the file in a code editor of your choice.
cd ~/project
touch network_scanner.py
Then, install the nmap, tkinter, and python-nmap command-line tool using the following command:
sudo apt update
sudo apt-get install nmap -y
sudo apt-get install python3-tk -y
sudo pip install python-nmap
Import necessary libraries
At the beginning of the file, import the required libraries: tkinter for GUI and subprocess for executing the nmap command-line tool.
import tkinter as tk
import subprocess
Define the scanning function
Define a function called scan_network that will be triggered when the "Scan Network" button is clicked. This function will retrieve the IP address entered by the user, clear the result text area, and execute the nmap command to scan the network.
def scan_network():
ip_address = entry_ip.get()
result_text.delete(1.0, tk.END)
result_text.insert(tk.END, f"Scanning network: {ip_address}\n\n")
try:
output = subprocess.check_output(["nmap", "-F", ip_address])
result_text.insert(tk.END, output.decode("utf-8"))
except subprocess.CalledProcessError:
result_text.insert(tk.END, "An error occurred while scanning the network.")
Create the main window
Create the main window of the GUI using the Tk() class from Tkinter.
window = tk.Tk()
window.title("Network Scanner")
Add input field and scan button
Create a label and an entry field for the user to enter the IP address. Then, create a "Scan Network" button that will call the scan_network function when clicked.
label_ip = tk.Label(window, text="Enter IP Address:")
label_ip.pack()
entry_ip = tk.Entry(window)
entry_ip.pack()
scan_button = tk.Button(window, text="Scan Network", command=scan_network)
scan_button.pack()
Add result text area
Create a text area using the Text() class from Tkinter. This text area will display the scan results.
result_text = tk.Text(window)
result_text.pack()
Start the main loop
Add the following line at the end of the file to start the main loop of the GUI:
window.mainloop()
Run the project
Save the file and run it using a Python interpreter. The GUI window will appear.
cd ~/project
python network_scanner.py
Enter the IP address you want to scan in the input field and click the "Scan Network" button. The network scan will be performed using nmap, and the results will be displayed in the text area.

Summary
In this project, you have learned how to build a network scanner in Python using the Tkinter library. The network scanner utilizes the nmap command-line tool to scan a specified IP address. The results are displayed in a graphical user interface, allowing users to easily perform network scans and view the output. You can further enhance the network scanner by adding more features and improving the user interface according to your requirements.



