Executing a remote process via WMI in Win32.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sean

    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('wbem scripting.swbem locator')
    remote_machine =
    wmi.ConnectServ er('<MACHINE>', 'root\\cimv2',A dministrator',' <PASSWORD>')

    process = remote_machine. InstancesOf("Wi n32_Process")
    for proc in process:
    size = int(proc.Workin gSetSize)/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:\Documen ts and Settings\scody\ test.py", line 14, in ?
    process.Create( "cmd.exe /K dir w:\\")
    File "C:\Python23\li b\site-packages\win32c om\client\dynam ic.py", line 460,
    in
    __getattr__
    raise AttributeError, "%s.%s" % (self._username _, attr)
    AttributeError: InstancesOf.Cre ate

    The process method should only have the one parameter as defined at:


    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

    #2
    Re: Executing a remote process via WMI in Win32.

    "Sean" <null-spam@tfh.ca> wrote in message news:<becdmd$hb o$1@canopus.cc. umanitoba.ca>.. .[color=blue]
    > 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('wbem scripting.swbem locator')
    > remote_machine =
    > wmi.ConnectServ er('<MACHINE>', 'root\\cimv2',A dministrator',' <PASSWORD>')
    >
    > process = remote_machine. InstancesOf("Wi n32_Process")
    > for proc in process:
    > size = int(proc.Workin gSetSize)/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:\Documen ts and Settings\scody\ test.py", line 14, in ?
    > process.Create( "cmd.exe /K dir w:\\")
    > File "C:\Python23\li b\site-packages\win32c om\client\dynam ic.py", line 460,
    > in
    > __getattr__
    > raise AttributeError, "%s.%s" % (self._username _, attr)
    > AttributeError: InstancesOf.Cre ate
    >
    > 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...[/color]



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

    I´m use ActiveState Python 2.2.1 firts you have it´s 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 don´t know why

    Example create process:
    (After Use Makepy)
    import win32com.client
    wmi = win32com.client .Dispatch('wbem scripting.swbem locator')
    remote_machine =
    wmi.ConnectServ er('<MACHINE>', 'root\\cimv2',A dministrator',' <PASSWORD>')
    strProcess=("cm d.exe /K & dir w:\\") #this is very important you must
    put &
    objprocess = remote_machine. Get('Win32_Proc ess')
    Method = objProcess.Meth ods_('Create')
    InParameters = Method.InParame ters.SpawnInsta nce_()
    InParameters.Pr operties_.Item( 'CommandLine'). Value=strProces s
    try:
    OutParameters = objProcess.Exec Method_ ('Create',InPar ameters)
    ReturnValue = OutParameters.P roperties_.Item ('ReturnValue') .Value
    ProcessID = OutParameters.P roperties_.Item ('ProcessId').V alue
    except pythoncom.com_e rror, (hr,msg,exc,arg ):
    ExceptionHandle r(hr,msg,exc,ar g)

    def ExceptionHandle r (self,hr,msg,ex c,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,helpI D)


    Example kill process:

    strPid="'yourpi d'" # In this form else not work
    sql="SELECT * FROM Win32_Process WHERE ProcessId=%s" %strPid
    objProc=objproc ess.ExecQuery(s ql)
    if objProc.Count== 0:
    print "Pid strPid Not Found"
    else:
    for process in objProc:
    strmeth = process.Methods _('Terminate')
    strparms=strmet h.InParameters
    strparms.Proper ties_('Reason') .Value=0
    process.ExecMet hod_('Terminate ',strparms)

    Sorry Sean for my bad Enghlis I hope help you

    Comment

    Working...