Search code examples
pythonsubprocesssecure-crt

python subprocess use


Sorry if this question has been asked 100 times before, unfortunately, i haven't found it if it was. I am looking to write a python script which will spawn a background process and monitor it to see if it has completed. I have seen the os.system command which is now recommended to utilize subprocess.call, but this seems to block the program until the process completes.

My end goal is to have the following:

1) Program runs and starts processessing sing information

2) Halfway through the program it will start a Windows DOS program which will need to run simultaneously

3) Program continues to run and keeps processing data.

4) Program monitors the DOS program and when it exits and then sends final information. But the DOS program must have exited for it to run this step.

If someone could point me in the right direction that would be great.


More info: I am trying to utilize the python processor built into Securecrt. It appears to open the CMD window fine for the program, but it does not print out any of the middle commands (just enable's in this example) until i kill the subprocess.

import os

import subprocess


def Main()

    crt.Screen.Synchronous = True

    VMCOMMAND = '\"C:\\Program Files\\VMWare\\VMware OVF Tool\\ovftool.exe\" ' + '--name=test' + ' C:\\temp\\test.ova' + ' vi://root:test@1.1.1.1'

    from subprocess import Popen


    p = Popen(VMCOMMAND)

    crt.Screen.Send(chr(13))
    crt.Screen.Send("enable" + chr(13))
    crt.Screen.Send("enable" + chr(13))
    crt.Screen.Send("enable" + chr(13))
    crt.Screen.Send("enable" + chr(13))


    returncode = p.wait()

    crt.Screen.Send("should not see enable" + chr(13))

Main ()

Solution

  • Use Popen constructor:

    from subprocess import Popen
    
    # start program
    p = Popen(["program.exe", "arg1"])
    # do other stuff here
    # ...
    # at the end wait for the program to exit
    returncode = p.wait()
    # run final step here