Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Q: How get current IP addres in shell?

Q: How get current IP addres in shell?
Thread Tools
Dedicated MacNNer
Join Date: Feb 2003
Status: Offline
Reply With Quote
Jul 18, 2003, 07:49 AM
 
Hi,

I hate to keep looking up my current dynamic IP address in System preferences, There has to be a clean shell way. The best I can do so far is:

ifconfig -u | grep netmask

But that gives me other info. Anything that just provides the currnt IP address?

Thanks.
     
Senior User
Join Date: Jan 2001
Location: Mahwah, NJ USA
Status: Offline
Reply With Quote
Jul 18, 2003, 08:12 AM
 
Originally posted by jgift:
Hi,

I hate to keep looking up my current dynamic IP address in System preferences, There has to be a clean shell way. The best I can do so far is:

ifconfig -u | grep netmask

But that gives me other info. Anything that just provides the currnt IP address?

Thanks.
Try:

ipconfig getifaddr en0

that should return the IP address of interface en0 and nothing else.
-DU-...etc...
     
jgift  (op)
Dedicated MacNNer
Join Date: Feb 2003
Status: Offline
Reply With Quote
Jul 18, 2003, 11:33 AM
 
Thanks but I get a kernel error... I used ifconfig and some grep and awk statements to finally get it.

Would have been cleaner your way
     
Senior User
Join Date: Jul 2000
Status: Offline
Reply With Quote
Jul 18, 2003, 03:42 PM
 
Originally posted by jgift:
Thanks but I get a kernel error... I used ifconfig and some grep and awk statements to finally get it.

Would have been cleaner your way
Try running "ipconfig getifaddr en1". I got an error as well, but I'm not on eno (Ethernet) as I'm using my built-in AirPort card (en1).
     
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status: Offline
Reply With Quote
Jul 18, 2003, 06:50 PM
 
Originally posted by jgift:
Thanks but I get a kernel error... I used ifconfig and some grep and awk statements to finally get it.
   awk can do it all by it's lonesome. Try the following:

ifconfig en0 | awk '/inet /{print $2}'
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
     
jgift  (op)
Dedicated MacNNer
Join Date: Feb 2003
Status: Offline
Reply With Quote
Jul 19, 2003, 12:38 AM
 
Thanks for the help...

1. Tried ipconfig getifaddr en1 but got the same:

get if addr en1 failed, (os/kern) failure

Tried it as PPP0 since I have an ethernet ADSL modem here. No go either.

2. I had an awk statement that did it great:

ifconfig -a | grep "inet" | grep "netmask" | grep -v "127.0.0.1" | awk {'print $2'}

But the new one posted does it in half the space... very clever. Thanks Gary!

I just replaced:

ifconfig en0 | awk '/inet /{print $2}'

with

ifconfig ppp0 | awk '/inet /{print $2}'
(Last edited by jgift; Jul 19, 2003 at 08:42 AM. )
     
Mac Enthusiast
Join Date: Sep 2000
Location: France
Status: Offline
Reply With Quote
Jul 19, 2003, 07:36 PM
 
What if you're behind a router ? Does anyboy konw how I can get my public IP (i.e. my Basestation's IP) from the Terminal ?
     
Senior User
Join Date: Jan 2001
Location: california
Status: Offline
Reply With Quote
Jul 19, 2003, 10:55 PM
 
Originally posted by Axel:
What if you're behind a router ? Does anyboy konw how I can get my public IP (i.e. my Basestation's IP) from the Terminal ?
you'll have to get it in the airport admin utility, i'm pretty sure. if you always leave it on, then that ip shouldn't change, in which case you can write down the ip or put it in a text file :c)
     
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status: Offline
Reply With Quote
Jul 20, 2003, 09:09 AM
 
Originally posted by Axel:
What if you're behind a router ? Does anyboy konw how I can get my public IP (i.e. my Basestation's IP) from the Terminal ?
   Yes, simply execute the following:

curl -s http://simple.showmyip.com
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
     
Senior User
Join Date: Oct 2000
Location: Midwest
Status: Offline
Reply With Quote
Jul 20, 2003, 09:34 AM
 
Substitute en1 for en0 if you are connected via an airport card.
Code:
ifconfig en0|awk '/inet /{print $2}'
HTH
Craig
     
jgift  (op)
Dedicated MacNNer
Join Date: Feb 2003
Status: Offline
Reply With Quote
Jul 20, 2003, 09:46 AM
 
Time to kill and playing around. This works also for me ...

tail /tmp/ppp.log | awk '/local /{print $10}'
     
Dedicated MacNNer
Join Date: Dec 2002
Location: someplace
Status: Offline
Reply With Quote
Jul 20, 2003, 04:05 PM
 
Originally posted by Gary Kerbaugh:
   Yes, simply execute the following:

curl -s http://simple.showmyip.com
Gary, have you had any problems lately with the showmyip.com domain? I have had trouble getting their subdomains to resolve properly lately. Perhaps it is just my DNS servers at fault. I'll do some experiments with some remote shell accounts and see if it's a local issue or net-wide. In the meantime, I have a broken script or two to deal with!
     
Dedicated MacNNer
Join Date: Dec 2002
Location: someplace
Status: Offline
Reply With Quote
Jul 20, 2003, 04:14 PM
 
Well, I've tried shell accounts in California, Georgia, and Massachusetts with the same results:
Code:
bash-2.05$ host -v simple.showmyip.com Trying "simple.showmyip.com" Host simple.showmyip.com not found: 3(NXDOMAIN) Received 37 bytes from 64.224.20.156#53 in 115 ms bash-2.05$ lynx -dump http://simple.showmyip.com Looking up simple.showmyip.com simple.showmyip.com Unable to locate remote host simple.showmyip.com. Alert!: Unable to connect to remote host. lynx: Can't access startfile http://simple.showmyip.com/ bash-2.05$ curl -s http://simple.showmyip.com bash-2.05$
Time to fall back to the older, slightly less efficient:
curl -s http://checkip.dyndns.org/ | awk '/Current IP Address:/ {print $4}'
     
Mac Enthusiast
Join Date: Jun 2000
Location: New Jersey, USA
Status: Offline
Reply With Quote
Jul 21, 2003, 04:32 PM
 
Is there some reason why I wouldn't have ipconfig on my machine? For that matter, I can't find ping either, except as part of Network Utility.app. Aren't these standard, or is there some package I need to install?
     
jgift  (op)
Dedicated MacNNer
Join Date: Feb 2003
Status: Offline
Reply With Quote
Jul 22, 2003, 07:33 AM
 
They should be standard. have you the BSD package installed?
     
Mac Enthusiast
Join Date: Jun 2000
Location: New Jersey, USA
Status: Offline
Reply With Quote
Jul 22, 2003, 10:13 AM
 
I'm not sure what you mean by "BSD Package". I've got 10.2.6 with dev tools installed.

Where are ipconfig and ping normally found?
     
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status: Offline
Reply With Quote
Jul 22, 2003, 10:26 AM
 
Originally posted by gatorparrots:
Gary, have you had any problems lately with the showmyip.com domain? I have had trouble getting their subdomains to resolve properly lately. Perhaps it is just my DNS servers at fault. I'll do some experiments with some remote shell accounts and see if it's a local issue or net-wide. In the meantime, I have a broken script or two to deal with!
Hi gatorparrots,
   Forgive the late reply; I check this list less frequently than my normal haunt. (and you know where that is) I've had no problems but I don't use it regularly. However, I have used it several times since this post and it's worked fine. Wasn't it you that told me about it? I remember that there was another site mentioned in the same thread at Apple. It required an awk filter as it included HTML but it worked. Unfortunately I don't remember what it was and the thread appears to have been archived.
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
     
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status: Offline
Reply With Quote
Jul 22, 2003, 10:34 AM
 
Originally posted by neilw:
Is there some reason why I wouldn't have ipconfig on my machine? For that matter, I can't find ping either, except as part of Network Utility.app. Aren't these standard, or is there some package I need to install?
   ipconfig is installed by the Essentials.pkg installer package in /usr/sbin/ipconfig and ping is installed by the BaseSystem.pkg installer package in /sbin/ping. Therefore the discussion about the BSD subsystem is moot. If you look in the MacOS X installer disk 1 you can find these installer packages and use Pacifist to pull out the files you need.
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
     
Dedicated MacNNer
Join Date: Dec 2002
Location: someplace
Status: Offline
Reply With Quote
Jul 22, 2003, 11:05 AM
 
Originally posted by Gary Kerbaugh:
Hi gatorparrots,
   Forgive the late reply; I check this list less frequently than my normal haunt. (and you know where that is) I've had no problems but I don't use it regularly. However, I have used it several times since this post and it's worked fine. Wasn't it you that told me about it? I remember that there was another site mentioned in the same thread at Apple. It required an awk filter as it included HTML but it worked. Unfortunately I don't remember what it was and the thread appears to have been archived.
Yes, it was I who made mention of http://simple.showmyip.com on your other haunt. Oddly enough, that subdomain now works here. It hadn't resolved all weekend, but now it appears that their DNS or server issues are resolved.

While slightly less efficient, I've never had a problem with this code in a script:
curl -s http://checkip.dyndns.org/ | awk '/Current IP Address:/ {print $4}'

Although, I did just find a perl script that is seemingly more efficient than either of the other two commands. Instead of requesting a remote site's IP discovery page and returning the results, this one gets the external IP address from a LAN-based router:
Code:
#!/usr/bin/perl # # fetch the current ip address from a Linksys BEFSR... series router # replace ":admin" with a custom password, if appropriate # Jerry LeVan (jerry.levan@eku.edu) # Mac OS X 10.1.4 # May 12,2002 # my ($pattern,$thePage,$theAddress,$wan,$ip,$theip); #define the pattern we are looking for... $wan = '^.*<!--WAN head-->.*'; $ip = 'IP Address:</td><td><font face=verdana size=2>'; $theip = '(\d+\.\d+\.\d+\.\d+)</td'; $pattern = $wan.$ip.$theip; # Fetch the page $thePage = `curl -s -u :admin http://192.168.1.1/Status.htm` ; # Look for the current IP embedded in the Status Page if ($thePage =~ m|$pattern|) { $theAddress = $1; print $theAddress,"\n"; # here you could mail the address ... } else { print "Problem getting address...\n"; }
(Last edited by gatorparrots; Jul 22, 2003 at 11:12 AM. )
     
Mac Enthusiast
Join Date: Jun 2000
Location: New Jersey, USA
Status: Offline
Reply With Quote
Jul 22, 2003, 11:44 AM
 
Originally posted by Gary Kerbaugh:
&nbsp;&nbsp;&nbsp;ipconfig is installed by the Essentials.pkg installer package in /usr/sbin/ipconfig and ping is installed by the BaseSystem.pkg installer package in /sbin/ping.
Sorry, false alarm, I have both of them. I had stupidly expected that the finder search would find them. Argh.

Thanks for the help...
     
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status: Offline
Reply With Quote
Jul 22, 2003, 12:14 PM
 
Originally posted by gatorparrots:
curl -s http://checkip.dyndns.org/ | awk '/Current IP Address:/ {print $4}'
&nbsp;&nbsp;&nbsp;Ah, that's it. I love the Perl script but my router doesn't seem to have a status page of any type. It has only a web config page that's "so fancy" it only works on Windoze. :-( I can get the data via telnet and I could write an expect script to get it but that's complicated enough that I'd rather depend on one of these sites, if they work. Well, I'm glad it's back up ... really glad!
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 10:10 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2