Search code examples
pythonlinuxpython-3.xpopen

Nmap not found when run from Python script


I have written basic port scanner for target ip and when I run it through kali vm it says sh: 1: nmap-F192.168.234.135: not found. but when I run nmap -F 192.168.234.135 ... its perfectly working. Can anyone point out the reason behind it. thanks

import os

def get_nmap(options,ip):
    command = "nmap" + options + "" + ip
    process = os.popen(command)
    result = str(process.read())
    return result

print(get_nmap('-F','192.168.234.135'))

Solution

  • Better, using the subprocess module:

    def get_nmap(options, ip) :
        return subprocess.check_output(["nmap", options, ip])
    #end get_nmap