Simulate port listen with Perl

Simulate port listen with Perl

On servers behind firewalls were netcat is not available this script can come in handy:

#!/usr/bin/perl

if ($#ARGV != 1 ) {
    print "usage: Use hostname as first argument and port as second argument.\n";
    exit;
}

$ho=$ARGV[0];
$po=$ARGV[1];

use IO::Socket; 
my $sock = new IO::Socket::INET ( 
                                LocalHost => $ho, 
                                LocalPort => $po, 
                                Proto => 'tcp', 
                                Listen => 1, 
                                Reuse => 1, 
                                ); 
die "Could not create socket: $!\n" unless $sock;
my $new_sock = $sock->accept(); 
while(<$new_sock>) { 
     print $_; 
} 
close($sock)

The above script takes two arguments:

./portsim.pl localhost 3278

would listen on port 3278 until someone tries to connect.

IO::Socket::INET is usually available on servers where Perl is installed.

The above version is a slightly modified version of a script found here:

http://www.perlfect.com/articles/sockets.shtml