def run(self, args):
try:
if not self.client.conn.modules["os.path"].exists("C:\\WIndows\\system32\\Packet.dll"):
raise PupyModuleError("WinPcap is not installed !. You should download/upload NPcap (https://github.com/nmap/npcap/releases) and install it silently (with the /S flag) ")
if not self.client.conn.modules['ctypes'].windll.Shell32.IsUserAnAdmin():
self.warning("you are running this module without beeing admin")
with redirected_stdo(self.client.conn):
old_completer=readline.get_completer()
try:
psc=self.client.conn.modules['pyshell.controller'].PyShellController()
readline.set_completer(psc.get_completer())
readline.parse_and_bind('tab: complete')
psc.write("from scapy.all import *")
while True:
cmd=raw_input(">>> ")
psc.write(cmd)
finally:
readline.set_completer(old_completer)
readline.parse_and_bind('tab: complete')
except KeyboardInterrupt:
pass
python类get_completer()的实例源码
def run(self, args):
try:
with redirected_stdo(self.client.conn):
old_completer=readline.get_completer()
try:
psc=self.client.conn.modules['pyshell.controller'].PyShellController()
readline.set_completer(psc.get_completer())
readline.parse_and_bind('tab: complete')
while True:
cmd=raw_input(">>> ")
psc.write(cmd)
finally:
readline.set_completer(old_completer)
readline.parse_and_bind('tab: complete')
except KeyboardInterrupt:
pass
def do_python(self,arg):
""" start the local python interpreter (for debugging purposes) """
orig_exit=builtins.exit
orig_quit=builtins.quit
def disabled_exit(*args, **kwargs):
self.display_warning("exit() disabled ! use ctrl+D to exit the python shell")
builtins.exit=disabled_exit
builtins.quit=disabled_exit
oldcompleter=readline.get_completer()
try:
local_ns={"pupsrv":self.pupsrv}
readline.set_completer(PythonCompleter(local_ns=local_ns).complete)
readline.parse_and_bind('tab: complete')
code.interact(local=local_ns)
except Exception as e:
self.display_error(str(e))
finally:
readline.set_completer(oldcompleter)
readline.parse_and_bind('tab: complete')
builtins.exit=orig_exit
builtins.quit=orig_quit
def run(self, args):
try:
if not self.client.conn.modules["os.path"].exists("C:\\WIndows\\system32\\Packet.dll"):
raise PupyModuleError("WinPcap is not installed !. You should download/upload NPcap (https://github.com/nmap/npcap/releases) and install it silently (with the /S flag) ")
if not self.client.conn.modules['ctypes'].windll.Shell32.IsUserAnAdmin():
self.warning("you are running this module without beeing admin")
with redirected_stdo(self.client.conn):
old_completer=readline.get_completer()
try:
psc=self.client.conn.modules['pyshell.controller'].PyShellController()
readline.set_completer(psc.get_completer())
readline.parse_and_bind('tab: complete')
psc.write("from scapy.all import *")
while True:
cmd=raw_input(">>> ")
psc.write(cmd)
finally:
readline.set_completer(old_completer)
readline.parse_and_bind('tab: complete')
except KeyboardInterrupt:
pass
def run(self, args):
try:
with redirected_stdo(self.client.conn):
old_completer=readline.get_completer()
try:
psc=self.client.conn.modules['pyshell.controller'].PyShellController()
readline.set_completer(psc.get_completer())
readline.parse_and_bind('tab: complete')
while True:
cmd=raw_input(">>> ")
psc.write(cmd)
finally:
readline.set_completer(old_completer)
readline.parse_and_bind('tab: complete')
except KeyboardInterrupt:
pass
def do_python(self,arg):
""" start the local python interpreter (for debugging purposes) """
orig_exit=builtins.exit
orig_quit=builtins.quit
def disabled_exit(*args, **kwargs):
self.display_warning("exit() disabled ! use ctrl+D to exit the python shell")
builtins.exit=disabled_exit
builtins.quit=disabled_exit
oldcompleter=readline.get_completer()
try:
local_ns={"pupsrv":self.pupsrv}
readline.set_completer(PythonCompleter(local_ns=local_ns).complete)
readline.parse_and_bind('tab: complete')
code.interact(local=local_ns)
except Exception as e:
self.display_error(str(e))
finally:
readline.set_completer(oldcompleter)
readline.parse_and_bind('tab: complete')
builtins.exit=orig_exit
builtins.quit=orig_quit
def run(self, line):
ishellCompleter = readline.get_completer()
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.set_completer(completer.complete)
sample_file = raw_input('Please enter the filename you want to open: ')
try:
sample_fh = open(sample_file,'r')
feathermodules.samples.extend([sample.strip() for sample in sample_fh.readlines()])
sample_fh.close()
feathermodules.samples = filter(lambda x: x != '' and x != None, feathermodules.samples)
except:
print 'Something went wrong. Sorry! Please try again.'
finally:
readline.set_completer(ishellCompleter)
def run(self, line):
ishellCompleter = readline.get_completer()
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.set_completer(completer.complete)
sample_file = raw_input('Please enter the filename you want to open: ')
try:
sample_fh = open(sample_file,'r')
feathermodules.samples.append(sample_fh.read())
sample_fh.close()
feathermodules.samples = filter(lambda x: x != '' and x != None, feathermodules.samples)
except:
print 'Something went wrong. Sorry! Please try again.'
finally:
readline.set_completer(ishellCompleter)
def pre_input(self, completefn):
if self.raw_input:
if HAVE_READLINE:
import atexit
self.old_completer = readline.get_completer()
# Fix Bug #3129: Limit the history size to consume less memory
readline.set_history_length(self.historysize)
readline.set_completer(completefn)
readline.parse_and_bind(self.completekey+": complete")
try:
readline.read_history_file()
except IOError:
pass
atexit.register(readline.write_history_file)
self.havecolor = True
if mswindows and self.enablecolor:
self.cwrite = readline.GetOutputFile().write_color
else:
self.cwrite = self.stdout.write
def pre_input(self, completefn):
if self.raw_input:
if HAVE_READLINE:
import atexit
self.old_completer = readline.get_completer()
# Fix Bug #3129: Limit the history size to consume less memory
readline.set_history_length(self.historysize)
readline.set_completer(completefn)
readline.parse_and_bind(self.completekey+": complete")
try:
readline.read_history_file()
except IOError:
pass
atexit.register(readline.write_history_file)
self.havecolor = True
if mswindows and self.enablecolor:
self.cwrite = readline.GetOutputFile().write_color
else:
self.cwrite = self.stdout.write
def __enter__(self):
self._original_completer = readline.get_completer()
self._original_delims = readline.get_completer_delims()
readline.set_completer(self.complete)
readline.set_completer_delims(' \t\n;')
# readline can be implemented using GNU readline or libedit
# which have different configuration syntax
if 'libedit' in readline.__doc__:
readline.parse_and_bind('bind ^I rl_complete')
else:
readline.parse_and_bind('tab: complete')
def test_context_manager_with_unmocked_readline(self):
from certbot.display import completer
reload_module(completer)
original_completer = readline.get_completer()
original_delims = readline.get_completer_delims()
with completer.Completer():
pass
self.assertEqual(readline.get_completer(), original_completer)
self.assertEqual(readline.get_completer_delims(), original_delims)
def __enter__(self):
# we only do something if we have readline
if not haveReadline:
return self
## Set up the new history context
self.historyFile = os.path.join( os.getenv('HOME'), ".{0}.history".format(self.basename))
(h, self.oldhistFile) = tempfile.mkstemp(prefix=self.basename, suffix=".hist", dir="/tmp")
# only need the filename, really
os.close(h)
readline.write_history_file(self.oldhistFile)
readline.clear_history()
# if reading the old history fails, fail silently
# (the file might not exist yet)
try:
readline.read_history_file(self.historyFile)
except:
pass
# store the old completer, install our own one
readline.parse_and_bind("tab: complete")
#readline.parse_and_bind("C-c: backward-kill-line")
self.oldCompleter = readline.get_completer()
readline.set_completer(self.completer)
return self
# clean up the context
def __enter__(self):
self._original_completer = readline.get_completer()
self._original_delims = readline.get_completer_delims()
readline.set_completer(self.complete)
readline.set_completer_delims(' \t\n;')
# readline can be implemented using GNU readline or libedit
# which have different configuration syntax
if 'libedit' in readline.__doc__:
readline.parse_and_bind('bind ^I rl_complete')
else:
readline.parse_and_bind('tab: complete')
def test_context_manager_with_unmocked_readline(self):
from certbot.display import completer
reload_module(completer)
original_completer = readline.get_completer()
original_delims = readline.get_completer_delims()
with completer.Completer():
pass
self.assertEqual(readline.get_completer(), original_completer)
self.assertEqual(readline.get_completer_delims(), original_delims)
def run(self, line):
ishellCompleter = readline.get_completer()
embed()
readline.set_completer(ishellCompleter)
def run(self, line):
def _formatOutput(res):
if isinstance(res, str):
return res
else:
try:
return "\n".join(_formatOutput(r) for r in res)
except TypeError:
return str(res)
ishellCompleter = readline.get_completer()
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab: complete")
readline.set_completer(completer.complete)
filePath = raw_input("Please specify a path to the output file: ").strip()
readline.set_completer(ishellCompleter)
if os.path.isfile(filePath):
confirm = raw_input("File already exists and will be overwritten, confirm? [y/N] ")
if confirm is "" or confirm[0] not in ("y", "Y"):
print "Canceled."
return
with open(filePath, "w+") as handle:
handle.write(_formatOutput(feathermodules.results))
def _cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
"""
# An almost perfect copy from Cmd; however, the pseudo_raw_input portion
# has been split out so that it can be called separately
self.preloop()
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey+": complete")
except ImportError:
pass
try:
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro)+"\n")
stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
line = self.pseudo_raw_input(self.prompt)
if (self.echo) and (isinstance(self.stdin, file)):
self.stdout.write(line + '\n')
stop = self.onecmd_plus_hooks(line)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass
return stop
def _cmdloop(self):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
"""
# An almost perfect copy from Cmd; however, the pseudo_raw_input portion
# has been split out so that it can be called separately
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey + ": complete")
except ImportError:
pass
stop = None
try:
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
line = self.pseudo_raw_input(self.prompt)
if self.echo and isinstance(self.stdin, file):
self.stdout.write(line + '\n')
stop = self.onecmd_plus_hooks(line)
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass
return stop
def completions(self, text):
completer = readline.get_completer()
i = 0
completions = []
while True:
completion = completer(text, i)
if completion is None:
break
completions.append(completion)
i += 1
return completions
def cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
"""
self.preloop()
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey+": complete")
except ImportError:
pass
try:
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro)+"\n")
stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
line = raw_input(self.prompt)
except EOFError:
line = 'EOF'
else:
self.stdout.write(self.prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
line = line.rstrip('\r\n')
line = self.precmd(line)
stop = self.onecmd(line)
stop = self.postcmd(stop, line)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass
def cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
"""
self.preloop()
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey+": complete")
except ImportError:
pass
try:
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro)+"\n")
stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
line = raw_input(self.prompt)
except EOFError:
line = 'EOF'
else:
self.stdout.write(self.prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
line = line.rstrip('\r\n')
line = self.precmd(line)
stop = self.onecmd(line)
stop = self.postcmd(stop, line)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass
def cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
"""
self.preloop()
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey+": complete")
except ImportError:
pass
try:
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro)+"\n")
stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
line = raw_input(self.prompt)
except EOFError:
line = 'EOF'
else:
self.stdout.write(self.prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
line = line.rstrip('\r\n')
line = self.precmd(line)
stop = self.onecmd(line)
stop = self.postcmd(stop, line)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass
def _cmdloop(self):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
This serves the same role as cmd.cmdloop().
:return: bool - True implies the entire application should exit.
"""
# An almost perfect copy from Cmd; however, the pseudo_raw_input portion
# has been split out so that it can be called separately
if self.use_rawinput and self.completekey:
try:
self.old_completer = readline.get_completer()
self.old_delims = readline.get_completer_delims()
readline.set_completer(self.complete)
# Don't treat "-" as a readline delimiter since it is commonly used in filesystem paths
readline.set_completer_delims(self.old_delims.replace('-', ''))
readline.parse_and_bind(self.completekey + ": complete")
except NameError:
pass
stop = None
try:
while not stop:
if self.cmdqueue:
# Run command out of cmdqueue if nonempty (populated by load command or commands at invocation)
line = self.cmdqueue.pop(0)
if self.echo and line != 'eos':
self.poutput('{}{}'.format(self.prompt, line))
else:
# Otherwise, read a command from stdin
line = self.pseudo_raw_input(self.prompt)
# Run the command along with all associated pre and post hooks
stop = self.onecmd_plus_hooks(line)
finally:
if self.use_rawinput and self.completekey:
try:
readline.set_completer(self.old_completer)
readline.set_completer_delims(self.old_delims)
except NameError:
pass
# Need to set empty list this way because Python 2 doesn't support the clear() method on lists
self.cmdqueue = []
self._script_dir = []
return stop
# noinspection PyUnusedLocal
def cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
"""
self.preloop()
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey+": complete")
except ImportError:
pass
try:
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro)+"\n")
stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
line = raw_input(self.prompt)
except EOFError:
line = 'EOF'
else:
self.stdout.write(self.prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
line = line.rstrip('\r\n')
line = self.precmd(line)
stop = self.onecmd(line)
stop = self.postcmd(stop, line)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass
def run_interactive_gtp_session(engine):
"""Run a GTP engine session on stdin and stdout, using readline.
engine -- Gtp_engine_protocol object
This enables readline tab-expansion, and command history in
~/.gomill-gtp-history (if readline is available).
Returns either when EOF is seen on stdin, or when the engine signals end of
session.
If stdin isn't a terminal, this is equivalent to run_gtp_session.
If a write fails with 'broken pipe', this raises ControllerDisconnected.
Note that this will propagate KeyboardInterrupt if the user presses ^C;
normally you'll want to handle this to avoid an ugly traceback.
"""
# readline doesn't do anything if stdin isn't a tty, but it's simplest to
# just not import it in that case.
try:
use_readline = os.isatty(sys.stdin.fileno())
if use_readline:
import readline
except Exception:
use_readline = False
if not use_readline:
run_gtp_session(engine, sys.stdin, sys.stdout)
return
def write(s):
sys.stdout.write(s)
sys.stdout.flush()
history_pathname = os.path.expanduser("~/.gomill-gtp-history")
readline.parse_and_bind("tab: complete")
old_completer = readline.get_completer()
old_delims = readline.get_completer_delims()
readline.set_completer(make_readline_completer(engine))
readline.set_completer_delims("")
try:
readline.read_history_file(history_pathname)
except EnvironmentError:
pass
_run_gtp_session(engine, raw_input, write)
try:
readline.write_history_file(history_pathname)
except EnvironmentError:
pass
readline.set_completer(old_completer)
readline.set_completer_delims(old_delims)
def cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
"""
self.preloop()
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey+": complete")
except ImportError:
pass
try:
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro)+"\n")
stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
import sys
sys.stdout.write(self.prompt)
line = raw_input()
except EOFError:
line = 'EOF'
else:
self.stdout.write(self.prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
line = line.rstrip('\r\n')
line = self.precmd(line)
stop = self.onecmd(line)
stop = self.postcmd(stop, line)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass
def cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
"""
self.preloop()
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey+": complete")
except ImportError:
pass
try:
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro)+"\n")
stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
line = input(self.prompt)
except EOFError:
line = 'EOF'
else:
self.stdout.write(self.prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
line = line.rstrip('\r\n')
line = self.precmd(line)
stop = self.onecmd(line)
stop = self.postcmd(stop, line)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass
def cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
"""
self.preloop()
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey+": complete")
except ImportError:
pass
try:
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro)+"\n")
stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
line = raw_input(self.prompt)
except EOFError:
line = 'EOF'
else:
self.stdout.write(self.prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
line = line.rstrip('\r\n')
line = self.precmd(line)
stop = self.onecmd(line)
stop = self.postcmd(stop, line)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass
def cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
"""
self.preloop()
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey+": complete")
except ImportError:
pass
try:
if intro is not None:
self.intro = intro
if self.intro:
self.stdout.write(str(self.intro)+"\n")
stop = None
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
line = raw_input(self.prompt)
except EOFError:
line = 'EOF'
else:
self.stdout.write(self.prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
line = line.rstrip('\r\n')
line = self.precmd(line)
stop = self.onecmd(line)
stop = self.postcmd(stop, line)
self.postloop()
finally:
if self.use_rawinput and self.completekey:
try:
import readline
readline.set_completer(self.old_completer)
except ImportError:
pass