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

I know how to use Win32::OLE to open and control IE, Excel, and other similar Windows Apps on a local or remote machine. However, I need to open a DOS window (cmd.exe) on a remote machine, use it to issue commands (dir, cd, ipconfig, etc...) and read the resulting output. I know I can do it without visibility with telnet, but I need the DOS window to be visible on the remote machine. The following works for IE, but not for cmd:
use Win32::OLE; my $ie = Win32::OLE->new('InternetExplorer.application') or die "oops\ +n"; $ie->{visible} = 1; $ie->Navigate("http://www.cpan.org"); my $dos = Win32::OLE->new('Cmd.application') or die "oops\n"; $dos->{visible} = 1;
I get this error:
Win32::OLE(0.1601) error 0x800401f3: "Invalid clas string" at C:\scrip +ts\dos.pl line 14 eval {...} called at C:\dos.pl line 14 oops
If it can't be done with Win32::OLE, is there another module that will let me do this?

Thanks!


"Peace, love, and Perl...well, okay, mostly just Perl!" --me

Apprentice

Replies are listed 'Best First'.
Re: How can I open and use a DOS window on another computer?
by BrowserUk (Patriarch) on Nov 24, 2003 at 05:00 UTC

    In order to use OLE to control an application, the appliation has to be OLE-enabled. That is to say, the program has to contain hooks (entrypoints) that allow it to respond to OLE commands and control.

    cmd.exe is not (and almost certainly never will be) OLE enabled.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!
    Wanted!

      In this case, you would not use Cmd.exe but rather WScript.Shell to perform file and network operations on a remote machine; and you can even launch programs from it (see The behavior of Win32::OLE->new from a couple of weeks ago). Being an OLE object, it's fully scriptable in Perl using Win32::OLE.

        Nice thought, but the OP's requirements were that the commands be executed in a visible command line window on the target machine, and I can't quite see how to do that using WSH? Either the commands will run in the background, or each will be started in it's own window I think?

        I guess a clearer explaination of what the OP is trying to achieve would make it easier to know what to suggest.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        Hooray!
        Wanted!

Re: How can I open and use a DOS window on another computer?
by davido (Cardinal) on Nov 24, 2003 at 04:41 UTC
    A discussion along a similar vein cropped up a month or so ago. You may find this node and its discussion thread to be helpful:

    Open a second DOS window

    I realize that it's not an exact match of your question, but if I recall the thread had a lot of good information that could prove helpful.

    Good luck!


    Dave


    "If I had my life to live over again, I'd be a plumber." -- Albert Einstein
Re: How can I open and use a DOS window on another computer?
by Roger (Parson) on Nov 24, 2003 at 05:18 UTC
    Your line below will not work:
    my $dos = Win32::OLE->new('Cmd.application') or die "oops\n"; $dos->{visible} = 1;
    Why? Because Cmd.application is not a registerred class and OLE does not know what to do with it.

    As to why 'Cmd.application' is not a registerred OLE class, I guess it's because of security issues, imagine openning a file with embedded object that invokes your CMD.exe and delete files...

    There are perl equivalents to what you wanted to do on the local machine -
    dir: opendir cd: chdir ipconfig: qx(PATH/ipconfig.exe)
    And you don't want to allow execution of DOS commands remotely, too much security concerns.

    I guess one thing you could do is to install trusted remote clientsservers that listen on TCP ports, and execute commands based on remote requests. One such client we are using here is On Command Remote which allows you to remotely issue command and install programs, etc...

    Worse comes to worst, you can always roll your own client/server systems.

Re: How can I open and use a DOS window on another computer?
by cLive ;-) (Prior) on Nov 24, 2003 at 08:49 UTC
    Perhaps Real VNC might be an option?

    .02

    cLive ;-)

Re: How can I open and use a DOS window on another computer?
by davis (Vicar) on Nov 24, 2003 at 10:16 UTC

    I'm surprised no-one has mentioned this yet, because if you're trying to use Windows, SysInternals' tools are very useful. For example:

    > psexec \\remotebox cmd
    Gets you a command prompt on "remotebox". You could (probably) do something hideous with pipes to run whatever commands you (or anyone else unless you're very careful) want.
    cheers


    davis
    It's not easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
    Update: Fixed typo.