3

I have a perl script that now works from the windows command line (ActivePerl installed). It sends commands to turn ports on/off on a power controller (smart power strip). I have it setup so that I take a couple of command line arguments. The IP address of the device, and the power operation I want to take on that device. i.e. 10.0.40.1 on should turn the device with the IP 10.0.40.1 on, or rather turn the appropriate port on in the power controller.

I have an if elseif statement that will evaluate the IP address passed to the script and determine which power controller and what port is appropriate. The only other operation is the port and the operation are joined so if the device is on port 3 of the controller and the operation passed was 'on' then the new variable becomes 3on. This is what the remainder of the script understands and being a novice, it was easier to leave that unchanged.

THE ISSUE: When I run this command from windows either by

perl Lpower.pl 10.0.30.15 on

or

Lpower.pl 10.0.30.15 on

The issue is the point of this was to allow another application to invoke the script on my client. The other app is a piece of network management software. Through supported custom GUI extension I was able to add items to the menu for each device being managed. I have one for "Power On" and another for "Power Off". I point it to the Lpower script with the path local to my client machine, and it is invoked and runs, it also passes the arguments as expected, but my if statement does not work. It will not match on any IP, nor will it match on the operation "on" "off". I don't need to evaluate the latter, I just put a statement in their to test.

My script prints the variables, and they do show up, but the if statement doesn't work. It does not appear that it is adding leading whitespace or anything.

Here is a code snip of the relevant parts:

# $language = "PerlScript"
# $interface = "1.0" 

#!c:\perl64\bin\perl -w -CA
...
print STDERR "UserUtil $version\n\n";
my ($ipaddr, $onoff) = @ARGV;
print "$ipaddr $onoff\n";

if ($onoff eq "off") {print "it matched off"}
if ($ipaddr eq "10.0.40.1") {
    $epc='10.0.30.92';
    $port='1';
    }
elsif ($ipaddr eq "10.0.40.105") {
    $epc='10.0.30.92';
    $port='2';
    }
elsif (($ipaddr eq "10.0.40.100") || ($ipaddr eq "10.0.40.101")) {
    $epc='10.0.30.92';
    $port='3';
    }
...
else {print "no matches found"}
$oper="$port$onoff";
$base='http://'.$auth.'@'.$epc.'/';
print "$epc $oper $ipaddr";
cmd($oper) && die "Unknown Command $_\n";

There are some print statements in there for debugging. The output from CLI looks like this (and it works, ports power on/off as expected):

10.0.30.15 off
it matched off10.0.30.93 1off 10.0.30.15

Invoked from the web-app (which launches the script on my client)

10.0.30.15 on
Unknown Command
no matches found  10.0.30.15 off

Why would it be different? Anything I can do to force the correct handling of the arguments passed? If I read correctly they are expected to be UTF-8 encoded strings, could that app be sending something different and therefore the if statements break?

One more note. The app calls the script directly and passes the arguments, it does not call the perl executable, my environment knows what to do with a pl script. Not sure if that makes a difference.

Any Help is much appreciated. I do have some coding in my distant back-ground, but never for a profession, and completely new to perl.

Wade
  • 45
  • 5
  • can you show us the "cmd" sub? Turn on use strict; use warnings; – Miguel Prz Jul 30 '14 at 06:24
  • probably because you got the `10.0.30.15 on` as one arg in `$ARGV[1]` and not two args. Show how you call this script from your web-app. Instead of `print "$ipaddr $onoff\n";` try `print join('/', @ARGV),"\n";` or check the `$#ARGV`; – clt60 Jul 30 '14 at 06:33
  • Your arguments may come across encoded, like this http://stackoverflow.com/questions/4510550/using-perl-how-do-i-decode-or-create-those-encodings-on-the-web – Mark Setchell Jul 30 '14 at 06:36
  • @jm666, that did the trick. with the join I found it came across as `10.0.30.15\off` from command line and `10.0.30.15 off` from the other app. I adjusted one of the other print lines so I could see the exact contents of `$ipaddress` and `$onoff` and found it was all in `$ARGV[1]`. modified the way the arguments were sent on the other side and that fixed it. Thanks all for the help. – Wade Jul 30 '14 at 21:52

1 Answers1

0

The relevant part of your script

my ($ipaddr, $onoff) = @ARGV;
print "$ipaddr $onoff\n";
if ($ipaddr eq "10.0.40.1") { print "matched\n"; }
else {print "no matches found\n"}

run it as

perl power.pl 10.0.40.1 off

prints:

10.0.40.1 off
matched

run it as

perl power.pl "10.0.40.1 off"    #two words as one arg

prints

10.0.40.1 off 
no matches found

when you add

use strict;
use warnings;

my ($ipaddr, $onoff) = @ARGV;
print "$ipaddr $onoff\n";
if ($ipaddr eq "10.0.40.1") { print "matched\n"; }
else {print "no matches found\n"}

will print:

Use of uninitialized value $onoff in concatenation (.) or string at power.pl line 5.
10.0.40.1 off 
no matches found
clt60
  • 62,119
  • 17
  • 107
  • 194