Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: Getting net addy on localhost?

by rob_au (Abbot)
on Nov 27, 2001 at 09:57 UTC ( #127734=note: print w/replies, xml ) Need Help??


in reply to Re: Getting net addy on localhost?
in thread Getting net addy on localhost?

Nifty idea, but the problem with this approach is that you still need to know the world-resolvable name of the machine. You cannot call Net::hostent::gethost() without a host name to resolve. eg.

perl -MNet::hostent -e '$host = gethost(); print $host->name, "\n"' Not enough arguments for Net::hostent::gethost at -e line 1, near "()" Execution of -e aborted due to compilation errors.

 
Ooohhh, Rob no beer function well without!

Replies are listed 'Best First'.
Re:**3 (rob_au) Getting net addy on localhost?
by belg4mit (Prior) on Nov 27, 2001 at 10:05 UTC
    And let's not get into the fact that you might have multiple IP addresses.

    You never said what platform you are on either. If UN*Xy the easiest pure-perl is to parse /etc/sysconfig/network-scripts/ifcfg-*. You might steal the code from http://www.webmin.com/webmin/

    --
    perl -p -e "s/(?:\w);([st])/'\$1/mg"

      The method that I pointed out in the addendum to my first post in this thread *should* suffice if you are wanting to resolve the default external IP address of the machine - But I do agree, there is a great amount of 'grey area' on this question given the number of available configurations and methods of interface.

      I don't however agree with the concept of parsing /etc/sysconfig/network-scripts/ifcfg-* - For starters, this is very much a Red Hat (and variant) locale for network configurations, *not* a general *NIX location pragma. Further to this, the idea of relying on text files generated outside of the real scope of the program makes me very nervous - I have rambled on this concept previously and as such won't repeat myself here.

      Suffice to say, if you are going to rely on *any* external source, consider it tainted and evil (-T, -T and -T) until proven otherwise - And even then treat it with care. If I had to rely on an external program source, then I would be looking directly at /sbin/ifconfig (the path of which should be hard-coded into the program, not drawn from an alternate configuration file) and parsing the output of that.

      Just my random thoughts on the matter

       

      Ooohhh, Rob no beer function well without!

        Well I have seen /etc/sysoncfig/network-scripts on other platforms. But I'll take your word on that. However this is one reason why I mentioned webmin which is ultra-cross-platform (even if a little ugly).

        The problem with calling something like ifconfig is relying upon a standard output scheme across platforms... I doubt Sun does it similar to anybody else (afterall they have their own cron, grep, and ping etc.) UPDATE: I do suppose this |P is a bit GNU-centric ;-).

        --
        perl -p -e "s/(?:\w);([st])/'\$1/mg"

      That is incorrect, '/etc/sysconfig/network-scripts/ifcfg-*' is a RedHat-ism and isn't even portable between all linux distros. As far as I know there is no standard file between unix and unix-like systems for this information. You can try /etc/hosts but that will only give you resolvable interfaces. Parsing the output of '/sbin/ifconfig' is more likely to be portable. (not that portability is the goal here, but we don't know what system your running on) SNMP (if you have it running) would be another possible means of getting interfaces addresses.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://127734]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2021-10-24 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (88 votes). Check out past polls.

    Notices?