How about something already done?! ;-P
#!/usr/bin/perl $|=1; use IO::Socket ; use IO::Select ; my $redirect_host = 'smtp.fln.terra.com.br' ; my $redirect_port = 25 ; my $local_port = $redirect_port ; my $accept_externals = 0 ; my $log_file = 'log.txt' ; ###################################################################### +########## my $server = IO::Socket::INET->new( Listen => 5, ( !$accept_externals ? (LocalAddr => 'localhost') : () ) , LocalPort => $local_port , Proto => 'tcp' , Blocking => 1 , ) ; my $client = $server->accept; my $remote = new IO::Socket::INET( PeerAddr => $redirect_host , PeerPort => $redirect_port , Proto => 'tcp', Timeout => 30) ; my %socks = ( $remote => 'SRV' , $client => 'CLT' , ) ; my %redir = ( $remote => $client , $client => $remote , ) ; open (LOG,">$log_file") ; my $sel = select(LOG) ; $|=1; select($sel); print "Waiting client...\n" ; my $can_read = IO::Select->new( $remote , $client ); my @has_buf ; while( @has_buf = $can_read->can_read(1) ) { foreach my $has_buf_i ( @has_buf ) { my $redir = $redir{$has_buf_i} ; my $buffer ; my $sel = IO::Select->new($has_buf_i) ; while( $sel->can_read(0.1) ) { read($has_buf_i, $buffer , 1 , length($buffer) ) ; if ($buffer =~ /\n$/s ) { last ;} } if ($buffer ne '') { print $redir $buffer ; print "$socks{$has_buf_i}>> $buffer" ; print LOG "$socks{$has_buf_i}>> $buffer" ; } } }
This script will listen and redirect to some port, and log the data to a file. Just edit the variables in the top and run. I used that to check the security of the SMTP server of my ISP.

Graciliano M. P.
"Creativity is the expression of the liberty".


In reply to Re: Working with ports in Perl by gmpassos
in thread Working with ports in Perl by antioch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.