views:

287

answers:

4

Hi,

I decided to tackle Python as a new language to learn. The first thing I want to do is code a script that will allow me to remotely restart services on other machines from my local machine. How would I accomplish this when the remote machine requires a username and password to log on? I don't need a full solution to be given to me but maybe some pointers on what libraries I should use or any issues I need to address when writing the script.

EDIT: All the remote machines are using Windows 2003

+2  A: 

People usually recommend paramiko as a library to do ssh (and I'm assuming that you need ssh to get into the remote machine). There is a good tutorial for it.

Edit: On windows, the easiest way is probably to use SysInternals psservice utility, to be invoked with os.system; this can start a remote service, and accepts logon credentials.

If you want to do it directly in Python, you need win32service.StartService. Before that, you need to open the remote service manager, and then the remote service. Before that, you need to impersonate the user as which you want to perform the operation, see the example.

Martin v. Löwis
os.system() shouldn't be used anymore. Use the subprocess module instead.
Aaron Digulla
+1  A: 

What kind of OS is your remote machine running? If it's linux, run ssh(1) using the subprocess module.

If it's windows, then get the win32 extensions. They allow you to call Windows functions. There should be an API to allow to access services. If they don't, there is a tool called sc (docs) which you can run using the subprocess module.

Aaron Digulla
The machines are using Windows 2003
Draco
+3  A: 

Take a look at Fabric wich is based on paramiko. This is really a good tool to automate remote tasks with python.

Fabric Documentation will show you how easy it is to use.

Pierre-Jean Coudert
+1  A: 

Which OS for the target machines? If 'service' is 'Windows NT service', and your local machine is also Windows, I'd use IronPython as the Python language implementation and call straight into the WMI facilities in the .net System.Management namespace -- they're meant for remote admin like that.

Steve Gilham
The machines are using Windows 2003
Draco