| 1 | package Net::Prober::ssh; | ||||||||||||||||||||||||||||
| 2 | 1 | $Net::Prober::ssh::VERSION = '0.16'; | |||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use base 'Net::Prober::Probe::TCP'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | sub defaults { | ||||||||||||||||||||||||||||
| 10 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | my $defaults = $self->SUPER::defaults(); | |||||||||||||||||||||||||||
| 13 | 2 | $defaults->{port} = 22; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | return $defaults; | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub probe { | ||||||||||||||||||||||||||||
| 19 | 1 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
probe: called CC=7 SCAR=24.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | my ($host, $port, $timeout, $username, $password) = | |||||||||||||||||||||||||||
| 22 | $self->parse_args($args, qw(host port timeout username password)); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | my $t0 = $self->time_now(); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | my $sock = $self->open_socket($args); | |||||||||||||||||||||||||||
| 27 | 1 | ▶ | if (! $sock) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | return $self->probe_failed( | |||||||||||||||||||||||||||
| 29 | reason => qq{Couldn't connect to SSH server $host:$port}, | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | chomp (my $ssh_banner = $self->_get_reply($sock)); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | if (! $ssh_banner) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | return $self->probe_failed( | |||||||||||||||||||||||||||
| 37 | reason => qq{Couldn't get SSH banner from $host:$port} | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # SSH-protoversion-softwareversion SP comments CR LF | ||||||||||||||||||||||||||||
| 42 | 1 | ▶ | if ($ssh_banner !~ qr{^SSH- | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | (?<protoversion> [^\-]+) - | ||||||||||||||||||||||||||||
| 44 | (?<softwareversion> [^\s]+) \s? | ||||||||||||||||||||||||||||
| 45 | (?<comments> .*)? $}x) { | ||||||||||||||||||||||||||||
| 46 | 0 | return $self->probe_failed( | |||||||||||||||||||||||||||
| 47 | reason => qq{Non-RFC compliant SSH banner from $host:$port? ($ssh_banner)}, | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | my %ssh_info = ( | ||||||||||||||||||||||||||||
| 52 | protoversion => $+{protoversion}, | ||||||||||||||||||||||||||||
| 53 | softwareversion => $+{softwareversion}, | ||||||||||||||||||||||||||||
| 54 | comments => $+{comments}, | ||||||||||||||||||||||||||||
| 55 | 1 | banner => $ssh_banner, | |||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # We can't try to login if we haven't got credentials | ||||||||||||||||||||||||||||
| 59 | 1 | ▶ | if ($username && $password) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$username and $password
MC/DC0%0/2
$username and $password
$username
$password
Truth table33%1/3
A and B
A= $usernameB= $password
| |||||||||||||||||||||||||||||
| 60 | 0 | $self->_send_command($sock, $username . "\n" . $password . "\n"); | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | if (! $self->_get_reply($sock)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | return $self->probe_failed( | |||||||||||||||||||||||||||
| 63 | reason => qq{Couldn't login to ssh $host:$port with user $username}, | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Say goodbye | ||||||||||||||||||||||||||||
| 69 | 1 | $self->_send_command($sock, 'exit'); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | return $self->probe_ok(%ssh_info); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub _send_command { | ||||||||||||||||||||||||||||
| 75 | 1 | ▶ | my ($self, $sock, $text_input) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_send_command: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | 1 | return $sock->send($text_input); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub _get_reply { | ||||||||||||||||||||||||||||
| 80 | 1 | ▶ | my ($self, $sock) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_reply: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | 1 | $sock->recv(my $reply, 1024); | |||||||||||||||||||||||||||
| 82 | 1 | $reply =~ s{\s+$}{}; | |||||||||||||||||||||||||||
| 83 | 1 | return $reply; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | 1; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||||||||||