2

I have a .py script with Nmap module in it. It works fine when launched from Visual Studio. But it keeps popping up Nmap console-windows when converted to a stand-alone executable.

So the thing is a tkinter GUI for Windows, that periodically pings and port-scans multiple hosts at a time. It uses Nmap for part of its features. Here is an over-minimized version of the thing:

import nmap as nm
import tkinter as tk
radar = nm.PortScanner()

class App(tk.Tk):
    def __init__(mr):
        tk.Tk.__init__(mr)
        mr.entry = tk.Entry(mr)
        mr.entry.pack()
        mr.entry.bind('<Return>', lambda event: mr.scaner())

    def scaner(mr):
        adr = mr.entry.get()
        report = radar.scan(adr, arguments ='-F --host-timeout 3000ms --max-rtt-timeout 1000ms --max-retries 0 -Pn')    
        try:
            if 'tcp' in radar[adr]:
                mr.entry['background'] = 'green'
            else:
                mr.entry['background'] = 'red'
        except KeyError:
            mr.entry['background'] = 'red'

def Main():
    app = App()
    app.mainloop()
if __name__ == "__main__":
    Main()

You input an IP address into the Entry-box, and press Enter key. If there's a live host, that has any open or filtered TCP-ports on it, the Entry-background turns green. If not, red. The problem arises after converting the whole thing to an .exe file. BTW, here's how:

pyinstaller --onefile --noconsole myscript.py

After that, the .exe works similarly, except for one annoying dumb thing. It launches a console window for about a sec, every time the scan is performed.

Any ideas on how to do the thing without launching those console windows will be much appreciated.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
shu9000
  • 31
  • 4

1 Answers1

1

Update and success. Solved by making a couple of edits in the the nmap.py module.

I found some related topics, where people tackled a similar problem with the 'subprocess' module.

Also, I dug into what's inside my nmap module (nmap.py). And it happend that my nmap module was actually using the 'subprocess' when doing the scans.

All in all the main keywords that helped me with solving the problem were as follows:

subprocess, creationflags, 0x08000000

Now the steps.

  1. Back up the nmap.py file just in case.
  2. In the nmap.py file find all the places, where the 'subprocess.Popen' is is called with some arguments. Add 'creationflags=0x08000000' as an argument in the end. Maybe a coma will be requirered in the end. Or maybe not. I found 3 such places. The resulting blocks may look something like this:
p = subprocess.Popen(some, arguments, blabla, here,  creationflags=0x08000000)
  1. Save the nmap.py and be happy.
shu9000
  • 31
  • 4