Extrct IP from ping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extrct IP from ping
# 1  
Old 05-04-2008
Extrct IP from ping

Hi guys,
I'm looking for a script to extract the IP address reported by the command:

$> ping -c 1 192.168.1.254
PING 192.168.192.254 (192.168.192.254) 56(84) bytes of data.
64 bytes from 192.168.192.254: icmp_seq=1 ttl=64 time=139 ms

--- 192.168.192.254 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 139.136/139.136/139.136/0.000 ms

What I have to get is the value after the "64 bytes from" and only the reported IP not the data after the ":".

In my real case the ping will be executed on broadcast address as 192.168.192.255.

I'm using a BASH script.
Thanks in advance
# 2  
Old 05-04-2008
play around with grep/awk and you'll have a solution
$ > ping -c 1 192.168.1.254 | grep "64 bytes from"|awk '{print $4}'
# 3  
Old 05-04-2008
Hammer & Screwdriver

You can also check with nmap whole network

nmap -sP 192.168.1.1-254 | grep Host | awk '{print $2}'

Regards,
Bash
# 4  
Old 05-04-2008
Hammer & Screwdriver

For single query of ping

ping -c 1 192.168.1.156 | grep "64 bytes from " | awk '{print $4}' | cut -d":" -f1

192.168.1.156


Regards,
Bash
# 5  
Old 05-04-2008
Quote:
Originally Posted by learnbash
ping -c 1 192.168.1.156 | grep "64 bytes from " | awk '{print $4}' | cut -d":" -f1
Your example show the Useless Use of grep and cut when awk can do all.
Code:
ping -c 1 192.168.1.156 | awk -F" |:" '/from/{print $4}'

Regards,
Dan
# 6  
Old 05-04-2008
With awk only:

Code:
ping -c 1 192.168.1.254 | awk -F'[ :]' 'NR==2 { print $4 }'

# 7  
Old 05-04-2008
Hammer & Screwdriver

Quote:
Originally Posted by danmero
Your example show the Useless Use of grep and cut when awk can do all.
Code:
ping -c 1 192.168.1.156 | awk -F" |:" '/from/{print $4}'

Regards,
Dan
Hi Dan,

I just gave the alternate method, as you see that was my second reply, but any way thanks for your comments.

Regards,
Bash
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

How to get reason for ping failure using perls Net::Ping->new("icmp");?

Hi I am using perl to ping a list of nodes - with script below : $p = Net::Ping->new("icmp"); if ($p->ping($host,1)){ print "$host is alive.\n"; } else { print "$host is unreacheable.\n"; } $p->close();... (4 Replies)
Discussion started by: tavanagh
4 Replies

3. IP Networking

PING

I am unable to ping my remote server.My server is unable to ping the same. both are able to ping the gateway. both the ip's are on same network.i use a proxy tunnel on my remote server.Help if any clues. (6 Replies)
Discussion started by: oslbhavana
6 Replies

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

5. IP Networking

Can't Ping

I have an old MP-Ras Unix system. I have setup netowkring but am unable to ping any local network pcs or the default gateway. If i use the arp -a command I receive the correct mac address for all connected pcs but I cannot ping anything except the local address. Any help would be appreciative. ... (7 Replies)
Discussion started by: Rutgerncas
7 Replies

6. Linux

Not able to ping

Hi All, Need your help one more time. I am trying to ping a linux machine which is not responding to ping. However traceroute can reach the machine and I can log in to it by ssh. I have checked /proc/sys/net/ipv4/icmp_echo_ignore_all it is already set as "0". It is not happening in the... (1 Reply)
Discussion started by: ailnilanjan
1 Replies

7. UNIX for Dummies Questions & Answers

Ping

Hi , I have one system installed on Linux Red Hat 3.0. I have ip of system 3.156.168.*** and i want to ping some port that is on this IP which command i can do this? sam70 (1 Reply)
Discussion started by: sam70
1 Replies

8. AIX

ping

We can ping out but the return never comes back even to localhost ping localhost PING loopback: (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=0 ms it just sits there.... any ideas, any files to check (1 Reply)
Discussion started by: csaunders
1 Replies

9. UNIX for Dummies Questions & Answers

ping

Hi there hope list can help I'm looking for a command that does the following lets say i ping a server 00.00.000.00 and this server is up and running how do i get the ping command to return a UP or a Down meaning if the command is true do this if the command is False do this ... (2 Replies)
Discussion started by: nemex
2 Replies

10. IP Networking

About Ping

Hi, When I pinged a machin, I got to be seen a different kind of reply from that machine. This is as below : "Reply from 136.128.11.116: Source quench received." Which I felt like an un-usual message. So what does this mean. Regards & Thanks in advance. Vishwa. (4 Replies)
Discussion started by: S.Vishwanath
4 Replies
Login or Register to Ask a Question