How I programmed a LAN Scanner using Bash
Shina Salau
Data Scientist || Former Cyber Security Intern @Prodigy InfoTech || IT Support || Bash Enthusiast
Today, I will share a project I picked up over the week. I programmed a Local Area Network (LAN) scanner using Bash. I also played with Python, to fetch the IP addresses on my network, but as you might have guessed, the execution speed is what I need at this stage, plus I don't need any fancy output after all, so I decided to stick with bash.
What is a LAN scanner?
A LAN (Local Area Network) scanner is a tool used to discover and analyse devices connected to a local network. It scans the network to identify active IP addresses, open ports, running services, and other network attributes of devices such as computers, printers, routers, and IoT devices. There are many LAN scanners (GUI-based) out there, including LaN Scanner itself (GUI), better cap (CLI), arp-scan (CLI), etc.
How important is a LAN scanner?
A LAN scanner is a useful tool for maintaining network security by providing visibility, identifying vulnerabilities, and ensuring compliance with security policies.
How I programmed my LAN scanner (snippets)
I mentioned earlier that I played with Python a bit:
Then I maintained just the `capture_users.sh` shell script and added the function that fetches not only the IP address but the MAC address of users on my etwork.
领英推荐
# `arp-scan` needs to be installed before this can work
To INSTALL
On Linux/Ubuntu:
--------------------------------
sudo apt-get install arp-scan
-------------------------------
-> on Mac:
----------------------------
brew install arp-scan
----------------------------
2. I fetched the list of IP addresses connected to the same network as I was using:
ips=$(sudo arp-scan --localnet | grep -o -E '([0-9]{1,3}\.){3}[0-9]{1,3}')
3. I fetched the list of MAC addresses in the `arp-scan` output using:
mac_addr=$(sudo arp-scan --localnet | grep -o -E '([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}')
NB: The regex values at `ips` and `mac_addr` were obtained from an online source.
4. Splitting into Arrays
Next, I split the outputs (`ips` and `mac_addr`) from the `arp-scans` into arrays and looped over to return the `ips` and `mac_addr`, side-by-side, as output.
The script is executable on multi-platforms such as MacOS, Linux, and Ubuntu operating systems.
Check this project out on my GitHub!
Thank you! Until next time!