3

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.

Dana the Sane
  • 828
  • 10
  • 19
  • 1
    http://serverfault.com/questions/195273/how-can-i-display-the-ip-address-of-an-interface – user9517 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. – user9517 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
  • @Wesley I suppose, but I _stressed_ some points of a question for a reason. I think that the community becomes far less useful when you can't ask a question with some overlap of others (without laying out every difference with existing questions). If the 'answer' is that there is no answer, saying so is quite enough, without the fuss. – Dana the Sane Aug 16 '11 at 15:22

4 Answers4

2

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.

EEAA
  • 109,363
  • 18
  • 175
  • 245
2

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.

user9517
  • 115,471
  • 20
  • 215
  • 297
1

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.

1

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

Zoredache
  • 130,897
  • 41
  • 276
  • 420