Using AWK for ping and print..


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using AWK for ping and print..
# 1  
Old 09-29-2005
Using AWK for ping and print..

I'm using a hosts file, formatted like this:

xxx.xxx.xxx.xxx dnsname #comment

Current command is:

for host in 'awk '/^[0-9]/ { print $1 }' etc/hosts'
do
ping $host 1 2>&1 | \
sed 's/.*no answer.*/[31;1m&[0m/;s/.*is alive.*/[32;2m[0m/'
done

(no answer returns red, is alive green)

How can I ping the IP, and have it return xxx.xxx.xxx.xxx is alive, along with the dnsname AND comment?

"xxx.xxx.xxx.xxx is alive dnsname #comment" for the output when just pinging the IP? Is this possible?

Using Solaris 5.5. Not a big Unix geek by any measure. Just trying to enhance a ping script a bit.

Thanks in advance...
# 2  
Old 09-29-2005
Code:
#!/bin/ksh

hosts='/etc/hosts'

while read ip host theRest
do
   ping "${ip}" 1>/dev/null 2>&1
   [[ "${?}" -eq 0 ]] && echo "${ip} is alive ${host} ${theRest}"
done < "${hosts}"

# 3  
Old 09-29-2005
Vgersh99,

Thanks for replying. I've put in your snippet, and it works as long as the IP ping is "alive". For a bad ping, there's no output on the screen. Is there a way to add that? Our main goal is to quickly identify a bad ping, which with the prior code came out red.

Also, I added in the color snippet after ${theRest}" using a |. Now the entire printout is green. I'd assume that a bad one would be all red?

Thanks again!
# 4  
Old 09-29-2005
Code:
#!/bin/ksh

hosts='/etc/hosts'

while read ip host theRest
do
   ping "${ip}" 1>/dev/null 2>&1
   [[ "${?}" -eq 0 ]] && echo "${ip} is alive ${host} ${theRest}" || echo "${ip} is dead ${host} ${theRest}"
done < "${hosts}"

# 5  
Old 09-29-2005
I added the color snippet, but only the red works on the failed pings (no green for the good pings). Is there a way to make the good pings green and the bad ones red? Have played around with the code and only get one color to show, either on the bad or good pings, but not both. Seems that the last argument is the only one to read the color code?

#!/bin/ksh

hosts='/etc/hosts'

while read ip host theRest
do
ping "${ip}" 1>/dev/null 2>&1
[[ "${?}" -eq 0 ]] && echo "${ip} is alive ${host} ${theRest}" || echo "${ip} IS DEAD!!! ${host} ${theRest}"
sed 's/.*IS DEAD!!!.*/[31;2m[0m/;s/.*is alive.*/[32;1m&[0m/'


done < "${hosts}"

Again, TIA.
# 6  
Old 09-29-2005
Never mind...figured it out. Just entered the color code before the double pipe and at the end of the second argument.


Thanks again!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Pattern Matching for PING tests

I have a script that logs into a server and pings several other servers in order to verify IP path between servers. The output can look like this, if good pings: Response from 1.1.1.4;_id=0, vlan_prio=0): seq=0 time=91.547 ms. Response from 1.1.1.4;_id=0, vlan_prio=0): seq=1 time=61.176 ms.... (7 Replies)
Discussion started by: he204035
7 Replies

2. Programming

Ping test sends mail when ping fails

help with bash script! im am working on this script to make sure my server will stay online, so i made this script.. HOSTS="192.168.138.155" COUNT=4 pingtest(){ for myhost in "$@" do ping -c "$COUNT" "$myhost" &&return 1 done return 0 } if pingtest $HOSTS #100% failed... (4 Replies)
Discussion started by: mort3924
4 Replies

3. UNIX and Linux Applications

Script help with awk/ping

I need to ping a host website ...like facebook.com and out the seconds of delay for 5 websites ..I need help writing a script that will print the bold PING facebook.com (173.252.90.36) 56(84) bytes of data. 64 bytes from edge-star-mini-shv-13-atn1.facebook.com (173.252.90.36): icmp_req=1... (1 Reply)
Discussion started by: 5sku5
1 Replies

4. Shell Programming and Scripting

Need shell script to Telnet multiple node , Ping some IP and print output

Hi Team, Need shell script to Telnet multiple node , Ping some IP and print output like pass or fail. Need this script to check reachability of multiple nodes at same time. Help me. I use this but not working... Eg. in this script i need to telnet... (4 Replies)
Discussion started by: Ganesh Mankar
4 Replies

5. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

6. Shell Programming and Scripting

Animation Ping on Solaris Like Cisco Ping

Hi, I develop simple animation ping script on Solaris Platform. It is like Cisco ping. Examples and source code are below. bash-3.00$ gokcell 152.155.180.8 30 Sending 30 Ping Packets to 152.155.180.8 !!!!!!!!!!!!!.!!!!!!!!!!!!!!!. % 93.33 success... % 6.66 packet loss...... (1 Reply)
Discussion started by: gokcell
1 Replies

7. Shell Programming and Scripting

Router ping log extract data from it Awk/Sed/grep

Hi, I am new to this world.. Using expect i loging to router and checking ping response to my links. I need to genarate report using this output and that report contains only three file link name, packet loss, latency. my output of script is like below: -bash-3.00$ monmw/mwbkp... (2 Replies)
Discussion started by: jkmistry
2 Replies

8. Shell Programming and Scripting

Help with Ping Script and AWK

Hey Guys, First time posting here. I keep getting back here on my google searches to help me in my scripting quest. Can i ask for some guidance please? I acquired a script, from these forums in fact that pings a list of IPs/Hostnames. Works a treat. Echos results to a file with the value... (3 Replies)
Discussion started by: stacky69
3 Replies

9. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

10. Shell Programming and Scripting

How do I get awk to print a " in it's print part?

The line is simple, use " '{ print $1"]"$2"\"$3THE " NEEDS TO GO HERE$4 }' I've tried \", "\, ^" and '"" but none of it works. What am I missing? Putting in the [ between $1 and $2 works fine, I just need to do the same with a ". Thanks. (2 Replies)
Discussion started by: LordJezo
2 Replies
Login or Register to Ask a Question