SoFunction
Updated on 2025-03-05

How to quickly check whether the 192.168.1 network segment IP is in use?

examine192.168.1Which IP addresses are being used in the network segment? You can use Shell scripts to combinepingCommand to concurrently scan the entire network segment. Here is the complete script to implement this function:

Script: Check whether the 192.168.1 network segment IP is in use

#!/bin/bash
# Define network segmentsNETWORK="192.168.1"
# Define timeout (seconds)TIMEOUT=1
# Define the number of IPs to be scanned in parallelPARALLEL=20
# Log Functionfunction log_info() {
  echo -e "[INFO] $1"
}
function log_success() {
  echo -e "\033[32m[SUCCESS] $1\033[0m"
}
function log_error() {
  echo -e "\033[31m[ERROR] $1\033[0m"
}
# Check if a single IP is onlinefunction check_ip() {
  local ip=$1
  if ping -c 1 -W "$TIMEOUT" "$ip" &> /dev/null; then
    log_success "IP $ip is online"
  else
    log_error "IP $ip is offline"
  fi
}
# Main functionfunction main() {
  log_info "Scanning network $NETWORK.0/24..."
  # Use loop to traverse the IP address of 1-254  for i in {1..254}; do
    ip="$NETWORK.$i"
    check_ip "$ip" &
    # Control the parallel number    if [[ $(jobs -r -p | wc -l) -ge $PARALLEL ]]; then
      wait -n
    fi
  done
  # Wait for all background tasks to complete  wait
}
# Execute the main functionmain

Script Description

  • Network segment definition

    • NETWORK="192.168.1": Define the network segment to be scanned.
  • Timeout

    • TIMEOUT=1:set uppingThe timeout time of the command is 1 second.
  • Parallel scanning

    • PARALLEL=20: Set the number of IPs to scan simultaneously to 20 to improve scanning efficiency.
  • Log output

    • Use different color logs to distinguish between online and offline IP addresses:
      • Green: IP online.
      • Red: IP offline.
  • check_ipFunction

    • usepingCommand checks whether a single IP is online.
    • ifpingIf successful, the IP will be online; otherwise, the IP will be offline.
  • Main functionmain

    • Traversal192.168.1.1arrive192.168.1.254All IP addresses of  .
    • use&Willcheck_ipPut it in the background to execute, and realize parallel scanning.
    • usejobsandwaitControl the number of parallel tasks.

How to use

  • Save the script asscan_network.sh

Grant execution permissions:

chmod +x scan_network.sh

Run the script:

./scan_network.sh

Sample output

[INFO] Scanning network 192.168.1.0/24...
[SUCCESS] IP 192.168.1.1 is online
[ERROR] IP 192.168.1.2 is offline
[SUCCESS] IP 192.168.1.3 is online
[ERROR] IP 192.168.1.4 is offline
...

Optimization suggestions

Add IP range parametersThe IP range of scanned can be specified through command line parameters, for example:

./scan_network.sh 192.168.1.10 192.168.1.20

Modify the script:

START_IP=${1:-1}
END_IP=${2:-254}
for i in $(seq "$START_IP" "$END_IP"); do
  ip="$NETWORK.$i"
  check_ip "$ip" &
done

Save the results to fileThe scan results can be saved to a file for easier analysis:

LOG_FILE="scan_results.txt"
function log_success() {
  echo -e "\033[32m[SUCCESS] $1\033[0m"
  echo "[SUCCESS] $1" >> "$LOG_FILE"
}
function log_error() {
  echo -e "\033[31m[ERROR] $1\033[0m"
  echo "[ERROR] $1" >> "$LOG_FILE"
}

usefpingReplacementpingfpingIt is a more efficient network scanning tool that supports batch scanning. If the system is installedfping, can be replacedpingOrder:

fping -c 1 -t "$TIMEOUT" "$ip" &> /dev/null

Summarize

The script passes parallelpingScan192.168.1Network segment, quickly detect which IP addresses are online. By adjusting the number of parallels and timeouts, scan speed and accuracy can be balanced. If you need more efficient scanning, you can consider usingnmaporfpingand other professional tools.

This is the article about how to quickly check whether the 192.168.1 network segment IP is being used. For more relevant shell checks whether the 192.168.1 network segment IP is being used, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!