Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Executing a remote process via WMI in Win32. (http://www.velocityreviews.com/forums/t319351-executing-a-remote-process-via-wmi-in-win32.html)

Sean 07-07-2003 06:20 PM

Executing a remote process via WMI in Win32.
 
I can connect to a machine remotely with no problems but I'm having trouble
trying to create a process remotely. Initially this was a perl and VB
script which I'm converting to python. Its entire purpose is to start a
process remotely. The problem initially was that the perl script could not
'see' any mapped drives as it would alway return 'access denied' so I'm
trying to not only rewrite the script but fix this problem and my idea was
to connect with the Administrator account.

Now the following script sort of works. I can get the set of running
processes but it dies out when I try to create one.

## being ##
import win32com.client
wmi = win32com.client.Dispatch('wbemscripting.swbemlocat or')
remote_machine =
wmi.ConnectServer('<MACHINE>','root\\cimv2',Admini strator','<PASSWORD>')

process = remote_machine.InstancesOf("Win32_Process")
for proc in process:
size = int(proc.WorkingSetSize)/1024
print proc.Caption, size,"kb"
# test to see if we can 'see' what's on a mapped drive.
# doesn't seem to go.
process.Create("cmd.exe /K dir w:\\")
## END ##

The script will print out all the processes fine but once it get's to
'Create' it dies out with and error message I don't understand.

NOTEPAD.EXE 80 kb
Traceback (most recent call last):
File "C:\Documents and Settings\scody\test.py", line 14, in ?
process.Create("cmd.exe /K dir w:\\")
File "C:\Python23\lib\site-packages\win32com\client\dynamic.py", line 460,
in
__getattr__
raise AttributeError, "%s.%s" % (self._username_, attr)
AttributeError: InstancesOf.Create

The process method should only have the one parameter as defined at:
http://userpages.umbc.edu/~kbradl1/w...f.html#process

I just tried using the 'Terminate' process but that didn't work either....

So I'm going to assume that the WMI objects are not instantiated as python
classes (hence why the methods are not working). Am I completely off base
here (meaning it is a syntax problem)?

Any suggestions or pointers to the right direction would be creately
appriciated...

--
Sean
(ps. remove -spam to email me).



jose maria 07-10-2003 12:41 PM

Re: Executing a remote process via WMI in Win32.
 
"Sean" <null-spam@tfh.ca> wrote in message news:<becdmd$hbo$1@canopus.cc.umanitoba.ca>...
> I can connect to a machine remotely with no problems but I'm having trouble
> trying to create a process remotely. Initially this was a perl and VB
> script which I'm converting to python. Its entire purpose is to start a
> process remotely. The problem initially was that the perl script could not
> 'see' any mapped drives as it would alway return 'access denied' so I'm
> trying to not only rewrite the script but fix this problem and my idea was
> to connect with the Administrator account.
>
> Now the following script sort of works. I can get the set of running
> processes but it dies out when I try to create one.
>
> ## being ##
> import win32com.client
> wmi = win32com.client.Dispatch('wbemscripting.swbemlocat or')
> remote_machine =
> wmi.ConnectServer('<MACHINE>','root\\cimv2',Admini strator','<PASSWORD>')
>
> process = remote_machine.InstancesOf("Win32_Process")
> for proc in process:
> size = int(proc.WorkingSetSize)/1024
> print proc.Caption, size,"kb"
> # test to see if we can 'see' what's on a mapped drive.
> # doesn't seem to go.
> process.Create("cmd.exe /K dir w:\\")
> ## END ##
>
> The script will print out all the processes fine but once it get's to
> 'Create' it dies out with and error message I don't understand.
>
> NOTEPAD.EXE 80 kb
> Traceback (most recent call last):
> File "C:\Documents and Settings\scody\test.py", line 14, in ?
> process.Create("cmd.exe /K dir w:\\")
> File "C:\Python23\lib\site-packages\win32com\client\dynamic.py", line 460,
> in
> __getattr__
> raise AttributeError, "%s.%s" % (self._username_, attr)
> AttributeError: InstancesOf.Create
>
> The process method should only have the one parameter as defined at:
> http://userpages.umbc.edu/~kbradl1/w...f.html#process
>
> I just tried using the 'Terminate' process but that didn't work either....
>
> So I'm going to assume that the WMI objects are not instantiated as python
> classes (hence why the methods are not working). Am I completely off base
> here (meaning it is a syntax problem)?
>
> Any suggestions or pointers to the right direction would be creately
> appriciated...




Hi Sean Im now work in a module that create several kind of methods
to manage process using wmi take one example:

Im use ActiveState Python 2.2.1 firts you have its that python know
of existencie of WMI for instance you must use COMMakepy utility now
Python *knows* about the 'WMI Scripting' typelibrary.

Note:this part of script its tested in windows 2000 and its functional
except in windows 2000 with SP2 that failed and I dont know why

Example create process:
(After Use Makepy)
import win32com.client
wmi = win32com.client.Dispatch('wbemscripting.swbemlocat or')
remote_machine =
wmi.ConnectServer('<MACHINE>','root\\cimv2',Admini strator','<PASSWORD>')
strProcess=("cmd.exe /K & dir w:\\") #this is very important you must
put &
objprocess = remote_machine.Get('Win32_Process')
Method = objProcess.Methods_('Create')
InParameters = Method.InParameters.SpawnInstance_()
InParameters.Properties_.Item('CommandLine').Value =strProcess
try:
OutParameters = objProcess.ExecMethod_ ('Create',InParameters)
ReturnValue = OutParameters.Properties_.Item('ReturnValue').Valu e
ProcessID = OutParameters.Properties_.Item('ProcessId').Value
except pythoncom.com_error, (hr,msg,exc,arg):
ExceptionHandler(hr,msg,exc,arg)

def ExceptionHandler (self,hr,msg,exc,arg):
print 'COM call failed: %s' % msg
if exc is not None:
wcode, source, text, helpFile, helpID, scode = exc
print 'Error source:',source
print text
print 'See also: %s (id=%d)' % (helpFile,helpID)


Example kill process:

strPid="'yourpid'" # In this form else not work
sql="SELECT * FROM Win32_Process WHERE ProcessId=%s" %strPid
objProc=objprocess.ExecQuery(sql)
if objProc.Count==0:
print "Pid strPid Not Found"
else:
for process in objProc:
strmeth = process.Methods_('Terminate')
strparms=strmeth.InParameters
strparms.Properties_('Reason').Value=0
process.ExecMethod_('Terminate',strparms)

Sorry Sean for my bad Enghlis I hope help you


All times are GMT. The time now is 02:01 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.