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

Hi Monks,

I am looking for some advice/idea on a problem I am facing with the Net::SSH2 Module.

I work in a windows environment with CYGWIN. I succesfully use Net::SFTP::Foreign with Net:SSH2 as a back-end to send and get files my programs need.

I now want my main (father) script to be able to start remote (sons) perl scripts on multiple servers.

I use a batch file to start my perl script. The batch file works fine and starts my perl script the way I want it to when I execute it through RDP. The script runs in a console window.

Here is the code I use to try to start my program :

my $ssh = Net::SSH2->new(); $ssh->connect($IP,$port) or die "Unable to connect $@ \n"; if ($ssh->auth_password($user, $password)) { my $chan = $ssh2->channel(); $chan->exec("c:/foo/bar/myScript.bat \n"); $chan->close; }

The strange thing is it works fine with various apps I tested it with (eg Calc.exe, Notepad.exe, the Padre IDE...) but I can't get it to start and display cmd.exe or batch file execution window. All I see on the server side is a conhost.exe process spawning for a second.

I'm assuming the problem might be not on the perl side as I have it working with some apps. But I thought it wouldn't be dumber to ask for the help of the wise than to stay alone and scared in the darkness of ignorance.

Thank you in advance for anything that could put me in the right direction.

Porax

Replies are listed 'Best First'.
Re: Net::SSH2 some programs launch but can't get my script to run
by roboticus (Chancellor) on Oct 17, 2014 at 12:48 UTC

    Porax:

    Windows doesn't know how to run a batch file normally. That's handled by the command shell. So if you change your command to something like cmd /c c:/foo/bar/myScript.bat you'll likely have better luck.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thanks for the quick reply Roboticus.

      I tried your suggestion but sadly didn't get any better luck with it.

      Porax

        Porax:

        Try adding a line something like this to the beginning of your batch file:

        dir >C:\Users\roboticus\JUNK
        Then see if the JUNK file is actually created. If so, you're not starting your batch file. I originally guessed that you were having trouble starting the batch file, but I'm thinking that you're actually having problems with Net::SSH2. If that's the case, I won't be much help, as I don't use that package (nor any similar ones).

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Net::SSH2 some programs launch but can't get my script to run
by syphilis (Archbishop) on Oct 19, 2014 at 03:01 UTC
    I tested it with (eg Calc.exe, Notepad.exe, the Padre IDE...) but I can't get it to start and display cmd.exe or batch file execution window

    Looks to me that the problem must be with the contents of the batch file.
    What's the command (in the batch file) that successfully starts Notepad.exe ?
    What's the command (in the batch file) that fails to start cmd.exe ?

    Cheers,
    Rob

      Hi Rob !

      Here is my batch file

      Title "Title" set PATH=%PATH%;C:\perlPath\perl\bin\bin;C:\path C:\perlPath\Perl.exe C:\path\script.pl @Pause

      My script is started (I can see the cmd an perl processes spawning) but remains undisplayed.

      to start calc.exe for my tests I use :

      start c:/windows/system32/calc.exe

      It works and the calculator is normally displayed

      Thank for any idea that could come to your mind :)

      Porax

        You're not really doing anything to create a cmd.exe console and keep it alive.
        C:\sisyphusion>type script.pl use warnings; print "Hello World\n"; C:\sisyphusion>type try.bat @echo off set PATH=C:\MinGW\perl516\bin;C:\MinGW\bin;%PATH% @echo on cmd /k perl script.pl C:\sisyphusion>
        When I double click on 'try.bat' I get a cmd.exe console pop up that contains:
        C:\sisyphusion>cmd /k perl script.pl Hello World C:\sisyphusion>
        I think you should be able to make use of that approach, too.
        It's the "/k" switch that keeps the cmd.exe console alive. If you use "/c" instead, then the cmd.exe console closes as soon as the perl script has finished executing.
        (The "@echo off" and "@echo on" merely conceal the output of the "set PATH" command - you probably already know that.)

        Cheers,
        Rob