Re: ping status (extracting hostname) (902 Views)
Reply
Advisor
madhucertify
Posts: 21
Registered: ‎09-01-2013
Message 1 of 10 (1,000 Views)

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

Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 2 of 10 (980 Views)

Re: ping status (extracting hostname)

[ Edited ]

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

Advisor
madhucertify
Posts: 21
Registered: ‎09-01-2013
Message 3 of 10 (966 Views)

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.

Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 4 of 10 (956 Views)

Re: ping status (extracting hostname)

[ Edited ]

>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.

Advisor
madhucertify
Posts: 21
Registered: ‎09-01-2013
Message 5 of 10 (953 Views)

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

 

 

Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 6 of 10 (941 Views)

Re: ping status (extracting hostname)

[ Edited ]

>[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

...

Advisor
madhucertify
Posts: 21
Registered: ‎09-01-2013
Message 7 of 10 (924 Views)

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

 

 

 

Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 8 of 10 (920 Views)

Re: ping status (extracting hostname)

[ Edited ]

>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}"

Advisor
madhucertify
Posts: 21
Registered: ‎09-01-2013
Message 9 of 10 (912 Views)

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

 

Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 10 of 10 (902 Views)

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

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.