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.
From what I've seen, the easiest way to get this information consistently from a variety of OSes is to use facter.
See also: Reference of built-in puppet facts.
If you want portable then you pretty much have to go with parsing the output of ifconfig.
facter
andip
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.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.
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