[web] Perl sending messages to script....?
I made a script, which works oke. But now I want to expand it so it can receive certain commands in runtime. I want this for monitoring perposes, for example, I need to know what's in a certain variable.
I could write the values to a file but I don't want that because I would have to print it within an interval which doesn't let me see the live values.... a very small interval is not a smart idea either.
I hope you understand what I'm trying to tell.
Example: testscript.pl
The script is running for a few hours and I want to be able to send a command like "list actions".
You can listen on a socket:
then just connect with netcat or whatever, and pass it perl expressions:
e.g:
grazer@localhost grazer $ nc localhost 27690
$time
recieved: $time
Wed Jan 19 01:34:55 2005
grazer@localhost grazer $ nc localhost 27690
$name
recieved: $name
Bob
grazer@localhost grazer $ nc localhost 27690
1+1
recieved: 1+1
2
Just be careful that this is not exposed to the internet for obvious security reasons.
HTH,
-g
#!/usr/bin/perluse strict;use IO::Socket;$|++; # turn off buffering# set up a socketmy $sock = IO::Socket::INET->new( LocalPort => '27690', Type => SOCK_STREAM, Listen => 1, Reuse => 1, Timeout => .1, ) || die $!;# this is your script while (1){ # do whatever c00l stuff # you are already doing my $name = 'Bob'; my $time = localtime; # check for a command if (my $inc = $sock->accept) { while (defined(my $cmd = <$inc>)){ print $inc "recieved: $cmd"; print $inc eval $cmd, "\n"; $inc->close; } }}
then just connect with netcat or whatever, and pass it perl expressions:
e.g:
grazer@localhost grazer $ nc localhost 27690
$time
recieved: $time
Wed Jan 19 01:34:55 2005
grazer@localhost grazer $ nc localhost 27690
$name
recieved: $name
Bob
grazer@localhost grazer $ nc localhost 27690
1+1
recieved: 1+1
2
Just be careful that this is not exposed to the internet for obvious security reasons.
HTH,
-g
Thanks [smile] I bet that will do the trick. Is there also a more secure way of achieving this? Anyway, it's meant to see the results of a few actions. I will put something in the code that it accepts only a couple of commands. Plus, I won't be using "eval" since I don't want to execute the commands I'm sending.
It will probably be something like this:
It will probably be something like this:
# check for a command if (my $inc = $sock->accept) { while (defined(my $cmd = <$inc>)) { if ($cmd eq 'dosomething') { print "I gotta do something\n"; } $inc->close; } }
The insecurity comes mostly from the "eval", in the actual system, I would most likely just set up a dispatch table of available commands, something like (untested):
this way, you can run specific callbacks without having to worry about executing arbitrary code.
good luck,
my %cmds = ( show_data => \&show_foo_data, my_foobar => \&foobar_method, finish => sub { exit },);$cmds{$command_from_network}->();sub show_foo_data { # do stuff}sub foobar_method { # do other stuff}
this way, you can run specific callbacks without having to worry about executing arbitrary code.
good luck,
Hey that's a great idea (the hash thing). why didn't I come up with that idea? That's much better than using if statements... plus, it's easier to expand or disable commands.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement