Complete-Host

 view release on metacpan or  search on metacpan

lib/Complete/Host.pm  view on Meta::CPAN

            default => 1,
        },
        include_ssh_known_hosts => {
            summary => 'Whether to include hosts in ssh known_hosts files',
            description => <<'_',

Only unhashed hosts will be included.

_
            schema => 'bool*',
            default => 1,
        },
    },
    result_naked => 1,
    result => {
        schema => 'array',
    },
};
sub complete_known_host {
    my %args = @_;

    my $inc_ip = $args{include_ip};

    my %hosts;

    # from /etc/hosts
    {
        last unless $args{include_hosts} // 1;
        log_trace("[comphost] Checking /etc/hosts") if $COMPLETE_HOST_TRACE;
        require Parse::Hosts;
        my $res = Parse::Hosts::parse_hosts();
        last if $res->[0] != 200;
        for my $row (@{ $res->[2] }) {
            if ($inc_ip) {
                log_trace("[comphost]   Adding: %s", $row->{ip}) if $COMPLETE_HOST_TRACE;
                $hosts{$row->{ip}}++;
            }
            for (@{$row->{hosts}}) {
                log_trace("[comphost]   Adding: %s", $_) if $COMPLETE_HOST_TRACE;
                $hosts{$_}++;
            }
        }
    }

    # from ifconfig output
  IFCONFIG:
    {
        last unless $inc_ip;
        log_trace("[comphost] Checking ifconfig output") if $COMPLETE_HOST_TRACE;
        require IPC::System::Options;
        for my $prog ("/sbin/ifconfig") {
            next unless -x $prog;
            my @lines = IPC::System::Options::readpipe(
                {lang=>"C"}, "$prog -a");
            next if $?;
            for my $line (@lines) {
                if ($line =~ /^\s*inet addr:(\S+)/) {
                    log_trace("[comphost]   Adding %s", $1) if $COMPLETE_HOST_TRACE;
                    $hosts{$1}++;
                }
                if ($line =~ m!^\s*inet6 addr:\s*(\S+?)(?:/\d+)?(?=\s)!) {
                    log_trace("[comphost]   Adding %s", $1) if $COMPLETE_HOST_TRACE;
                    $hosts{$1}++;
                }
            }
            last IFCONFIG;
        }
    }

    # from SSH known_hosts
    {
        last unless $args{include_ssh_known_hosts} // 1;
        my @files;
        push @files, "$ENV{HOME}/.ssh/known_hosts"
            if $ENV{HOME};
        for my $file (@files) {
            next unless -f $file;
            log_trace("[comphost] Checking %s", $file) if $COMPLETE_HOST_TRACE;
            open my($fh), "<", $file or next;
            while (my $line = <$fh>) {
                next unless $line =~ /\S/;
                next if $line =~ /^\s*#/;
                $line =~ /^(\S+)/ or next;
                my $h = $1;
                next if $h =~ /\A\|/; # hashed
                my $is_ip = $h =~ $re_ipv6 || $h =~ $re_ipv4;
                next if $is_ip && !$inc_ip;
                log_trace("[comphost]   Adding %s", $h) if $COMPLETE_HOST_TRACE;
                $hosts{$h}++;
            }
        }
    }

    # TODO: from git remotes

    # TODO: from shell history of ssh commands

    require Complete::Util;
    Complete::Util::complete_hash_key(word => $args{word}, hash=>\%hosts);
}

1;
# ABSTRACT: Completion routines related to hostnames

__END__

=pod

=encoding UTF-8

=head1 NAME

Complete::Host - Completion routines related to hostnames

=head1 VERSION

This document describes version 0.05 of Complete::Host (from Perl distribution Complete-Host), released on 2017-07-03.

=for Pod::Coverage .+

=head1 FUNCTIONS

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.607 second using v1.00-cache-2.02-grep-82fe00e-cpan-b63e86051f13 )