0

I'm working on a script which should run a command in a new subprocess. At the moment im using the python subprocess.Popen() for this. My problem is that I can run commands like "dir" but not a installed program. When I open the cmd prompt and write "program -h" I am getting the normal help outprint. When I try to open it with Popen() I am getting an error that the program can not the found. It is like he is not finding the executable even if the cmd promt can find it when I look for it manually. For the test i used ncat as an example program.

import time
from subprocess import *
import subprocess

execom = "ncat -h"  # DOES NOT WORK IN PYTHON BUT MANUALLY IN CMD
execom1 = "ncat -h -e cmd"  # DOES NOT WORK IN PYTHON BUT MANUALLY IN CMD
execom2 = "dir"  # WORKS
p = Popen(execom, shell=True)

out = p.communicate()[0]

Does someone know how to fix that?

EDIT: I got the solution with the hint of 2e0byo. I added the paths to the systemvariabels but the system needed an extra restart to pass it. It worked without restart from cmd prompt but not from python script. After restart it works from both now.

Duffmanta
  • 3
  • 3
  • what does `os.getenv("PATH")` show? Is the dir containing `ncat` in it? (side not: is `ncat` a proper `.exe`, or is it e.g. a batch script, and in that case, can you just call the `.exe` directly? Regardless, it will likely work if you use the *full* path to the .exe – 2e0byo Oct 11 '21 at 09:25
  • `os.getenv("PATH")` shows the following: `C:\Users\IEUser\PycharmProjects\Interpreter\Scripts;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\ProgramData\chocolatey\bin;C:\Program Files\Puppet Labs\Puppet\bin;C:\Program Files\Git\cmd;C:\Users\IEUser\AppData\Local\Microsoft\WindowsApps;` Ncat is an executable and no script. When i look into the windows settings I see that i have added `C:\Program Files (x86)\Nmap` to Path (for user and for system). "ncat" is a part of my Nmap installation and in this folder. – Duffmanta Oct 11 '21 at 09:35

1 Answers1

1

Per the comments, the problem is just that ncat isn't in your PATH, so you need to add it:

import os
env = os.environ.copy()
env["PATH"] = r"C:\Program Files (x86)\Nmap;" + env["PATH"]
p = Popen(execom, env=env)
...

See this question for modifying the env.

As to why ncat isn't in your path I really don't know; I don't use Micro$oft Windoze very much. Perhaps someone will be along with a canonical answer to that question, or you can ask it over on superuser and see if someone knows how to set it up. If I needed this to be portable I would just check a bunch of folders for ncat and bail if I couldn't find it.

2e0byo
  • 5,305
  • 1
  • 6
  • 26
  • 1
    Thanks for your help. Actually it was added to the sysemvariabels. I could use "ncat" without problems in my cmd prompt. It just needed a restart to discover it for python and i don't know why. The hint to print it in python gave me the solution thanks for that. :) – Duffmanta Oct 11 '21 at 09:48
  • 1
    @Duffmanta Needing to restart to refresh environments is fairly common, particularly on windows, sadly. Hence the old windows 'tech' advice of 'have you tried rebooting it....' – 2e0byo Oct 11 '21 at 09:50
  • Yes i feel very bad about creeping around 2 days on a mistake like this. Actually I develop the script for Linux, but it has to work for windows aswell. -_- In linux it was not a problem at all. – Duffmanta Oct 11 '21 at 09:58