LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2009, 11:26 PM   #1
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Rep: Reputation: 30
How to ping and return only the IP address?


Is there a simple way to return an IP by pinging a hostname? Ideally it takes low resources because I have a lot of hostnames to ping!

Thanks in advance
 
Old 05-04-2009, 11:50 PM   #2
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Is your objective to determine the IP, because if so, you should relly use a tool devised for that task such as nslookup or dig. That said, pinging a hostname will normally return an IP, so I don't quite see what you are asking.
 
Old 05-05-2009, 12:03 AM   #3
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Original Poster
Rep: Reputation: 30
I'd like a command that returns ONLY the IP, so something that parses the mumbo jumbo results and outputs only:
1.2.3.4

Is what I'm asking for
 
Old 05-05-2009, 01:00 AM   #4
Neminath
LQ Newbie
 
Registered: Jan 2009
Posts: 13

Rep: Reputation: 1
You can use nmap it has nice features for ping scan of network
it gives lot of other information but u can use a simple perl script to filter out what u want
 
Old 05-05-2009, 01:25 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have and know Python and able to use it
Code:
#!/usr/bin/env python
import socket
for hosts in open("file"):
    hosts=hosts.strip()
    try:
        print "doing ",hosts,socket.gethostbyname(hosts)
    except Exception,e:
        print e
output:
Code:
# more file
www.google.com
www.abc.com
www.test.com
localhost
# ./test.py
doing  www.google.com 216.239.61.104
doing  www.abc.com 199.181.132.250
doing  www.test.com 205.178.152.103
doing  localhost 127.0.0.1
 
1 members found this post helpful.
Old 05-05-2009, 01:39 AM   #6
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Original Poster
Rep: Reputation: 30
Hmm.. i'm sure there's an awk or sed one liner.. or perl 1 liner to get this done.. that looks like it could be server intensive..
 
Old 05-05-2009, 02:10 AM   #7
guest
Member
 
Registered: May 2003
Distribution: CentOS 5 64 bit
Posts: 255

Original Poster
Rep: Reputation: 30
for example:
ping -n -q -c1 yahoo.com |grep '(' | egrep -o '[0-9.]+'

but it's returning:
.
209.191.93.53
56
84
.


I just need the:
209.191.93.53
 
Old 05-05-2009, 02:39 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by guest View Post
that looks like it could be server intensive..
definitely not.
 
Old 05-05-2009, 03:55 AM   #9
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Try

dig +short www.example.com | sed -e '1d'

This will still give you multiple IPs where valid (for example, www.google.com)
 
Old 05-05-2009, 09:59 AM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
A one-liner that depends on the format of the reply from ping:
Code:
ping -c 1 some.ip.name | perl -e '$header=<>;$header=~m/ \(([^)]+)\) /;print $1;'
Tested on RHEL4.
--- rod.
 
Old 05-05-2009, 10:10 AM   #11
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Thumbs up

Quote:
Originally Posted by guest View Post
Hmm.. i'm sure there's an awk or sed one liner.. or perl 1 liner to get this done.. that looks like it could be server intensive..
Another, one liner.

Make a file called list contains all hostnames.

and run this

Code:
for i in `cat list`;
do
ping -c 1 $i | head -1 | awk -F "(" '{print $2}' | awk -F ")" '{print $1}'
done;
Tested on RHEL3 and RHEL4
 
Old 05-05-2009, 10:40 AM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
'host' anyone ?

Code:
host yahoo.com
yahoo.com has address 69.147.114.224
yahoo.com has address 209.191.93.53
yahoo.com has address 209.131.36.159
yahoo.com mail is handled by 1 c.mx.mail.yahoo.com.
yahoo.com mail is handled by 1 d.mx.mail.yahoo.com.
yahoo.com mail is handled by 1 e.mx.mail.yahoo.com.
yahoo.com mail is handled by 1 f.mx.mail.yahoo.com.
yahoo.com mail is handled by 1 g.mx.mail.yahoo.com.
yahoo.com mail is handled by 1 a.mx.mail.yahoo.com.
yahoo.com mail is handled by 1 b.mx.mail.yahoo.com.
One can't expect just one IP address.
 
Old 05-05-2009, 07:40 PM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by vikas027 View Post
Another, one liner.

Make a file called list contains all hostnames.

and run this

Code:
for i in `cat list`;
do
ping -c 1 $i | head -1 | awk -F "(" '{print $2}' | awk -F ")" '{print $1}'
done;
Tested on RHEL3 and RHEL4
no need that much piping
Code:
ping -c 1 $i | awk 'NR==1{gsub(/\(|\)/,"",$3);print $3}'
 
Old 05-07-2009, 11:46 AM   #14
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Smile

Quote:
Originally Posted by ghostdog74 View Post
no need that much piping
Code:
ping -c 1 $i | awk 'NR==1{gsub(/\(|\)/,"",$3);print $3}'
Hi ghostdog, I am not very expert in scripting. I know my codes are not optimized But yes I can make things work. I know what I do in 4-5 lines is very much possible in single line by might utilities like sed/awk... I am still in process of learning

Could you please explain the awk line you used here ?

Thanks in adv.
 
Old 05-07-2009, 02:06 PM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Why does IP address has to be returned through 'ping' in the first place ? I.e. why not to resolve hosts without 'ping' ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Return MAC Address with Ping Critcho Linux - Networking 16 05-04-2011 11:40 AM
Ping Return Remote Host Information qwerty_tele Linux - Security 3 11-24-2008 11:05 AM
can't ping local IP address but can ping remote hosts rob_xx17 Linux - Networking 4 12-02-2006 08:39 AM
Return true or false if I have ping reply Menestrel Programming 4 11-29-2004 12:40 AM
windows 98 m/c ping to ip address of red hat server but fails to ping hostname ravilohot Linux - Networking 2 09-07-2004 04:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration