Knowing which devices are connected to your local network can help you manage and secure your environment. This Bash script scans the local network for active devices and logs their IP addresses and hostnames.

Step-by-Step Guide:

1. Define the Network Range: Specify the IP range of your local network.

NETWORK="192.168.1.0/24"

 

2. Define the Log File: Specify the location where the log file will be stored.

LOG_FILE="/var/log/network_scan.log"

 

3. Scan the Network: Use nmap to scan the network and capture the IP addresses and hostnames of active devices.

nmap -sn $NETWORK -oG - | awk '/Up$/{print $2, $3}' > $LOG_FILE

 

4. Output the Results: Display the contents of the log file.

cat $LOG_FILE

 

Complete Script:

#!/bin/bash

# Network range
NETWORK=“192.168.1.0/24”

# Log file
LOG_FILE=“/var/log/network_scan.log”

# Scan the network
nmap -sn $NETWORK -oG – | awk ‘/Up$/{print $2, $3}’ > $LOG_FILE

# Output the results
cat $LOG_FILE

Usage:

  • Replace 192.168.1.0/24 with your network range.
  • You can run it traditionally with sh /path/to/script.sh or add it to crontab for continous scans.
  • Make sure you have nmap installed in your Linux OS and if not, install it first

This script helps you keep track of the devices on your network, making it easier to manage and secure your local environment.