Linux.com

Using Shell code in Perl - `Backticks`

Link to this post 01 Oct 10

HI all. First time poster here, so please bear with me.

I have a real time data feed that I'm pulling data off of into a local port. The command I use to grab the data is
"netcat localhost 12345".
This command works perfectly in the shell command line. The problem is that it runs continuously. To have it run only for 100 ms would be perfect.
In the Perl Script I am implementing this command with, my goal is to put each line into a string so I can parse the data, filter if need be, and do it again as a loop. The following commands are in a while(1) loop. I tried a number of things to get the data into a string line by line...

while(`netcat localhost 12345`) #because I know that backticked system commands return their output to the STDOUT

while(!`netcat localhost 12345`) #I thought maybe it worked with negative logic

$new_line = `netcat localhost 12345`; #This just didnt work at all...

`echo |netcat localhost 12345 >> $new_line`;

Unfortunately none have worked. I know that you can netcat the data into a file, but I would like to like to cut out the middle man, if possible. Hopefully someone knows a simple command that can help me.

Link to this post 01 Oct 10

Yah, your issue is that ` ` and system(), etc would spawn a new shell, and netcat will still behave the way it normally does and just hold the socket open.

You might be better off writing your own socket code in perl. Perl implements 2 methods, the C socket() call, and there is a perl module that implments a more OOP method called IO::Socket. I've used both in the past, but it's beena long time (I do most of my scripting in Python lately). However, if I remember right, the perldoc documentation for IO::Socket includes a simple example that is pretty general and adaptable to your needs.


perldoc IO::Socket::INET

That should do the trick for you. If you need more help than that, let me know, and I can probably whip up a skeleton for you.

Link to this post 01 Oct 10

Hi gomer,

I have the simplest code I could wish for in this reply here. I am thinking that this is the code I want, for with I had used "netcat localhost 12345". The recieve (recv SOCKET,SCALAR,LENGTH,FLAGS) function I am thinking is the one I want, although the description for its FLAGS is too vague - "Takes the same flags as the system call of the same name. "
My -f is just a guess for a proper flag, but i cannot disclude any flags for they are a mandatory parameter.

I have included my code's test case below. When I ran this program, I expected when a line went through the data feed, it would be printed on the screen. I tested this with a separate shell and it was not the case. Do you have any ideas on what I might be doing wrong?

###
use IO::Socket::INET;

$sock = IO::Socket::INET->new(Listen => 1, #queue size for listen
LocalAddr => 'localhost',
LocalPort => 12345,
Proto => 'tcp');


while(1)
{

my $new_line;

recv($sock, $new_line, 200, -f);

if (!$new_line) {next;}

print $new_line."\n";

}
###

Link to this post 02 Oct 10


my $new_line;

while (1)
{
$sock->recv($new_line, 200, -f);
if ......
......

You EITHER use IO::Socket to create an object and then use the object methods, OR you create a file handle w/ socket() and then use recv() ....

Link to this post 02 Oct 10

Beautiful, thank you so much for your help. It works!

Link to this post 04 Oct 10

Also, if you are using the system tool nc (vs the GNU netcat utility), it has a -w timeout option that will close the connection after 'timeout' number of seconds of no activity on the connection.

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board