Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm writing some setup scripts/documentation and I want to dynamically set the listen ip for a service, given that I know that a particular network interface will be on a particular network. There are a bunch services on my network that I'd like to configure this way, so I hope that this will streamline my work a (tiny) bit.

I know that can do this by parsing incantations of ifconfig, ip, etc, but I'm wondering if this is the canonical solution.

share|improve this question
1  
serverfault.com/questions/195273/… –  Iain Aug 15 '11 at 18:01
    
@Iain I read that question before asking, but the answer doesn't satisfy my question. I'm asking if there are alternatives to parsing cmd output with grep, sed, awk, etc. –  Dana the Sane Aug 15 '11 at 18:52
2  
@Dana Take note that it's usually a good thing to pre-empt the cries of "Dupe! DUPE!!" by listing out what other ServerFault / StackExchange Q/A threads you've looked at but discarded (and why you discarded them as not being pertinent). –  Wesley Aug 15 '11 at 19:23
1  
@DanatheSane: If you want portable then you pretty much have to go with parsing the output of ifconfig. facter for example isn't native to Solaris and probably other Uinixlike OSes. The output of ifconfig varies across platforms too. You really are going to have to write a script that detects the OS and does the right thing. –  Iain Aug 15 '11 at 20:16
    
@Iain Then that would have been an acceptable answer as I asked if such a command exists. –  Dana the Sane Aug 16 '11 at 15:19
show 1 more comment

4 Answers

up vote 2 down vote accepted

If you want portable then you pretty much have to go with parsing the output of ifconfig. facter and ip for example aren't native to Solaris and probably other Uinixlike OSes. The output of ifconfig varies across platforms too. You really are going to have to write a script that detects the OS and does the right thing. I have collected some one liners for various operating systems here.

share|improve this answer
add comment

From what I've seen, the easiest way to get this information consistently from a variety of OSes is to use facter.

$ facter | grep ipaddress
ipaddress => 192.168.0.2 
ipaddress_eth0 => 192.168.0.2 
ipaddress_eth1 => 192.168.0.3

See also: Reference of built-in puppet facts.

share|improve this answer
    
Facter looks good and there's a package available from RHEL (for once). –  Dana the Sane Aug 15 '11 at 18:41
add comment

ifconfig is the most common tool I've encountered. If you're certain you want to work only in shell/ifconfig level of things, just write some shell code to parse the output from "ifconfig en0" (you mentioned knowing the interface). Check for status: active, and then look in the "inet" line to grok the IP address.

Also, if you have DNS services on these systems, you could use just resolve the output from the "hostname" command.

Or even go around and add an extra record to your /etc/hosts (if you've got files-based resolution in the mix) like "selfAddress" and put the box's IP in there -- one thing to set on each system. Then in your start/documentation/etc just resolve that static name, and on each system you'll get the IP.

share|improve this answer
add comment

I can think of one canonical solution that should be vendor neutral and work across many platforms, including Linux, *nix, Windows, router firmware, and many other systems.

Enable SNMP on everything and then grab the interface/ip list via SNMP. It is a relatively complex way to grab information, hence the scrapping of the ip from ifconfig, ip, ipconfig, and other tools.

The relevant sections to walk include

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.