Skip to content

Simple bash script for PING a lan section under linux.


You can use it under Windows too by installing Cygwin which let you use cool stuff from Linux under Windows. It works well, i will write some articles about what you can do with it later.

I use this script for ping a piece of network to see if the peripherals are on line, printers, computers, phones. Feel free to use it, not perfect but it works and the job is done. Copy and paste the code in a text file named mylanping.sh and do :

chmod u+x mylanping.sh

After that you just have to

./mylanping.sh 192.168.1 1 25

An it will ping from 192.168.1.1 to 192.168.1.25 with a nice colored output

The script

#!/bin/bash

lan=$1;
start=$2;
end=$3;

if [ -z $1 ]
then
        echo -e "\033[1;91mNetwork is missing\033[0m";
        echo -e 'Example "./mylanping.sh \033[1;91m10.95.1\033[0m 1 254"';
        exit 1;
fi
if [ -z $2 ]
then
        echo -e "\033[1;91mFirst ip is missing\033[1;91m";
        echo -e 'Example "./mylanping.sh 10.95.1 \033[1;91m1\033[0m 254"';
        exit 1;
fi
if [ -z $3 ]
then
        echo -e "\033[1;91mLast ip is missing\033[1;91m";
        echo -e 'Example "./mylanping.sh 10.95.1 1 \033[1;91m254\033[0m"';
        exit 1;
fi

clear
echo -e "\033[1;95mNetwork ping from\033[0m \033[1;92m$lan .1\033[0m to \033[1;92m$lan .254\033[0m:";
for ip in $(seq $2 $3);
do
        REP=`ping -c 1 -W 0 $lan.$ip| tail --lines 1|cut -d '/' -f 6`;
        if [ -z $REP ]
        then
                echo -e "\033[4;31mNo response from\033[0m \033[1;91m$lan.$ip\033[0m";
        else
                echo -e "\033[4;32mtime to resp :\033[0m \033[4;34m$REP\033[0m from \033[1;94m$lan.$ip\033[0m";
        fi
done

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: