Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
XP is just a number
 
PerlMonks  

Remotely Start/Stop Services

by dubl1n (Initiate)
on Feb 01, 2013 at 19:57 UTC ( #1016607=perlquestion: print w/ replies, xml ) Need Help??
dubl1n has asked for the wisdom of the Perl Monks concerning the following question:

Hi All - Let me start off by saying that I am not looking for someone to write a script for me, I am a beginner and I am just looking for someone to push me in the correct direction. Here is my scenario: I have a script written that uses Win32:Service to start and stop services on a local machine, whether that is my machine or one of our servers. What I am looking for is the ability to run the script on my machine and remotely start/stop the services. Right now if I put in a hostname in my script I return nothing. To confuse things a bit I am an admin on the servers but I have an admin account for the servers and my user account for my machine. Any thoughts, any modules that I could look into?

Comment on Remotely Start/Stop Services
Re: Remotely Start/Stop Services
by BrowserUk (Pope) on Feb 01, 2013 at 20:32 UTC
    What I am looking for is the ability to run the script on my machine and remotely start/stop the services.

    To do that, the user id under which you run the program locally will require the permissions to act on the remote machine.

    Right now if I put in a hostname in my script I return nothing.

    "I return nothing" is a piss poor description. I assume you mean that the attempt fails, but that's a great leap from what you written.

    Perhaps you mean something like: "I get nothing returned"?

    But even that fails to say what it is you get nothing returned from; what that "it" is, and what it is trying to do;. Even the nature of the "nothing" is important.

    Ie. What api are you calling? What are you expecting to get returned from that API if it succeeded? What is the "nothing" you are getting back in failure? eg. 0, '', undef?

    HAve you checked $^E after the failure?

    To confuse things a bit I am an admin on the servers but I have an admin account for the servers and my user account for my machine.

    Again, not a good description. I assume that you mean that you run the program local under your user account even though you have admin accounts on the machines you are trying to control.

    As I said above, unless the user ID you use has the appropriate permissions on the remote machine to perform the action requested, it will fail.

    You can either:

    1. give your local user id permissions on the remote machines;
    2. or programmically logon onto the remote system and obtain permissions that way.

    Win32::IntAuth might help you with the latter.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.
      Thanks for the response. My apologies if my descriptions were not correct. Again, I am a beginner trying to grow in my current role. With that said, to respond to your post - I have a user account that I log into my local machine. I also have an admin account that I use to log into our servers. 2 separate accounts. I am trying to run the script on my machine, access the server with my admin credentials and start/stop the services I want. I cannot add my local user to the remote server, that is not an option. My other error where "I get nothing returned" was my error in my script which I have fixed and again, I apologize if I was not more descriptive. I appreciate the suggestion of Win32::IntAuth and see if that could help. Thanks again!
Re: Remotely Start/Stop Services
by dasgar (Deacon) on Feb 01, 2013 at 21:15 UTC

    This is not exactly a pure Perl route, but here's one way to accomplish the task.

    Download PsService or PsExec to the machine that you're running your Perl code on. Then you can do a system call (via exec, system, back ticks, etc.) to call PsService to directly start/stop services on a remote Windows system or call PsExec to remotely run net start or net stop on the remote Windows system.

    Both utilities are free and allow you to provide credentials for the remote system. You might want to manually run them once to deal with the EULA pop up that occurs on the first run.

    (NOTE: The remote system needs to have Remote Procedure Call service running in order to use PsExec. By default, that service should be running. Not sure if PsService requires that or not.)

    May not be a "Perl" solution, but it should get the job done.

Re: Remotely Start/Stop Services
by perl514 (Pilgrim) on Feb 02, 2013 at 01:28 UTC

    Hi,

     

    You can have your script read a text file that has the server names given one below the other. Then, your script would read each line of the text file, pick up the servername, do a ping or an nslookup and if thats successful, it would execute the command. To remotely execute the commands, try the Net::SSH2 Module or Net::SSH::Any module.

    Net::SSH2 comes pre installed with Strawberry Perl or DWIM Perl. Net::SSH::Any you'll have to install and it will run on top of Net::SSH2. Hope this helps.

    Perlpetually Indebted To PerlMonks

    use Learning::Perl; use Beginning::Perl::Ovid; print "Awesome Books";
    http://dwimperl.com/windows.html is a boon for Windows.

      The modules that you suggested would work great if the remote systems were Linux. But it looks like the OP in a Windows only environment. Windows doesn't come with an SSH server. Although someone could install something like openSSH or Cygwin to get an SSH server running on Windows, I'm not entirely sure that the SSH environment in that case would have to utilities to control Windows services. I personally have never tried that so I guess I could be wrong about that.

      Anyways, IMHO I still think it will be much quicker and easier to use PsService or PsExec from the SysInternals suite.

        Hi dasgar,

         

        I have tried these modules on Windows and they work fine.

        If the OP wants to login to servers, he will have to have ssh installed on the Win Workstation from which he will be firing his queries. That is why I suggested the options.

        Perlpetually Indebted To PerlMonks

        use Learning::Perl; use Beginning::Perl::Ovid; print "Awesome Books";
        http://dwimperl.com/windows.html is a boon for Windows.

Re: Remotely Start/Stop Services
by nikosv (Hermit) on Feb 02, 2013 at 12:24 UTC

    I bet that at some point you have controlled,configured,start,stopped, a network printer through a web interface. So why not use the same concept on your remote machine?

    You could set up a http server which would be accessed only by machines in the local network ( no need for Apache there are other more simple to set up ones) and then either set a web form or a web service more likely, that through it you could start and stop the remote service.

    Alternatively, you could Connect to WMI on a Remote Computer and use its Win32_Service class to control your service. A good example can be found Using WMI to manipulate services (Install, Uninstall, Start, Pause etc…)

    But you have to go through many hoops like configuring DCOM, permissions,firewalls etc

Re: Remotely Start/Stop Services
by dubl1n (Initiate) on Feb 06, 2013 at 16:19 UTC
    Thanks all for the suggestions and comments. Most appreciated. I have learned a lot from the suggestions and this project. Thanks Again!!

      For completeness, it would be nice if you would detail which way you went (and if possible, why) for later visitors pursuing similar goals.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-04-19 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (478 votes), past polls