[Tutor] Re: Pstools and Python (was: Remote processes)

Bernard Lebel python at bernardlebel.com
Wed Aug 25 09:53:25 CEST 2004


Hello everyone,

In case someone is interested, I have investigated that PsTools thing to
start and stop remote services, and it works like a charm.

The code is attached as text file.


Cheers
Bernard
-------------- next part --------------
# -----------------------------------------------------

# The cmservices submodule is a toolset to handle remote processes.
# It is ideal to manage renderfarm applications, or processes running on user computers.
#
# The cmserives submodule uses the SysInternal's PsTools to handle remote processes
# and services.
#
# -----------------------------------------------------
# Import block

import os
import time

# -----------------------------------------------------




		
def startservice( aClients =[], sUser = '', sPW = '', sAction = '', sServ = '' ):
	
	"""
	 startserv will start a given service on a remote computer.
	 Its only purpose is to tell opserv what operation to perform.
	"""
	
	# Action to perform
	sAction = 'start'
	
	# Call operation function
	opserve( aClients, sUser, sPW, sAction, sServ )




def stopservice( aClients = [], sUser = '', sPW = '', sAction = '', sServ = '' ):
	
	"""
	 stopserv will stop a given service on a remote computer.
	 Its only purpose is to tell opserv what operation to perform.
	"""
	
	# Action to perform
	sAction = 'stop'
	
	# Call operation function
	opserve( aClients, sUser, sPW, sAction, sServ )
	
	
		


def opserve( aClients = [], sUser = '', sPW = '', sAction = '', sServ = '' ):
	
	"""
	opserve pefroms the actual service operation, as told by startserv or stopserv.
	
	 aClient is the list of clients on wich to change the service.
	 sUser is the user name to use (should be 'Administrator' )
	 sPW is the password for the provided user name
	 sAction is the action to perform ('start' or 'stop')
	 sServ is the servive to perform the action on.
	 
	 Example of full command (built by the opserv loop function):
	 sCom = 'psservice \\\\Xw6000r-04 -u Administrateur -p 123cv stop "XSI BatchServe"'
	"""

	# PsTools application
	sPS = 'psservice'
	
	# Check if client list has at least one client
	if len( aClients ) < 1:
		print 'No list of clients provided, use cm.buildlist() or cm.getextlist() to generate client list.'
	else:
		# Check if sUser was provided
		if sUser == '':
			sUser = raw_input( 'User name: ' )
			
		# Define password
		if sPS == '':
			sPW = str( raw_input( 'Password: ' ) )
			
		# Check if service was provided
		if sServ == '':
			sServ = raw_input( 'Service to start: ' )
		
		
		# Change current working directory to go into PsTools directory
		os.chdir( 'C:\\Pstools' )
		
		# Iterate through list of clients to perform operation
		for sClient in aClients:
			# Define command
			sCom = sPS + ' ' + sClient + ' ' + '-u' + ' ' + sUser + ' ' + '-p' + ' ' + sPW + ' ' + sAction + ' ' + '"' + sServ + '"'
			#print 'Command to be executed: ' + sCom
			os.system( sCom )
			
			# If we start services over a large number of clients, we better put a delay
			if sAction == 'start':
				time.sleep( 10 )



More information about the Tutor mailing list