Skip to content

Commit

Permalink
Merge pull request #67 from newlog/windows_support
Browse files Browse the repository at this point in the history
Fix PATH lookup and command line for Windows OS
  • Loading branch information
savon-noir committed Nov 26, 2015
2 parents f6f1f3c + 23a1fc6 commit 00ccad5
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions libnmap/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from threading import Thread
from xml.dom import pulldom
import warnings
import platform
try:
import pwd
except ImportError:
Expand Down Expand Up @@ -87,6 +88,8 @@ def __init__(self, targets="127.0.0.1",
unsafe_opts = set(['-oG', '-oN', '-iL', '-oA', '-oS', '-oX',
'--iflist', '--resume', '--stylesheet',
'--datadir'])
# more reliable than just using os.name() (cygwin)
self.__is_windows = platform.system() == 'Windows'
if fqp:
if os.path.isfile(fqp) and os.access(fqp, os.X_OK):
self.__nmap_binary = fqp
Expand Down Expand Up @@ -153,7 +156,9 @@ def _whereis(self, program):
:todo: add a default path list in case PATH is empty.
"""
for path in os.environ.get('PATH', '').split(':'):
split_char = ';' if self.__is_windows else ':'
program = program + '.exe' if self.__is_windows else program
for path in os.environ.get('PATH', '').split(split_char):
if (os.path.exists(os.path.join(path, program)) and not
os.path.isdir(os.path.join(path, program))):
return os.path.join(path, program)
Expand Down Expand Up @@ -248,8 +253,8 @@ def run(self):
return: return code from nmap execution
"""
self._run_init()

_tmp_cmdline = shlex.split(self.__nmap_command_line)
_tmp_cmdline = self.__build_windows_cmdline() if self.__is_windows \
else shlex.split(self.__nmap_command_line)
try:
self.__nmap_proc = subprocess.Popen(args=_tmp_cmdline,
stdout=subprocess.PIPE,
Expand Down Expand Up @@ -408,6 +413,17 @@ def __process_event(self, eventdata):
pass
return rval

def __build_windows_cmdline(self):
cmdline = []
cmdline.append(self.__nmap_binary)
if self.__nmap_fixed_options:
cmdline += self.__nmap_fixed_options.split()
if self.__nmap_dynamic_options:
cmdline += self.__nmap_dynamic_options.split()
if self.__nmap_targets:
cmdline += self.__nmap_targets # already a list
return cmdline

@property
def command(self):
"""
Expand Down

1 comment on commit 00ccad5

@ShinyGuo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you

Please sign in to comment.