I am new to perl and have been looking in tutorials and searching for an answer to no avail. I need to come up with a way to listen on a specific port for data coming in from a PBX and then append this data to a file. I was trying to use netcat for this but netcat will not stay open in the background.

I then tried to do something with perl but have hit a dead end. Here is what I am trying to run as a script

#!/usr/bin/perl use strict; use warnings; use IO::Socket; my $sock = new IO::Socket::INET ( LocalHost => '127.0.0.1', LocalPort => '6000', Proto => 'tcp', Listen => 1, ); die "Could not create socket: $!\n" unless $sock; my $new_sock = $sock->accept(); while(<$new_sock>) { print $_; } close ($sock);

this bit of code works and if I telnet to the box/port, I get everything I type on the screen. Now to take it a step further, I wanted to redirect this to a file and append it. I expect this to run in the background all day long as the PBX will be sending info on the calls that come in that we need for analysis.

I have tried redirecting the perl script at the command line

 perl receiver.pl >> /tmp/test_file

with no success and I have tried to add some logic in the script by adding a fh but that wasn't working either.

my $new_sock = $sock->accept(); while(<$new_sock>) { open my $fh, '>>', $file or die $!; close $fh;

Can anyone point me in the right direction here?

.Thanks, Steve


In reply to script to listen on a port & redirect to a file by a31modela

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.