Created
November 12, 2018 05:55
-
-
Save ik5/738a7e9e8f875e028447c59f23f8bb22 to your computer and use it in GitHub Desktop.
Simple port scanner example written in Perl 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use warnings; | |
use strict; | |
use v5.20; | |
use IO::Socket::INET; | |
use experimental qw( switch ); | |
sub scan { | |
my ($address, $port, $timeout) = @_; | |
my @ips = inet_aton($address) || return "Can't resolve $address"; | |
my $ip = inet_ntoa(@ips) || return "Can't extract from @ips"; | |
my $socket = IO::Socket::INET->new( | |
PeerHost => $ip, | |
PeerPort => $port, | |
Timeout => $timeout, | |
); | |
if ($@) { | |
my $err = ''; | |
given ($@) { | |
when(/connect: timeout$/) { $err = 'closed' } | |
when(/connect: connection refused$/i) { $err = 'filtered' } | |
default { $err = $@ } | |
} | |
return $err; | |
} | |
$socket->close(); | |
return 'open'; | |
} | |
say scan('google.com', 443, 3); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment