Operating System - HP-UX
1745833 Members
4273 Online
108723 Solutions
New Discussion

Re: ping status (extracting hostname)

 
madhucertify
Advisor

ping status

Hi

 

i am writing script for checking the ping status for all servers from centralized server.

i am using the below script and there is no problem in the script
---------------------------------------------------------------------

cat pingcheck.sh

for HOST in `cat /home/madhu/userdetail/hostdetails.txt`
do
echo "  "
ping $HOST -n 5 > /home/madhu/log/All.log

if [ $? -ne 0 ]; then
echo "host $HOST not responding" > /home/madhu/log/failure.log
STAT="*Not Responding*"
else
echo "host $HOST responding" > /home/madhu/log/success.log
STAT="Responding"
fi
echo  "${HOST} ${STAT}"
done
echo " "
echo " "
echo "  Enter any Key:\c"
read dmy
--------------------------------------------------------------------

$ cat /home/madhu/userdetail/hostdetails.txt
Netwok segment-1
host1
host2

---------------------------------------------------------------------

 

but I need to do few modifications as below,

we have more than 10 different network.

 

so we can't have 10 hostlist files  --> /home/madhu/userdetail/hostdetails.txt

 

but as per the above script,I have to use 10 different host files.

 

but my thought is to have something like this

 

$ cat /home/madhu/userdetail/hostdetails.txt
[Netwok segment-1]
host1
host2
[Netwok segment-2]
host3
host4

 

I have to do some modification in script while refering to hostdetails.

 

I want to use one hostfile entry for all networks.

 

so when I check ping status for Netwok segment-1,script should refer only to hosts matching
to pattern [Netwok segment-1]

 

need help in modifying the script based on the pattern matching from referring
to single host file entry

 

 

--Madhu

9 REPLIES 9
Dennis Handly
Acclaimed Contributor

Re: ping status (extracting hostname)

You need to use proper indentation so you can understand your script.  Also remove evil cats:

for HOST in $(< /home/madhu/userdetail/hostdetails.txt); do
   echo
   ping $HOST -n 5 > /home/madhu/log/All.log

 

Your log will only have the last output, unless you append.

 

>$ cat /home/madhu/userdetail/hostdetails.txt
Network segment-1
host1

Did you want to process this first line or skip it?

 

$ cat /home/madhu/userdetail/hostdetails.txt
[Network segment-1]
host1
host2
[Network segment-2]
host3
host4

 

It would be much easier to process if you had it like:

[Network segment-1] host1
[Network segment-1] host2
[Network segment-2] host3
[Network segment-2] host4

 

Do you literally have "[Network segment-2]"  Or some other name but with that punctuation?

But if you want to process your format you could do:

awk -v segment="Network segment-2" '

BEGIN { found = 0 }
substr($0, 1, 1) == "[" {  # start of segment
   if ($0 ~ segment) {
      found = 1
      next
   }
   if (found) exit # no more
   next
}
found {
   print $0
}' /home/madhu/userdetail/hostdetails.txt | while read HOST; do

   echo
   ping $HOST -n 5 > /home/madhu/log/All.log

...

done

madhucertify
Advisor

Re: ping status

 

 

Hi

 

Modified Script :

------------------------------------------------------------------

awk -v segment="Network segment-2" '

BEGIN { found = 0 }
substr($0, 1, 1) == "[" {  # start of segment
if ($0 ~ segment) {
found = 1
next
}
if (found) exit # no more
next
}
found {
print $0
}' /home/madhu/userdetail/hostdetails.txt | while read HOST; do

echo ping $HOST -n 5 > /home/madhu/log/All.log
done

----------------------------------------------------

 

 

see the below output:


$ sh -x pincheck.sh
+ awk -v segment=Network segment-2

BEGIN { found = 0 }
substr($0, 1, 1) == "[" {  # start of segment
if ($0 ~ segment) {
found = 1
next
}
if (found) exit # no more
next
}
found {
print $0
} /home/madhu/userdetail/hostdetails.txt
+ read HOST

*********************************************************

 

No log is appended to /home/madhu/log/All.log

 

I guess host file is not reading correctly from /home/madhu/userdetail/hostdetails.txt.

 

>>Did you want to process this first line or skip it?

 

I want to skip.

 

there are so many Data Center and each data center has many network name.

 

so I have to check ping status for each network in each Data Center.

 

so when I am checking ping status for Data Center1 and network segment1,other hosts entry in different network

should not be taken as input from hostfile.

 

>>Do you literally have "[Network segment-2]"  Or some other name but with that punctuation?

 

"[Network segment-2]"---- I gave it for Example.

 

originally it will be as something like IDC-Secure SER

 

IDC-Data Center Name

 

similarly I have IDC-Secure None and with other names.

Dennis Handly
Acclaimed Contributor

Re: ping status (extracting hostname)

>I guess host file is not reading correctly from /home/madhu/userdetail/hostdetails.txt.

 

I need to see a more realistic version of that file.  I'm assuming there are "[" in column 1.

madhucertify
Advisor

Re: ping status

HI

 

[Network segment-1] host1
[Network segment-1] host2
[Network segment-2] host3
[Network segment-2] host4

 

>>I need to see a more realistic version of that file.  I'm assuming there are "[" in column 1

 

[IDC-Secure SER] tes00101

[IDC-Secure SER] tes00201

[EDC-SER] pgn00101

 

-- madhu

 

 

Dennis Handly
Acclaimed Contributor

Re: ping status (extracting hostname)

>[IDC-Secure SER] tes00101

 

My original script works on your original format.  Try this one:

awk -v segment="IDC-Secure SER" '

$0 ~ segment { print $NF }

' /home/madhu/userdetail/hostdetails.txt | while read HOST; do
   ping $HOST -n 5 > /home/madhu/log/All.log

...

madhucertify
Advisor

Re: ping status

Hi Dennis,

 

Your sugesstion worked fine.

 

I modified the script as below,

 

-------

>/home/madhu/log/Pingstatus.log
awk -v segment="IDC-Secure SER" '

$0 ~ segment { print $NF }

' /home/madhu/userdetail/hostdetails.txt | while read HOST; do
ping $HOST -n 5 > /home/madhu/log/All.log

if [ $? -ne 0 ]; then
echo "host $HOST not responding"
STAT="\033[41m\033[37m *Failure* \033[0m"
else
echo "host $HOST responding"
STAT="*Success*"
fi
echo  "|${HOST} ${STAT}|"
echo  "|${HOST} ${STAT}|" >> /home/madhu/log/pingstatus.log
done
echo " "
echo " "
echo "  Enter any Key:\c"
read dmy

 

---------------------

 

above script is working fine and no problem in it .

 

but there is some changes in my host file enrty -> /home/madhu/userdetail/hostdetails.txt

 

previously it was

 

[IDC-Secure SER] tes00101

 

but now as per recommendation my host file should be like as below ,

 

---------------------------------------------------------------------------------------

 

name:details:NO:ipaddress:hostname:status:network

 

tes00101:prod:NO:10.01.23.45:tes00101:production:IDC-Secure SER

---------------------------------------------------------------------------------------

 

as per the original script,host name should be greped based on the network area,but I also need to display the sixth field in the ouput along with the hostname and ping status.

 

original script output will be as similar to below

 

tes00101 *success*

 

but now,I have to make changes in the output as below,

 

--------------------------------------------------------------------------------------------------

tes00101 *success* prodction (sixth field should be taken from host entry file.

---------------------------------------------------------------------------------------------------

 

-madhu

 

 

 

Dennis Handly
Acclaimed Contributor

Re: ping status (extracting hostname)

>tes00101:prod:NO:10.01.23.45:tes00101:production:IDC-Secure SER

>I also need to display the sixth field in the output along with the hostname and ping status.

 

awk -F: -v network="IDC-Secure SER" '

$0 ~ network { print $5, $6 }

' /home/madhu/userdetail/hostdetails.txt | while read HOST H_STATUS; do

...

   echo  "${HOST} ${STAT} ${H_STATUS}"

madhucertify
Advisor

Re: ping status

HI Dennis,

 

1.awk -F: -v network="IDC-Secure SER" '

2.$0 ~ network { print $5, $6 }

3.' /home/madhu/userdetail/hostdetails.txt | while read HOST H_STATUS; do

 

-->above changes worked fine.

 

can you explain me what you have done.

 

 

awk -F: -v network="IDC-Secure SER" '-->it will grep the filed matching with IDC-Secure SER.

 

$0 ~ network { print $5, $6 }  ->

 

$0 --> it refers to the file name (/home/madhu/userdetail/hostdetails.txt)

 

~ -> refers to the parent dir.

 

but i am not sure with the second line.

 

Can you explain it ?

 

-madhu

 

Dennis Handly
Acclaimed Contributor

Re: ping status (extracting hostname)

>awk -F: -v network="IDC-Secure SER" '-->it will grep the field matching with IDC-Secure SER.

>$0 ~ network { print $5, $6 }  ->
 
$0 is the whole line, when I didn't know your exact file format.  For your new format we should use $7.
 
~  Is the match (ERE) operator.  If you want an exact match, you can just use:

$7 == network