Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Windows Remote Desktop In Perl

by muralidharan (Novice)
on Jan 08, 2010 at 08:04 UTC ( #816239=perlquestion: print w/ replies, xml ) Need Help??
muralidharan has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks..

I just got trained in Perl and i am asked to find out how to connect to a remote windows desktop (Windows Server 2003) from a windows machine (Windows XP Pro SP3) through Perl, researching on this for past one month but of no use. We have been doing this via Remote Desktop Connection for years (mstsc). There are a bunch of Go Grid servers and we log on to it using Remote Desktop and run Perl scripts there. Is it possible to do this through Perl - i.e. logging on to the Go Grid Server (of course its a WAN) with the IP, Username and Password. Then copying the script from local to server, if not possible, just run the scripts that are already there. I have been trying this with Net::SSH::Perl Module and Net::Telnet Module but it shows error. I'm not even sure whether these modules can be of help to me. Will paste the code and the errors anyways.

use Net::SSH::Perl; $scon = Net::SSH::Perl->new ("xx.xxx.xx.xx", port=>80); #$scon->login("username","password"); my($stdout, $stderr, $exit) = $ssh->cmd("foo"); $ssh->close;
Error: Connection closed by remote host at D:\remote.pl line 40
use Net::Telnet(); $telnet = new Net::Telnet ( Errmode=>'die', Prompt => '/\$ $/i'); $telnet->open(host=> 'xx.xxx.xx.xxx', Port => 80); $telnet->login('username', 'password'); print $telnet->cmd('who');
Error: timed-out waiting for login prompt at D:\remote.pl line 74

Please serve the needful. Thank You Monks!!!!!!!!!!!!

20100114 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Comment on Windows Remote Desktop In Perl
Select or Download Code
Re: Windows Remote Desktop In Perl
by Corion (Pope) on Jan 08, 2010 at 08:20 UTC

    MS Terminal Server is different from a telnet server or a Secure Shell Server (ssh). So it comes as little surprise to me that this does not work for you. I guess one approach would be to find whether there are SSH servers for Windows Server 2003, as I doubt that somebody has written a scriptable interface to the Terminal Server Client that you can use through Perl.

      Thank You for the reply Corion.. I just want to connect to the remote server irrespective of the protocol or client, whatever may be. Is there any way of doing it in Windows??

        Use the Terminal Server Client, or ask the administrator of the system about your options. Once you are clear about the options, you can then ask here again about how these options can be used through Perl.

      1. Create a xyz.RDP file Which will have all information like username,password & ipaddress. 2. Run following command on windows shell start mstsc /con /f "./rdp/xyz.rdp"
Re: Windows Remote Desktop In Perl
by BrowserUk (Pope) on Jan 08, 2010 at 08:44 UTC

    RDC uses a proprietary full GUI terminal protocal RDP that is completely incompatible with Telnet, SSH and similar which are text-only protocols.

    You would have to use the application programming interface. Roku::RCP seems to be related, but I know nothing more about it.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Hello Corion, actually we are using Remote Desktop Connection (i.e. Terminal Server Client) for years. Its really a heck to login and then run scripts in many servers (Its damn slower with GUI). That's why we require a alternate in Perl, so that everything can be done Command Line. So we even tried PsExec.exe which is a windows command line utility for RDC but it shows a lot of error when trying to connect. Is there a way to do all these jobs command line??? Please help me out monks.....

        If you have the appropriate permissions, you can for example use WMI to launch remote processes. But you have not really been forthcoming in your requirements, and your question is about Remote Desktop, not "ways to launch a program on a remote machine".

Re: Windows Remote Desktop In Perl
by Anonymous Monk on Jan 08, 2010 at 09:19 UTC
    Sorry Corion, I don't have permission to use WMI command line utility. I thought i was clear in my requirement. Ok, let me be clear- Instead of using Remote Desktop Connection, Is there a way to connect to a remote system using Dos Prompt (Command Line) and then accessing the files inside it? So that i can add those Dos commands inside my Perl program. Or else, is there a way to access remote computer through any of the Perl modules?? My ultimate aim is to do all the work command line. Thank You.

      There are many ways:

      • Use ssh. This requires you to install an SSH server on the target machines.
      • Use WMI. This requires you to have the appropriate permissions in your domain to launch processes remotely.
      • Have a periodic job on the target machines fetch and run a Perl program that sits at a well-known location. To run a program on a machine, you put the appropriate script into the well-known location.
      • If it's just to access files remotely, just have the machines export the relevant directories via SMB.
      • Use Win32::GUI to automate the Windows Terminal Server Client and send mouse clicks to it.

      I don't know Windows Server 2003, but I'm told that you can operate it through the text console, so I assume that the Microsoft documentation about Windows Server 2003 has information on how to do that. I would read the documentation to learn more about the environment.

        Thank You Corion for your precious reply. Its quite shocking to know there is no module in perl to connect to a remote computer directly as Remote Desktop does without any dependencies. Anyway, will try all the ways you suggested. Thank You once again.
Re: Windows Remote Desktop In Perl
by Anonymous Monk on Jan 08, 2010 at 09:52 UTC
    Actually there is a command line utility which can be used instead of Remote Desktop Connection- psexec.exe (from microsoft) which has a lot of commands to do many operations in the remote server, command line from client. It seems it will be much faster as there will be no GUI and it can be invoked in Perl through system command. But it asks me to enable Admin$ share in the remote server which is actually enabled there. Anyways will figure it out and thank you so much for your help.
Re: Windows Remote Desktop In Perl
by tokpela (Chaplain) on Jan 08, 2010 at 10:15 UTC

    I am not sure if this would work for you but another option could be to use DCOM to access the remote machine. I have not done this so I don't know how difficult it would be to implement.

    The below quote is from an old article Win32: PerlCOM and PerlCtrl

    There is more to the Perl and COM picture than I've discussed here. PerlCOM and controls developed with PerlCtrl can both be used with DCOM (Distributed COM), which provides a low-hassle way to launch COM objects on remote machines. In fact, PerlCOM and controls you develop with PerlCtrl need no modification to work with DCOM. You just need to set their permissions with the DCOMCNFG.EXE utility to allow remote users to launch instances of the objects on your machine.

    But, here are a few links to Perl pages that discuss using DCOM and Perl.

      Thank You so much for your reply Monks!!!!
Re: Windows Remote Desktop In Perl
by Anonymous Monk on Jan 08, 2010 at 12:35 UTC
    Ha!!! Found a solution for my prob :-) I connected the remote computer using Telnet from local and executed all the commands, as i do in remote server after logging on with Remote Desktop. The problem before was, Telnet service was not activated in the remote server. So by typing 'services.msc' in Run, activated the Telnet process (After logging into Remote Server using RDC). This may sound silly but this is really a very big issue for me :-E Especially thanks Corion for helping me and all the monks who replied!!!!
      Hi there,

      I am having the same issue as you mentioned earlier. I started the Telnet services on both the server but the script is failing with a timeout error. Could you please paste the code which is working for you?

      use warnings; use Net::Telnet; $myIPaddress = "Host IP"; $myDefaultPassword = 'pwd'; $myUsername = "username"; $myHostname = "somehostname"; $OutputFile = "$myHostname" . "_" . ".txt"; open (OUTPUT, ">>$OutputFile"); $telnet = new Net::Telnet ( Timeout=>10,Errmode=>'die'); $telnet->open("$myIPaddress"); $telnet->print("$myUsername"); $telnet->print("$myDefaultPassword"); $telnet->print("enable"); @running = $telnet->waitfor("/$myHostname# \$/i"); $running = "@running"; print OUTPUT $running; close (OUTPUT);
      Error -pattern match timed-out at C:\Lokesh\Perl\t.pl line 24 Please help... Thanks,
        Hi Monks I executed same program,I got the following error "Unknown remote Host" ,I enable the telnet services on both servers.Please send the complete code how to connect remote server(windows).
        Hi Muralidharan, Could you please share the working code.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://816239]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-11-23 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (127 votes), past polls