0

I want to create a Python script that opens a cmd in remote Windows machine using psexec, and runs my_program.exe from this cmd, and when some event occurs it sends Ctrl+c to my_program.exe which handles this signal somehow.

Here's my code:

from os import chdir, path
from subprocess import Popen, PIPE


psexec_dir = r'C:\Users\amos1\Downloads\PSTools'
chdir(psexec_dir)
path.join(psexec_dir, 'psexec.exe')

command = ['psexec.exe', '\\amos', 'cmd']
p = Popen(command, stdin = PIPE, stdout = PIPE)
p.stdin.write(b'my_program.exe\r\n')
while True:
    if some_condition:
        ctrl_c = b'\x03'
        p.stdin.write(ctrl_c)
        break
for line in p.stdout.readlines():
    print(line)
p.kill()

The problems:

  • my_program.exe does not run
  • p.kill raises WindowsError: [Error 5] Access is denied (even though I used the answers from here and did both chdir and path.join in my code)
  • Notice that both my computer and the target computer are Windows machines
halfer
  • 19,824
  • 17
  • 99
  • 186
Dudi Frid
  • 152
  • 7
  • If everything is windows, why are you using python? This would be easier to implement in plain cmd or PowerShell – Maximilian Burszley Dec 08 '17 at 21:26
  • @TheIncorrigible1 - for this particular task, yes, but Python is immensely more powerful than batch scripting or PowerShell - even Microsoft themselves are using it for some of their tools. – zwer Dec 08 '17 at 21:30
  • `wmic /node:127.0.0.1 process call create "c:\windows\notepad.exe"` What a load of rubbish about Python. With all the addins python is as powerful as a Microsoft language, but not as standard. You cannot with standard python do the above command but can in all MS languages. – ACatInLove Dec 08 '17 at 21:30
  • Because it's a part of bigger code written in Python (even the condition itself depends on a rather big code in Python) – Dudi Frid Dec 08 '17 at 21:31
  • Power seems irrelevant when you're doing a pretty basic scripting task – Maximilian Burszley Dec 08 '17 at 21:32
  • @ACatInLove could you please ellaborate on this? How does my_condition (which written in Python, and is too complicated to translate to Batch) gets involved with this? – Dudi Frid Dec 08 '17 at 21:40
  • It's how to run a program on another computer in Windows. All Microsoft languages (and python with a COM addin) can do. Above is command prompt. – ACatInLove Dec 08 '17 at 21:43
  • How does PSExec get involved with it. PSExec IS NOT PART OF WINDOWS. You are the one that specified that this is a CMD question. Perhaps go back to snake land. – ACatInLove Dec 08 '17 at 21:49
  • @ACatInLove And how could you send inputs to the program this way? – Dudi Frid Dec 08 '17 at 21:51
  • @ACatInLove in the above code I tried to open cmd using psexec – Dudi Frid Dec 08 '17 at 21:52

0 Answers0