0

I'm trying to bundle my Python codebase into an .exe file using pyinstaller.

My code executes a shell command using nmap. This works fine in development because I have nmap installed separately on my dev environment.

However, if I want run this as an exe on another machine (without installing nmap separately on that machine) I can't seem to do this.

I've looked into the --add-binary option of pyinstaller and this is how I'm using it:

pyinstaller --onefile --add-binary "C:\Program Files (x86)\Nmap\nmap.exe";. --paths=path_to_my_python_file python_file_to_run -n name_of_my_exe_file

The resulting exe file is able to import Python specific dependencies and execute all the other Python code without any difficulties but it cannot run the nmap shell command.

This is how I'm running nmap through Python (full command omitted for brevity):

subprocess.Popen(["powershell.exe", "nmap" )],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
  1. Is it even possible to bundle nmap with my exe file?
  2. If yes, am I doing it correctly?
  3. If not, what are my options? (I can't use any python based nmap libraries like python-nmap etc.)

[UPDATE] Working Solution: Final pyinstaller statement:

pyinstaller --paths=path_to_my_python_file --noconfirm python_file_to_run --noconfirm -n name_of_exe_file 

Used robocopy to copy all the nmap files I needed:

robocopy  path_to_nmap_source_directory path_to_installation_destination_directory /COPYALL /E /NFL /NDL /NJH /NJS /nc /ns /np

Python code (Bundling data files with PyInstaller (--onefile)) to get execution path of nmap:

def get_base_path():
    base_path = ""
    if getattr(sys, 'frozen', False):
            base_path = sys._MEIPASS
    return base_path

def get_exe_path(file):
    #file is the name of your exe file e.g nmap.exe
    base_path = get_base_path()
    exe_path = os.path.join(base_path, file)
    return exe_path
jaimish11
  • 536
  • 4
  • 15
  • Why run it through powershell, which anyway won’t work if nmap.exe isn’t on the path, why not execute it directly? – DisappointedByUnaccountableMod May 24 '21 at 15:03
  • Well in any case I need to bundle nmap with the exe file right? – jaimish11 May 24 '21 at 15:29
  • You won’t have it to run unless you do - never tried bundling an exe but you can bundle binaries so why not try? – DisappointedByUnaccountableMod May 24 '21 at 16:52
  • Okay I'm not quite sure what you're suggesting. Could you elaborate? – jaimish11 May 24 '21 at 17:06
  • See the documentation e.g. https://pyinstaller.readthedocs.io/en/stable/usage.html#what-to-bundle-where-to-search – DisappointedByUnaccountableMod May 24 '21 at 18:31
  • If you look at my pyinstaller command, you'll see that I have included the --add-binary option. I'm just not sure if that is the correct way to do it. That is what you're referring to right? – jaimish11 May 25 '21 at 11:38
  • Looks good - all you need to do now is run nmap.exe from `.` relative to where the exe actually runs from - to get this location see the documentation https://pyinstaller.readthedocs.io/en/stable/runtime-information.html?highlight=_meipass#run-time-information also see this useful function `resource_path` in Jonathon Reinhart’s answer in this question: https://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile/54881911#54881911 – DisappointedByUnaccountableMod May 25 '21 at 12:12
  • I've added the binary like this ```--add-binary "C:\Program Files (x86)\Nmap\nmap.exe;." ``` whereby ```C:\Program Files (x86)\Nmap\nmap.exe``` is where my actual nmap exe is stored. However, this still doesn't work. – jaimish11 May 26 '21 at 07:50
  • Update your question with the details of the error you get, i.e. more than ‘still doesn’t work’. – DisappointedByUnaccountableMod May 26 '21 at 08:03
  • 1
    Updated question with the working solution according to suggestions from @barny – jaimish11 Jun 03 '21 at 10:48

1 Answers1

0

Your best bet would be to use a python implementation of nmap, there are many on pip such as python-nmap, I would suggest downloading a pure python implementation and using that instead of an exe.

  • Yes I'm aware that there are python implementations. However, I can't seem to obtain the same level of detail I need with python-nmap. i.e the shell command returns more comprehensive information. – jaimish11 May 24 '21 at 15:31