LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   shell script (https://www.linuxquestions.org/questions/linux-server-73/shell-script-4175459780/)

sagar666 04-27-2013 08:01 AM

shell script
 
Hi,

i want to see which ip addresses are live and which ip addresses are dead so that i write a shell script as shown in below

#/bin/bash
ping=1
echo -e "\nenter ip range exampie is 10.10.3.:\c "
read number
while [ $ping -le 10 ]
do
ping -c 2 $number$ping
(( ping++ ))
done
but now i want save the file separately live and dead ip address

rigor 04-27-2013 11:42 AM

sagar666,

You can check the manual page for ping on your distro, or just manually check the return code from the ping command. But on the distro I'm using, ping returns zero if successful, non-zero otherwise. So I would tend to do something like the following.


Code:

#!/bin/bash

echo -e "\nEnter base ip address, for example 10.10.3.:  \c "
read number

>  alive
>  dead

for ((  ping=1 ;  ping <= 10 ;  ping++  ))
    do

        ping -c 2 $number$ping

        if [[  $?  -eq  0  ]]
            then
                echo  $number$ping  >>  alive
            else
                echo  $number$ping  >>  dead
        fi

    done

So you'll get a list of the "live" IP addresses in the file named "alive" and those that are "dead" in the file named "dead".

I also might add something to check the format of the input value before using it.

hyperdaz 04-27-2013 11:47 AM

Job of nmap?

Code:

nmap -sP 10.10.3.0/25 > alive
will show all up hosts everything else is down

sagar666 04-28-2013 05:02 AM

Thanks rigor and hyperdaz

one more thing if host machine is in off it wont ping and it ip address goes to dead file. i want know only unassigned ip addresses even machines is in off ???

hyperdaz 04-28-2013 06:20 AM

Hi Sagar666,

I take it you have a full list of all hosts on the network, as any tool would not know if its "OFF" or "not assigned", ping or pretty much any networking tool is looking at what is turned ON.

As you are only scanning 10 IPs from your original script
Quote:

[ $ping -le 10 ]
or
Quote:

<= 10
this should be quite a fast check :) worth doing manually once or so even if this is a large network its probably worth the time to do a physical check make sure labelling is correct that you have all serial numbers and possibly warranty or support contracts or other licencees.

You can probably find most serial numbers of machines that are on with "dmesg"

If you are going to test the whole network something like 128 or 256 range then ping is going to take quite a long time not sure if that's an issue, nmap will take a much shorter time.

If you do have a work network or a large network then you might want to set up a monitoring tool this will check many different things for you non-stop i.e. something like Nagios Core http://www.nagios.org or (M)/Monit http://mmonit.com.

hdaz

sagar666 04-28-2013 06:31 AM

Hi hdaz,

if i try to nmap -sP 10.10.3.0/25 > alive getting error nmap command not found
in redhat 6.1

sagar

hyperdaz 04-28-2013 06:33 AM

Just to confirm that is RHEL 6.1? or redhat 6.1 the latter was dated about 1996 and RHEL was a couple of years ago

to be sure run:
Code:

cat /etc/redhat-release
I'll take it it's RHEL 6.1 is there any reason why your still on 6.1? 6.4 is the latest version there might be logical reasons for not upgrading if this is a locked down network but normally it is highly recommended to update to the latest to get full security fixes/patches etc.

to install nmap on RHEL
Code:

up2date install nmap
or if yum is installed i.e. CentOS rather than RHEL

Code:

yum install nmap
I will also add that if this is a corporate network then one might be restricted to install applications such as nmap.

Nmap is a very powerful tool that can gather a lot of information with different optoins etc, then if you are not the Admin of the network/servers/VMs/desktops then you might want to ask for permission first.

Hdaz

chrism01 04-28-2013 07:35 AM

Quote:

to install nmap on RHEL....
Actually, Centos is a rebuild of RHEL, and RHEL switched to yum as of 5.0.
Later versions of v4 (4.8, 4.9 ??) also had the option to install and use yum instead of up2date.

sagar666 04-28-2013 07:49 AM

I installed nmap .Now it is working fine and also very powerful tool.we can verify all up ips with a seconds
Thank u very much


All times are GMT -5. The time now is 03:37 AM.