in reply to Re: parsing a terrible /etc/hosts
in thread parsing a terrible /etc/hosts

Cpan? Let me try hosts ->

Config::Hosts Interface to /etc/hosts file

Parse::Hosts Parse /etc/hosts

App::ParseHosts Parse /etc/hosts (CLI)

Looks promising

Replies are listed 'Best First'.
Re^3: parsing a terrible /etc/hosts
by huck (Prior) on Mar 24, 2017 at 07:34 UTC

    Config::Hosts Interface to /etc/hosts file

    if ($hosts->{$ip}) { print STDERR "Line $l: Warning: duplicate IP entry $ip, the last one + will be used\n"; }
    As far as i remember(AIX, ubuntu, win) you may have duplicate lines with the same ip and they are "joined".

    Also "output" mixes up names and ips in same array. While strange, this is a valid line
    127.0.0.1 192.168.0.1
    it makes the host-name 192.168.0.1 map to the ip of 127.0.0.1, common use is by script kiddies tho.

    Parse::Hosts Parse /etc/hosts

    unless (defined $content) { open my($fh), "<", "/etc/hosts" or return [500, "Can't read /etc/hosts: $!"]; local $/; $content = <$fh>; }
    only reads /etc/hosts or you have to read it yourself and pass it in. "output" is an array of hashs {ip => $ip, hosts => \@hosts}

    App::ParseHosts Parse /etc/hosts (CLI) Just a wrapper around Parse::Hosts to allow you to read anyfile

    Mine parses the lines same way as them, and has more usable data structures for the task and example. Mine doesnt check for valid ipv4 or ipv6 format tho like Config::Hosts almost does. I looked at Config::Hosts first and didnt like it

    add: and none are core either

Re^3: parsing a terrible /etc/hosts
by f77coder (Beadle) on Mar 25, 2017 at 03:06 UTC
    @Anonymous-Monk

    Thanks for the links.

    Well, my first inclination is to write code not search, I like to know what 'stuff' is doing. To each their own. I've been burned before by relying on modules.