#!/usr/bin/env perl

=head1 NAME

ssh-auth-types - Show the remote authentication types SSH presents.

=head1 SYNOPSIS

  ssh-auth-types host1 host2 .. hostN

=cut

=head1 DESCRIPTION

This script will connect to a remote SSH server, using "none" as an
authentication type.  It will then parse out the supported/accepted
authentication types from the error-message.

=cut

=head1 AUTHOR

 Steve
 --
 http://www.steve.org.uk/

=cut

=head1 LICENSE

Copyright (c) 2013 by Steve Kemp.  All rights reserved.

This script is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.

The LICENSE file contains the full text of the license.

=cut


use strict;
use warnings;

use File::Temp qw/ tempfile /;


#
# Get the host, and ensure it was present
#
while ( my $host = shift )
{
    testHost($host);
}


sub testHost
{
    my ($host) = (@_);


    #
    # Create a temporary file
    #
    my ( $fh, $tmp ) = tempfile();

    #
    # Connect to the remote host.
    #
    system("ssh -o PreferredAuthentications=none $host 2>$tmp >&2");

    #
    #  Now look for the output
    #
    open( my $handle, "<", $tmp ) or
      die "Failed to open tmp file $!";

    while ( my $line = <$handle> )
    {
        if ( $line =~ /^permiss.*\(([^(]+)\)/i )
        {
            my $options = $1;
            print $host . ": " . join( " ", split( /,/, $options ) ) . "\n";
        }
    }
    close($handle);


    unlink($tmp);


}
