Skip to content

Instantly share code, notes, and snippets.

@SebDeclercq
Created June 28, 2017 17:20
Show Gist options
  • Select an option

  • Save SebDeclercq/408bb20ae71a83fbc0f1bc8a2c359071 to your computer and use it in GitHub Desktop.

Select an option

Save SebDeclercq/408bb20ae71a83fbc0f1bc8a2c359071 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# Script qui scanne les ports d'un serveur afin d'y trouver
# les ports ouverts.
use warnings;
use strict;
use feature 'say';
use IO::Socket;
use Term::ANSIColor;
$|=1; # Autoflush
my $interactif = 0;
my ( $host, $startPort, $endPort );
if ( $ARGV[0] =~ /^-+i(nteractif)?/i ) {
$interactif = $ARGV[0];
}
else {
( $host, $startPort, $endPort ) = (
shift // "http://localhost", # Soit ARGV[XXX] soit défaut
shift // 1,
shift // 100
);
}
if ( $interactif ) { # Mode interactif
print "Host>\t";
chomp( $host = <STDIN> );
print "Start port>\t";
chomp( $startPort = <STDIN> );
print "End port>\t";
chomp( $endPort = <STDIN> );
}
say "Scanning \"$host\" from port $startPort to port $endPort";
for ( $startPort .. $endPort ) { # Pour chaque port dans le range
print "Scanning $_>\t";
my $socket = IO::Socket::INET->new( # Essaie d'ouvrir un socket
PeerAddr => $host,
PeerPort => $_,
Proto => 'tcp',
Timeout => 1,
);
# Si un socket est ouvert, OPEN en vert, sinon CLOSED en rouge
say $socket ? color("bold green")."OPEN" : color("bold red")."CLOSED", color("reset");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment