Solved

Perl and listening TCP socket

Posted on 2007-03-22
7
629 Views
Last Modified: 2012-05-05
Hi gurus


I need to listen a TCP socket for a specifig string, take that string and make something with it.

So far I have no luck and here is my code:

----------------------  START ----------------------------
#!/usr/bin/perl -w
use IO::Socket::INET;
use LWP::UserAgent;
use IO::Socket;


# Open LOG file for debug
open (LOG,">>/tmp/log.txt");

#Open socket
$MySocket=new IO::Socket::INET->new(Listen  => 5,
                             LocalPort => 3319,
                               Proto => 'tcp') or die "$!" unless $sock;


# Next we need to wait so long, that we receive a string "PROG_ID xxxxxxx" ,where xxxx is a some ASCII string. Time to wait is from few seconds to 6 hours
while(1)
{
        $MySocket->recv($ID,512);
      print LOG "From socket $ID\n";       # Are we getting any

              if ($ID =~ /^PROG_ID/)
              {
                      print LOG "IF-LAUSEESSA: $ID\n";
                      $ID =~ s/PROGRAM_START //;      #RIP off "PROG_ID and leave only ASCII string
                  print LOG "ASCII ID is: $ID\n";

            }
}
 
0
Comment
Question by:salmjuh
  • 3
  • 2
7 Comments
 
LVL 8

Expert Comment

by:bebonham
ID: 18775763
try it using regular sockets, no module.  I never got those modules working.


#!/usr/bin/perl -w
use Socket;
# Open LOG file for debug
open (LOG,">>/tmp/log.txt");

#Open socket
my $port=3319;
my $proto=getprotobyname('tcp');
socket(Server, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l",1)) or die "setsockopt: $!";
bind(Server, sockaddr_in($port, INADDR_ANY)) or die "bind: $!";
listen(Server,SOMAXCONN) or die "listen: $!";

my $paddr;
my $ID;

# Next we need to wait so long, that we receive a string "PROG_ID xxxxxxx" ,where xxxx is a some ASCII string. Time to wait is from few seconds to 6 hours

for( ; $paddr = accept(Client, Server); close Client)
{
my($port, $iaddr) = sockaddr_in($paddr);
my $buff;
my $string_size =14;  ####PLEASE PUT HOW BIG THE STRING IS FROM THE CLIENT
while(read Client, $buff, $string_size)
{
$ID.=$buff;
print LOG "From socket $ID\n";       # Are we getting any
     
              if ($ID =~ /^PROG_ID/)
              {
                      print LOG "IF-LAUSEESSA: $ID\n";
                      $ID =~ s/PROGRAM_START //;      #RIP off "PROG_ID and leave only ASCII string
                  print LOG "ASCII ID is: $ID\n";
                 $ID="";

            }
}
 }
0
 
LVL 8

Expert Comment

by:bebonham
ID: 18775824
you could also use
while($buff=<Client>)

if you don't want to specify string size.

but then I think you will need to know when a PROG_ID xxxxxx code ends like by the newline or somehting
since I can't see what data the client is putting in I can't tell u for sure.
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 18777396
Here's your IO::Socket code fixed up.  It assumes your client is sending CRLFs.  If it isn't, it'd block until the socket close.    

#!/usr/bin/perl -w
use IO::Socket;

# Open LOG file for debug
open (LOG,">>/tmp/log.txt");

#Open socket
$MySocket=new IO::Socket::INET->new(Reuse => 1,
                              Listen  => 5,
                              LocalPort => 3319,
                              Proto => 'tcp') or die "$!";

# Next we need to wait so long, that we receive a string "PROG_ID xxxxxxx" ,where xxxx is a some ASCII string. Time to wait is from few seconds to 6 hours

$MySocket->listen();
while ( $conn = $MySocket->accept() ) {
     while ($ID = <$conn>) {
          print LOG "From socket $ID\n";       # Are we getting any
          if ($ID =~ /^PROG_ID/)
          {
               print LOG "IF-LAUSEESSA: $ID\n";
               $ID =~ s/PROGRAM_START //;      #RIP off "PROG_ID and leave only ASCII string
               print LOG "ASCII ID is: $ID\n";
          }
     }
}

---------
If you're client isn't using CRLFs and you really want to read 512 bytes at a time, change this:

   while ($ID = <$conn>) {

To:

   while (read($conn, $ID, 512))  {

The read will block until it receives 512 bytes or until the socket is closed-- not sure what you're after.  Also this:  

    $ID =~ s/PROGRAM_START //;

doesn't really jive with your comment (or with your earlier check):

   #RIP off "PROG_ID and leave only ASCII string

Did you mean?

   $ID =~ s/PROG_ID\s.//;
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:salmjuh
ID: 18806204
Hi Gurus


Here is detailed information for the problem.

The string what I need to wait look like this:

"PROGRAM_START 1243564236\r\n"


So how do a wait for a string and 6 to 10 number which end \r\n ?


Thansk for your help

-js
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 18808274
Try:

#!/usr/bin/perl -w
use IO::Socket;

# Open LOG file for debug
open (LOG,">>/tmp/log.txt");

#Open socket
$MySocket=IO::Socket::INET->new(Reuse => 1,
                              Listen  => 5,
                              LocalPort => 3319,
                              Proto => 'tcp') or die "$!";

$MySocket->listen();
while ( $conn = $MySocket->accept() ) {
     while ($ID = <$conn>) {
          chomp;
          print LOG "From socket $ID\n";       # Are we getting any
          if ($ID =~ /PROGRAM_START\s+(\d{6,10)/)
          {
               print LOG "IF-LAUSEESSA: $ID\n";
               print LOG "ASCII ID is: $1\n";
          }
     }
}
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 125 total points
ID: 18808285
Sorry typo.  Change this line:

   if ($ID =~ /PROGRAM_START\s+(\d{6,10)/)

To:

   if ($ID =~ /PROGRAM_START\s+(\d{6,10})/)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now