Skip to content

Instantly share code, notes, and snippets.

@eldaroid
Last active October 7, 2024 19:04
Show Gist options
  • Save eldaroid/8e6158980c327db1c87fec86f8f8eecd to your computer and use it in GitHub Desktop.
Save eldaroid/8e6158980c327db1c87fec86f8f8eecd to your computer and use it in GitHub Desktop.
RU: Скрипт проверяет доступность IP-адресов в указанном диапазоне. Он пингует каждый IP-адрес в сети 192.168.0.x и выводит сообщение, если адрес доступен. EN: The script checks the availability of IP addresses in the specified range. It pings each IP address in the 192.168.0.x network and displays a message if the address is available
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 <number_of_ips>"
exit 1
fi
if ! [[ $1 =~ ^[0-9]+$ ]] || [ $1 -lt 0 ] || [ $1 -gt 255 ]; then
echo "Error: The argument must be a number in the range from 0 to 255."
exit 1
fi
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
NET="192.168.0."
LIMIT=$1
for ((i=0; i<LIMIT; i++))
do
ADR="${NET}${i}"
echo "Pinging $ADR..."
if ping -c 1 -W 1 $ADR &> /dev/null
then
echo -e "${GREEN}$ADR is alive${NC}\n"
else
echo -e "${RED}$ADR is not reachable${NC}\n"
fi
done
@eldaroid
Copy link
Author

eldaroid commented May 16, 2024

The same on Windows:

# Проверка наличия одного аргумента
if ($args.Count -ne 1) {
    Write-Host "Usage: .\CheckIPs.ps1 <number_of_ips>"
    Write-Host "It is necessary to enter a number in the range from 0 to 255."
    exit 1
}

# Проверка, что аргумент находится в диапазоне от 0 до 255
if (-not ($args[0] -match '^[0-9]+$') -or $args[0] -lt 0 -or $args[0] -gt 255) {
    Write-Host "Error: The argument must be a number in the range from 0 to 255."
    exit 1
}

# Цвета текста (работают в Windows Terminal)
$green = "`e[32m"
$red = "`e[31m"
$nc = "`e[0m"  # Сброс цвета

$net = "192.168.0."
$limit = [int]$args[0]

for ($i = 0; $i -lt $limit; $i++) {
    $adr = "$net$i"
    Write-Host "Pinging $adr..."  # Отладочное сообщение
    if (Test-Connection -ComputerName $adr -Count 1 -Quiet) {
        Write-Host "$green$adr is alive$nc"
    } else {
        Write-Host "$red$adr is not reachable$nc"
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment