padawan_linuxero has asked for the wisdom of the Perl Monks concerning the following question:

I have this little program that uses rexec is very simple I just want to run the command "ls"
#!/usr/bin/perl my @output; my $rc; my $command = "ls"; my $userid = "saai"; my $password = "saaisi0"; ($rc, @output) = rexec(xxx.xx.xx.xx,$command, [$userid, [$password]]); for (@output) { print "$_\n"; }
and these is the error
Error opening sock Invalid argument at C:/Perl/site/lib/Net/Rexec.pm l +ine 20.
Can you guys help me?

Replies are listed 'Best First'.
Re: Problem with Net::rexec
by toolic (Bishop) on Apr 08, 2008 at 20:06 UTC
    Aside from kyle's good suggestions, I think I have an idea as to why you are getting the Invalid argument error.

    I did a search on CPAN for Net::Rexec, and after reading the 4-sentence description, I decided to look at the source code (all 27 lines, or so). Here is a snippet:

    sub rexec { my($host) = shift; my($sock) = IO::Socket::INET->new(PeerAddr => $host, PeerPort => 'exec(512)', Proto => 'tcp'); die "Error opening sock $!" if (!defined($sock));

    It seems that you have to be careful how you pass the IP address to the sub, otherwise it might get corrupted. I think you need to make sure it gets passed as a string, instead of a number (with 3 decimal points). I created a little testcase to illustrate my point:

    #!/usr/bin/env perl use warnings; use strict; foo('111.22.33.44'); foo(111.22.33.44); sub foo { my $host = shift; print "host=$host\n"; }

    which prints:

    host=111.22.33.44 host=o!,

    I do not know why this happens. Perhaps a more knowledgeable monk can explain.

    Update: this is my perl version (just in case this is OS- or version-specific):

    This is perl, v5.8.5 built for x86_64-linux-thread-multi

      The 111.22.33.44 is a version string, or "v-string", which you could have also written v111.22.33.44 for a little more clarity. They're described in perldata under "Version Strings" in "Scalar values", which says they're deprecated and going to go away in the future.

Re: Problem with Net::rexec
by kyle (Abbot) on Apr 08, 2008 at 19:37 UTC

    First, I'd question whether the remote host provides the rexec service. As far as I know, that's not supported very much anymore and has been mostly replaced with ssh (see Net::SSH). So, check whether this works from the command line before proceeding in Perl.

    Second, I'm pretty sure that "[userid, [password]]" in the documentation means that those arguments are optional. Your code should actually say:

    ($rc, @output) = rexec($addr, $command, $userid, $password );

    As an aside, I'd guess that the contents of @output has newlines already, so you can print it this way:

    print @output;
      hello!
      We have a program in visual basic 6 that runs that command "rexec", I need to explain a bit, I am trying to make a program that can execute a command in the server, lets call "attend" to the command that I need to run, "attend" is actually a program that has been there for ages, but it has a very important job, this program validates all the operation files, so to run it is quite simple in the server : "attend filename" and that it. But since we are not going to run the "attend" program in the server we need it to run on the pc's.
      I know that rexec works.

      I hope this can shed more light and you could help me thank you