Skip to content
Snippets Groups Projects
Commit c41c2867 authored by gpotter2's avatar gpotter2
Browse files

Small improvements

parent 1998f2f6
No related branches found
No related tags found
No related merge requests found
...@@ -335,7 +335,7 @@ def select_objects(inputs, remain, customTypes=()): ...@@ -335,7 +335,7 @@ def select_objects(inputs, remain, customTypes=()):
r = [] r = []
def look_for_select(): def look_for_select():
for fd in inputs: for fd in inputs:
if isinstance(fd, ObjectPipe) or isinstance(fd, Automaton._IO_fdwrapper) or (isinstance(fd, customTypes)): if isinstance(fd, (ObjectPipe, Automaton._IO_fdwrapper) + customTypes):
if fd.checkRecv(): if fd.checkRecv():
r.append(fd) r.append(fd)
else: else:
......
...@@ -62,6 +62,8 @@ class PipeEngine: ...@@ -62,6 +62,8 @@ class PipeEngine:
raise AttributeError(attr) raise AttributeError(attr)
def checkRecv(self): def checkRecv(self):
"""As select.select is not available, we check if there
is some data to read by using a list that stores pointers."""
return len(self.__fd_queue) > 0 return len(self.__fd_queue) > 0
def fileno(self): def fileno(self):
...@@ -493,46 +495,56 @@ class TermSink(Sink): ...@@ -493,46 +495,56 @@ class TermSink(Sink):
self.opened = False self.opened = False
if self.openearly: if self.openearly:
self.start() self.start()
def _start_windows(self):
def start(self):
if not self.opened: if not self.opened:
self.opened = True self.opened = True
if WINDOWS: self.__f = get_temp_file()
self.__f = get_temp_file() self.name = "Scapy" if self.name is None else self.name
self.name = "Scapy" if self.name is None else self.name # Start a powershell in a new window and print the PID
# Start a powershell in a new window and print the PID cmd = "$app = Start-Process PowerShell -ArgumentList '-command &{$host.ui.RawUI.WindowTitle=\\\"%s\\\";Get-Content \\\"%s\\\" -wait}' -passthru; echo $app.Id" % (self.name, self.__f.replace("\\", "\\\\"))
cmd = "$app = Start-Process PowerShell -ArgumentList '-command &{$host.ui.RawUI.WindowTitle=\\\"%s\\\";Get-Content \\\"%s\\\" -wait}' -passthru; echo $app.Id" % (self.name, self.__f.replace("\\", "\\\\")) _p = subprocess.Popen([conf.prog.powershell, cmd], stdout=subprocess.PIPE)
print([conf.prog.powershell, cmd]) _output, _stderr = _p.communicate()
_p = subprocess.Popen([conf.prog.powershell, cmd], stdout=subprocess.PIPE) # This is the process PID
_output, _stderr = _p.communicate() self.__p = int(_output)
# This is the process PID print("PID:" + str(self.__p))
self.__p = int(_output) def _start_unix(self):
print("PID:" + str(self.__p)) if not self.opened:
else: self.opened = True
self.__r,self.__w = os.pipe() self.__r,self.__w = os.pipe()
cmd = ["xterm"] cmd = ["xterm"]
if self.name is not None: if self.name is not None:
cmd.extend(["-title",self.name]) cmd.extend(["-title",self.name])
if self.keepterm: if self.keepterm:
cmd.append("-hold") cmd.append("-hold")
cmd.extend(["-e", "cat 0<&%i" % self.__r]) cmd.extend(["-e", "cat 0<&%i" % self.__r])
self.__p = subprocess.Popen(cmd, shell=True, executable="/bin/bash") self.__p = subprocess.Popen(cmd, shell=True, executable="/bin/bash")
os.close(self.__r) os.close(self.__r)
def stop(self): def start(self):
if WINDOWS:
return self._start_windows()
else:
return self._start_unix()
def _stop_windows(self):
if not self.keepterm: if not self.keepterm:
self.opened = False self.opened = False
if not WINDOWS: # Recipe to kill process with PID
os.close(self.__w) # http://code.activestate.com/recipes/347462-terminating-a-subprocess-on-windows/
self.__p.kill() import ctypes
self.__p.wait() PROCESS_TERMINATE = 1
else: handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, self.__p)
# Recipe to kill process with PID ctypes.windll.kernel32.TerminateProcess(handle, -1)
# http://code.activestate.com/recipes/347462-terminating-a-subprocess-on-windows/ ctypes.windll.kernel32.CloseHandle(handle)
import ctypes def _stop_unix(self):
PROCESS_TERMINATE = 1 if not self.keepterm:
handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, self.__p) self.opened = False
ctypes.windll.kernel32.TerminateProcess(handle, -1) os.close(self.__w)
ctypes.windll.kernel32.CloseHandle(handle) self.__p.kill()
self.__p.wait()
def stop(self):
if WINDOWS:
return self._stop_windows()
else:
return self._stop_unix()
def _print(self, s): def _print(self, s):
if self.newlines: if self.newlines:
s+="\n" s+="\n"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment