Gregory Machin am Donnerstag, 14. Dezember 2006 13:24:
> hi

Hi Gregory

> the script will not work

"Not work?" :-)

> if I use $ARGV[0] but works 100% if I hard 
> code the $input variable;
> what have i missed ?

You can give anything as cmd line argument, and the script will tell you that 
it is not running, because of

a) open PROS, "ps -ef|grep $input |";
combined with
b) unless ($line =~ m/grep/){...}

This script is running as root... that makes it even more important to check 
user provided data, what exactly is executed in the shell, and what binaries 
are called.

- use absolute paths for binaries (ps and grep in this case)
- make sure that $input only contains ascii characters
  for example, in this case, only a to z:

  my ($input) = $ARGV[0]=~/([a-z]{,16})/; # untested
  die unless $input;

btw: /etc/init.d/grep start ???

This is a hint to also check if $input corresponds to a binary 
in /etc/init.d at the beginning of the script :-)

This would solve the grep-not-allowed/script-uses-grep-and-does-not-work 
problem :-)

What about also using /etc/init.d/some_daemon status and check if this 
information is in sync with the ps output (it isn't always, and the restart 
could therefore fail - what you don't test)

hope this helps

Dani

> #!/usr/bin/perl
>
> # if your prgram has the string "grep" in the name or in the path
> # this program won't work.
>
> use strict;
> use warnings;
>
> my $line;
> my $input;
>
> $input=$ARGV[0]; #$input="httpd";
> chomp $input;
> open PROS, "ps -ef|grep $input |";
>
> while ($line = <PROS>){
>   unless ($line =~ m/grep/){
>   print "$input is running\n";
> exit;
> }
> }
>
> print "$input isn't running\n";
> exec "/etc/init.d/$input restart &";
>
> Many thanks
>
> --
> Gregory Machin
> [EMAIL PROTECTED]
> www.linuxpro.co.za

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to