if __name__ == '__main__': import sys import getopt import os.path # in production builds we don't need the stupid warnings if sys.platform == "darwin" and len( sys.argv) > 1 and sys.argv[1].startswith("-psn_"): # get rid of the process serial number on mac sys.argv = sys.argv[:1] + sys.argv[2:] try: _options, _args = getopt.gnu_getopt(sys.argv[1:], 'c:d:f:p:') _invalid_args=[x for x in _args if x not in ['debug', 'bitfling'] and \ not bp_cli.valid_command(x)] if _invalid_args: raise getopt.GetoptError('Invalid argument(s): ' + ','.join(_invalid_args)) except getopt.GetoptError: e = sys.exc_info()[1] _invalid_args = True _error_str = e.msg if _invalid_args: # invalid/unknown arguments try: import wx import guihelper _msg = '%s\nUsage: %s [-c config file]|[-d config dir] [-p comm port] [-f phone model] [CLI Command] [debug] [bitfling]\n' % ( _error_str, sys.argv[0]) sys.stderr.write(_msg) # try to display an error message box _app = wx.PySimpleApp() guihelper.MessageDialog(None,
def interact(mydict=None, argv=None, mybanner=None, loglevel=logging.INFO): # type: (Optional[Any], Optional[Any], Optional[Any], int) -> None """ Starts Scapy's console. """ # We're in interactive mode, let's throw the DeprecationWarnings warnings.simplefilter("always") # Set interactive mode, load the color scheme from scapy.config import conf conf.interactive = True conf.color_theme = DefaultTheme() if loglevel is not None: conf.logLevel = loglevel STARTUP_FILE = DEFAULT_STARTUP_FILE PRESTART_FILE = DEFAULT_PRESTART_FILE session_name = None if argv is None: argv = sys.argv try: opts = getopt.getopt(argv[1:], "hs:Cc:Pp:d:H") for opt, param in opts[0]: if opt == "-h": _usage() elif opt == "-H": conf.fancy_prompt = False conf.verb = 30 elif opt == "-s": session_name = param elif opt == "-c": STARTUP_FILE = param elif opt == "-C": STARTUP_FILE = None elif opt == "-p": PRESTART_FILE = param elif opt == "-P": PRESTART_FILE = None elif opt == "-d": conf.logLevel = max(1, conf.logLevel - 10) if len(opts[1]) > 0: raise getopt.GetoptError("Too many parameters : [%s]" % " ".join(opts[1])) except getopt.GetoptError as msg: log_loading.error(msg) sys.exit(1) # Reset sys.argv, otherwise IPython thinks it is for him sys.argv = sys.argv[:1] SESSION, GLOBKEYS = init_session(session_name, mydict) if STARTUP_FILE: _read_config_file(STARTUP_FILE, interactive=True) if PRESTART_FILE: _read_config_file(PRESTART_FILE, interactive=True) if not conf.interactive_shell or conf.interactive_shell.lower() in [ "ipython", "auto" ]: try: import IPython from IPython import start_ipython except ImportError: log_loading.warning( "IPython not available. Using standard Python shell " "instead.\nAutoCompletion, History are disabled.") if WINDOWS: log_loading.warning("On Windows, colors are also disabled") conf.color_theme = BlackAndWhite() IPYTHON = False else: IPYTHON = True else: IPYTHON = False if conf.fancy_prompt: from scapy.utils import get_terminal_width mini_banner = (get_terminal_width() or 84) <= 75 the_logo = [ " ", " aSPY//YASa ", " apyyyyCY//////////YCa ", " sY//////YSpcs scpCY//Pp ", " ayp ayyyyyyySCP//Pp syY//C ", " AYAsAYYYYYYYY///Ps cY//S", " pCCCCY//p cSSps y//Y", " SPPPP///a pP///AC//Y", " A//A cyP////C", " p///Ac sC///a", " P////YCpc A//A", " scccccp///pSP///p p//Y", " sY/////////y caa S//P", " cayCyayP//Ya pY/Ya", " sY/PsY////YCc aC//Yp ", " sc sccaCY//PCypaapyCP//YSs ", " spCPY//////YPSps ", " ccaacs ", " ", ] # Used on mini screens the_logo_mini = [ " .SYPACCCSASYY ", "P /SCS/CCS ACS", " /A AC", " A/PS /SPPS", " YP (SC", " SPS/A. SC", " Y/PACC PP", " PY*AYC CAA", " YYCY//SCYP ", ] the_banner = [ "", "", " |", " | Welcome to Scapy", " | Version %s" % conf.version, " |", " | https://github.com/secdev/scapy", " |", " | Have fun!", " |", ] if mini_banner: the_logo = the_logo_mini the_banner = [x[2:] for x in the_banner[3:-1]] the_banner = [""] + the_banner + [""] else: quote, author = choice(QUOTES) the_banner.extend(_prepare_quote(quote, author, max_len=39)) the_banner.append(" |") banner_text = "\n".join( logo + banner for logo, banner in six.moves.zip_longest(( conf.color_theme.logo(line) for line in the_logo), (conf.color_theme.success(line) for line in the_banner), fillvalue="")) else: banner_text = "Welcome to Scapy (%s)" % conf.version if mybanner is not None: banner_text += "\n" banner_text += mybanner if IPYTHON: banner = banner_text + " using IPython %s\n" % IPython.__version__ try: from traitlets.config.loader import Config except ImportError: log_loading.warning( "traitlets not available. Some Scapy shell features won't be " "available.") try: start_ipython(display_banner=False, user_ns=SESSION, exec_lines=["print(\"\"\"" + banner + "\"\"\")"]) except Exception: code.interact(banner=banner_text, local=SESSION) else: cfg = Config() try: from IPython import get_ipython if not get_ipython(): raise ImportError except ImportError: # Set "classic" prompt style when launched from # run_scapy(.bat) files Register and apply scapy # color+prompt style apply_ipython_style(shell=cfg.TerminalInteractiveShell) cfg.TerminalInteractiveShell.confirm_exit = False cfg.TerminalInteractiveShell.separate_in = u'' if int(IPython.__version__[0]) >= 6: cfg.TerminalInteractiveShell.term_title_format = ("Scapy v%s" % conf.version) # As of IPython 6-7, the jedi completion module is a dumpster # of fire that should be scrapped never to be seen again. cfg.Completer.use_jedi = False else: cfg.TerminalInteractiveShell.term_title = False cfg.HistoryAccessor.hist_file = conf.histfile cfg.InteractiveShell.banner1 = banner # configuration can thus be specified here. try: start_ipython(config=cfg, user_ns=SESSION) except (AttributeError, TypeError): code.interact(banner=banner_text, local=SESSION) else: code.interact(banner=banner_text, local=SESSION) if conf.session: save_session(conf.session, SESSION) for k in GLOBKEYS: try: del (six.moves.builtins.__dict__[k]) except Exception: pass
def interact(mydict=None, argv=None, mybanner=None, loglevel=20): global session import code, sys, os, getopt, re from scapy.config import conf conf.interactive = True if loglevel is not None: conf.logLevel = loglevel console_handler = logging.StreamHandler() console_handler.setFormatter( logging.Formatter("%(levelname)s: %(message)s")) log_scapy.addHandler(console_handler) the_banner = "Welcome to Scapy (%s)" if mybanner is not None: the_banner += "\n" the_banner += mybanner if argv is None: argv = sys.argv import atexit try: import rlcompleter, readline except ImportError: log_loading.info("Can't load Python libreadline or completer") READLINE = 0 else: READLINE = 1 class ScapyCompleter(rlcompleter.Completer): def global_matches(self, text): matches = [] n = len(text) for lst in [dir(__builtin__), session]: for word in lst: if word[:n] == text and word != "__builtins__": matches.append(word) return matches def attr_matches(self, text): m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text) if not m: return expr, attr = m.group(1, 3) try: object = eval(expr) except: try: object = eval(expr, session) except (NameError, AttributeError): return from scapy.packet import Packet, Packet_metaclass if isinstance(object, Packet) or isinstance( object, Packet_metaclass): words = filter(lambda x: x[0] != "_", dir(object)) words += [x.name for x in object.fields_desc] else: words = dir(object) if hasattr(object, "__class__"): words = words + rlcompleter.get_class_members( object.__class__) matches = [] n = len(attr) for word in words: if word[:n] == attr and word != "__builtins__": matches.append("%s.%s" % (expr, word)) return matches readline.set_completer(ScapyCompleter().complete) readline.parse_and_bind("C-o: operate-and-get-next") readline.parse_and_bind("tab: complete") session = None session_name = "" STARTUP_FILE = DEFAULT_STARTUP_FILE PRESTART_FILE = DEFAULT_PRESTART_FILE iface = None try: opts = getopt.getopt(argv[1:], "hs:Cc:Pp:d") for opt, parm in opts[0]: if opt == "-h": _usage() elif opt == "-s": session_name = parm elif opt == "-c": STARTUP_FILE = parm elif opt == "-C": STARTUP_FILE = None elif opt == "-p": PRESTART_FILE = parm elif opt == "-P": PRESTART_FILE = None elif opt == "-d": conf.logLevel = max(1, conf.logLevel - 10) if len(opts[1]) > 0: raise getopt.GetoptError("Too many parameters : [%s]" % " ".join(opts[1])) except getopt.GetoptError, msg: log_loading.error(msg) sys.exit(1)
NUM.append(int(v)) except ValueError: v1, v2 = map(int, v.split("-")) for vv in range(v1, v2 + 1): NUM.append(vv) elif opt == "-m": MODULES.append(optarg) elif opt == "-k": KW_OK.append(optarg.split(",")) elif opt == "-K": KW_KO.append(optarg.split(",")) try: from scapy import all as scapy except ImportError, e: raise getopt.GetoptError("cannot import [%s]: %s" % (SCAPY, e)) for m in MODULES: try: mod = import_module(m) __builtin__.__dict__.update(mod.__dict__) except ImportError, e: raise getopt.GetoptError("cannot import [%s]: %s" % (m, e)) except getopt.GetoptError, msg: print >> sys.stderr, "ERROR:", msg raise SystemExit autorun_func = { Format.TEXT: scapy.autorun_get_text_interactive_session, Format.ANSI: scapy.autorun_get_ansi_interactive_session,
def RemoveNamespace(elem): for it in elem.getiterator(): prefix, has_namespace, postfix = it.tag.partition('}') if has_namespace: it.tag = postfix """ Main """ if __name__ == "__main__": argv = sys.argv[1:] try: if len(argv) < 10: raise getopt.GetoptError('') opts, args = getopt.getopt( argv, "d:s:w:n:e:", ["directory", "south", "west", "north", "east"]) except getopt.GetoptError: print( '--directory <dir> --south <lat> --west <lon> --north <lat> --east <lon>' ) sys.exit(2) d = r'' s = -90 w = -180 n = 90 e = 180
def set_config(self, argv): """ 录入从命令行获取的参数值,只处理长格式 :param argv: run as set_config(sys.argv[1:]) """ config_list = [] for part_name in UtilParameter.part: if self.config_menu[part_name] is False: continue for k in self.config_menu[part_name].keys(): config_list.append(k + '=') config_list.append(part_name + '.' + k + '=') try: options, args = getopt.getopt(argv, "", config_list) except Exception: raise getopt.GetoptError("Wrong argv-format") from Exception # sys.exit() # argv格式错误,返回错误信息 for option, value in options: option = option.replace('-', '') opt = option.split('.') if len(opt) == 1: num = 0 for part_name in UtilParameter.part: if option in self.config_menu[part_name].keys(): num += 1 val = get_value( self.config_menu[part_name][option]["type"], value) if val is None: raise KeyError( "Wrong assignment for '{}.{}'".format( str(part_name), str(option))) from Exception # 出现错误的变量类型,返回错误信息 else: self.config[part_name][option] = val if num == 0: raise Exception("Unknown parameter '{}'".format( str(option))) # 发现不存在该变量,返回错误信息 elif num > 1: raise Exception("One more parameters named '{}'".format( str(option))) # 发现存在同名变量,返回错误信息 else: part_name = opt[0] option = opt[1] try: val = get_value( self.config_menu[part_name][option]["type"], value) if val is None: raise KeyError("Wrong assignment for '{}.{}'".format( str(part_name), str(option))) from Exception # 出现错误的变量类型,返回错误信息 else: self.config[part_name][option] = val except Exception: raise KeyError("Unknown parameter '{}.{}'".format( str(part_name), str(option))) from Exception
def main(argv): ignore_globals = list(six.moves.builtins.__dict__.keys()) # Parse arguments FORMAT = Format.ANSI TESTFILE = sys.stdin OUTPUTFILE = sys.stdout LOCAL = 0 NUM = None KW_OK = [] KW_KO = [] DUMP = 0 CRC = True ONLYFAILED = False VERB = 3 GLOB_PREEXEC = "" PREEXEC_DICT = {} SCAPY = "scapy" MODULES = [] TESTFILES = [] try: opts = getopt.getopt(argv, "o:t:T:c:f:hln:m:k:K:DdCFqP:s:") for opt, optarg in opts[0]: if opt == "-h": usage() elif opt == "-F": ONLYFAILED = True elif opt == "-q": VERB -= 1 elif opt == "-D": DUMP = 2 elif opt == "-d": DUMP = 1 elif opt == "-C": CRC = False elif opt == "-s": SCAPY = optarg elif opt == "-P": GLOB_PREEXEC += "\n" + optarg elif opt == "-f": try: FORMAT = Format.from_string(optarg) except KeyError as msg: raise getopt.GetoptError("Unknown output format %s" % msg) elif opt == "-t": TESTFILES.append(optarg) TESTFILES = resolve_testfiles(TESTFILES) elif opt == "-T": TESTFILES.remove(optarg) elif opt == "-c": data = parse_config_file(optarg, VERB) ONLYFAILED = data.onlyfailed VERB = data.verb DUMP = data.dump CRC = data.crc SCAPY = data.scapy PREEXEC_DICT = data.preexec GLOB_PREEXEC = data.global_preexec OUTPUTFILE = data.outfile TESTFILES = data.testfiles LOCAL = 1 if data.local else 0 NUM = data.num MODULES = data.modules KW_OK = [data.kw_ok] KW_KO = [data.kw_ko] try: FORMAT = Format.from_string(data.format) except KeyError as msg: raise getopt.GetoptError("Unknown output format %s" % msg) TESTFILES = resolve_testfiles(TESTFILES) elif opt == "-o": OUTPUTFILE = open(optarg, "w") elif opt == "-l": LOCAL = 1 elif opt == "-n": NUM = [] for v in (x.strip() for x in optarg.split(",")): try: NUM.append(int(v)) except ValueError: v1, v2 = [int(e) for e in v.split('-', 1)] NUM.extend(range(v1, v2 + 1)) elif opt == "-m": MODULES.append(optarg) elif opt == "-k": KW_OK.append(optarg.split(",")) elif opt == "-K": KW_KO.append(optarg.split(",")) if VERB > 2: print("### Booting scapy...", file=sys.stderr) try: from scapy import all as scapy except ImportError as e: raise getopt.GetoptError("cannot import [%s]: %s" % (SCAPY, e)) for m in MODULES: try: mod = import_module(m) six.moves.builtins.__dict__.update(mod.__dict__) except ImportError as e: raise getopt.GetoptError("cannot import [%s]: %s" % (m, e)) except getopt.GetoptError as msg: print("ERROR:", msg, file=sys.stderr) raise SystemExit autorun_func = { Format.TEXT: scapy.autorun_get_text_interactive_session, Format.ANSI: scapy.autorun_get_ansi_interactive_session, Format.HTML: scapy.autorun_get_html_interactive_session, Format.LATEX: scapy.autorun_get_latex_interactive_session, Format.XUNIT: scapy.autorun_get_text_interactive_session, } if VERB > 2: print("### Starting tests...", file=sys.stderr) glob_output = "" glob_result = 0 glob_title = None UNIQUE = len(TESTFILES) == 1 # Resolve tags and asterix for prex in PREEXEC_DICT.keys(): if "*" in prex: pycode = PREEXEC_DICT[prex] del PREEXEC_DICT[prex] for gl in glob.iglob(prex): _pycode = pycode.replace( "%name%", os.path.splitext(os.path.split(gl)[1])[0]) PREEXEC_DICT[gl] = _pycode pos_begin = 0 runned_campaigns = [] # Execute all files for TESTFILE in TESTFILES: if VERB > 2: print("### Loading:", TESTFILE, file=sys.stderr) PREEXEC = PREEXEC_DICT[ TESTFILE] if TESTFILE in PREEXEC_DICT else GLOB_PREEXEC output, result, campaign = execute_campaign(open(TESTFILE), OUTPUTFILE, PREEXEC, NUM, KW_OK, KW_KO, DUMP, FORMAT, VERB, ONLYFAILED, CRC, autorun_func, pos_begin, ignore_globals) runned_campaigns.append(campaign) pos_begin = campaign.end_pos if UNIQUE: glob_title = campaign.title glob_output += output if not result: glob_result = 1 break if VERB > 2: print("### Writing output...", file=sys.stderr) # Concenate outputs if FORMAT == Format.HTML: glob_output = pack_html_campaigns(runned_campaigns, glob_output, LOCAL, glob_title) OUTPUTFILE.write(glob_output.encode("utf8")) OUTPUTFILE.close() # Return state return glob_result
def execute(self, argv): try: opts, args = getopt.gnu_getopt(argv, 'i:v', [ 'instance=', 'verbose', 'debug', 'help']) except getopt.GetoptError as e: logger.error(e) self.print_help() sys.exit(1) instance_name = 'pki-tomcat' for o, a in opts: if o in ('-i', '--instance'): instance_name = a elif o == '--debug': logging.getLogger().setLevel(logging.DEBUG) elif o in ('-v', '--verbose'): logging.getLogger().setLevel(logging.INFO) elif o == '--help': self.print_help() sys.exit() else: logger.error('Unknown option: %s', o) self.print_help() sys.exit(1) if len(args) == 0: raise getopt.GetoptError("Missing event name.") if len(args) > 1: raise getopt.GetoptError("Too many arguments specified.") event_name = args[0] instance = pki.server.instance.PKIServerFactory.create(instance_name) if not instance.exists(): logger.error('Invalid instance %s.', instance_name) sys.exit(1) instance.load() subsystem_name = self.parent.parent.name subsystem = instance.get_subsystem(subsystem_name) if not subsystem: logger.error('No %s subsystem in instance %s.', subsystem_name.upper(), instance_name) sys.exit(1) disable = subsystem.disable_audit_event(event_name) subsystem.save() msg = None if disable: msg = 'Audit event "{}" disabled. You may need to restart the ' \ 'instance.'.format(event_name) else: msg = 'Audit event "{}" already disabled.'.format(event_name) print(len(msg) * '-') print(msg) print(len(msg) * '-') event = subsystem.get_audit_event_config(event_name) AuditCLI.print_audit_event_config(event)
def main(): global logging, log_file print() print("AIQ version 1.0") print() # get the command line arguments try: opts, args = getopt.getopt(sys.argv[1:], "r:d:l:a:n:s:t:", ["help", "log", "simple_mc"]) except getopt.GetoptError(err): print(str(err)) usage() sys.exit(2) agent_str = None refm_str = None disc_rate = None episode_length = None cluster_node = "" simple_mc = False sample_size = None agent_params = [] refm_params = [] threads = 0 # exit on no arguments if opts == []: usage() sys.exit() # parse arguments for opt, arg in opts: if opt == "-a": args = arg.split(",") agent_str = args.pop(0) for a in args: agent_params.append(float(a)) elif opt == "-r": args = arg.split(",") refm_str = args.pop(0) for a in args: refm_params.append(float(a)) elif opt == "-d": disc_rate = float(arg) elif opt == "-l": episode_length = int(arg) elif opt == "-s": sample_size = int(arg) elif opt == "-n": cluster_node = "_" + arg elif opt == "-t": threads = int(arg) elif opt == "--log": logging = True elif opt == "--simple_mc": simple_mc = True else: print("Unrecognised option") usage() sys.exit() # basic parameter checks if agent_str == None: raise NameError("missing agent") if refm_str == None: raise NameError("missing reference machine") if disc_rate == None: disc_rate = 1.0 if logging and simple_mc: raise NameError("Simple mc doesn't do logging") if agent_str == "Manual" and not simple_mc: raise NameError("Manual control only works with the simple mc sampler") # compute episode_length to have 95% of the infinite total in each episode # or if episode_length given compute the proportion that this gives proportion_of_total = 0.95 if episode_length == None: if disc_rate == 1.0: print( "With a discount rate of 1.0 you must set the episode length.") print() usage() sys.exit() else: episode_length = int( log(1.0 - proportion_of_total) / log(disc_rate)) else: proportion_of_total = 1.0 - disc_rate**episode_length # construct refrence machine refm_call = refm_str + "." + refm_str + "( " if len(refm_params) > 0: refm_call += str(refm_params.pop(0)) for param in refm_params: refm_call += ", " + str(param) refm_call += " )" refm = eval(refm_call) # construct agent agent_call = agent_str + "." + agent_str + "( refm, " + str(disc_rate) for param in agent_params: agent_call += ", " + str(param) agent_call += " )" agent = eval(agent_call) # report settings print("Reference machine: " + str(refm)) print("RL Agent: " + str(agent)) print("Discount rate: " + str(disc_rate)) print("Episode length: " + str(episode_length)), if disc_rate != 1.0: print(" which covers %3.1f%% of the infinite geometric total" \ % (100.0*proportion_of_total)) else: print() if agent == "Manual()" and not simple_mc: print("Error: Manual agent only works with simple_mc sampling") sys.exit() if disc_rate != 1.0 and proportion_of_total < 0.75: print() print( "WARNING: The episode length is too short for this discout rate!") print() print("Sample size: " + str(sample_size)) # load in program samples samples, dist = load_samples(refm, cluster_node, simple_mc) if sample_size == None: sample_size = len(sample_data) # The following is a crude check as we can still run out of samples in a # stratum depending on how the adaptive stratification decides to sample. if sample_size > 2.0 * len(sample_data): print() print("Error: More samples have been requested than are available in " \ "the program sample file! (including fact that they are sampled twice)") sys.exit() # report logging if logging: log_file_name = "./log/" + str(refm) + "_" + str(disc_rate) + "_" \ + str(episode_length) + "_" + str(agent) + cluster_node \ + strftime("_%Y_%m%d_%H_%M_%S",localtime()) + ".log" log_file = open(log_file_name, 'w') for i in range(1, len(dist)): log_file.write(str(dist[i]) + " ") log_file.write("\n") log_file.flush() print("Logging to file: " + log_file_name) # run an estimation algorithm if simple_mc: simple_mc_estimator(refm_call, agent_call, episode_length, disc_rate, sample_size) else: # Kill agent and pass in its constructor call, this is because on Windows # some agents have trouble serialising which messes up the multiprocessing # library that Python uses. Easier just to construct the agent inside the # method that gets called in parallel. agent = None stratified_estimator(refm_call, agent_call, episode_length, disc_rate, samples, sample_size, dist, threads) # close log file if logging: log_file.close()
pyperclip.copy(code) return code else: print("Error: Connection Timeout") time.sleep(2) return else: print(f"Error: Paste '{paste}'\n is not a (h/p)astebin link") time.sleep(2) return if __name__ == "__main__": # needs to run with arguments try: opts, args = getopt.getopt(sys.argv[1:], "c:p", ["copy=", "paste"]) for opt, arg in opts: if opt in ("-c", "--copy"): if arg == "pastebin": pastebin() elif arg == "hastebin": hastebin() else: raise getopt.GetoptError(f"Arg not allowed: {arg}") if opt in ("-p", "--paste"): link_to_clipboard() except getopt.GetoptError: print( "phastebin.py { { -c | --copy } { pastebin | hastebin } } | { -p | --paste } " ) sys.exit(2)
def execute(self, argv): try: opts, args = getopt.gnu_getopt(argv, 'i:f:v', [ 'instance=', 'filter=', 'verbose', 'debug', 'help']) except getopt.GetoptError as e: logger.error(e) self.print_help() sys.exit(1) instance_name = 'pki-tomcat' event_filter = None for o, a in opts: if o in ('-i', '--instance'): instance_name = a elif o in ('-f', '--filter'): event_filter = a elif o == '--debug': logging.getLogger().setLevel(logging.DEBUG) elif o in ('-v', '--verbose'): logging.getLogger().setLevel(logging.INFO) elif o == '--help': self.print_help() sys.exit() else: logger.error('Unknown option: %s', o) self.print_help() sys.exit(1) if len(args) == 0: raise getopt.GetoptError("Missing event name.") if len(args) > 1: raise getopt.GetoptError("Too many arguments specified.") event_name = args[0] instance = pki.server.instance.PKIServerFactory.create(instance_name) if not instance.exists(): logger.error('Invalid instance %s.', instance_name) sys.exit(1) instance.load() subsystem_name = self.parent.parent.name subsystem = instance.get_subsystem(subsystem_name) if not subsystem: logger.error('No %s subsystem in instance %s.', subsystem_name.upper(), instance_name) sys.exit(1) subsystem.update_audit_event_filter(event_name, event_filter) subsystem.save() event = subsystem.get_audit_event_config(event_name) AuditCLI.print_audit_event_config(event)
target = "\xff\xff\xff\xff\xff\xff" dev = "eth0" slptime = 2 for opt, parm in opts[0]: if opt == "-h": usage() elif opt == "-t": target = mac2str(parm) elif opt == "-i": dev = parm elif opt == "-s": try: slptime = float(parm) except ValueError, msg: raise getopt.GetoptError( "'sleep' parameter error: " + msg.__repr__(), None) if len(opts[1]) == 0: raise getopt.GetoptError("[host] parameter missing", None) elif len(opts[1]) > 1: raise getopt.GetoptError( "Too many parameters : [%s]" % string.join(opts[1]), None) else: host = opts[1][0] print "dev:", dev print "target:", str2mac(target) print "host:", host except getopt.error, msg: print "ERROR:", msg usage()
rom.sfc The ROM file to load. Must be specified after all options. save.srm The SRAM to load (optional). Must be specified after the ROM. Warning: Won't be updated or overwritten during or after emulation. """.format(sys.argv[0], libsnes, joymap_arg) # parse arguments try: opts, args = getopt.getopt(sys.argv[1:], "hl:f:s:j:", ["help", "libsnes=", "joymap="]) if len(args) < 1: raise getopt.GetoptError('Must specify one ROM argument.') for o,a in opts: if o in ('-h', '--help'): usage() exit(0) elif o in ('-l', '--libsnes'): libsnes = a elif o in ('-j', '--joymap'): if len(a) != 12: raise getopt.GetoptError('--joymap must specify a string of length 12.') joymap_arg = a except Exception, e: print str(e), usage() sys.exit(1)
def main(argv): collection = None try: opts, args = getopt.getopt(argv, "h:o:d:u:p:c", [ "host=", "output=", "db=", "username="******"password="******"collection=" ]) if len(argv) not in [8, 10]: raise getopt.GetoptError("Incorrect number of arguments") except getopt.GetoptError as e: print(e.msg) print( 'Usage is: CollectionDBtoXML.py --host <dbhost> --output <outputfile> --db <database> ' '--collection <collectionid> --username <dbusername> --password <dbuserpass>' ) sys.exit() for opt, arg in opts: if opt in ("-h", "--host"): host = arg elif opt in ("-o", "--output"): output_file = arg elif opt in ("-d", "--db"): db = arg elif opt in ("-u", "--username"): username = arg elif opt in ("-p", "--password"): password = arg elif opt in ("-c", "--collection"): collection = arg conn = None try: conn = psycopg2.connect(host=host, database=db, user=username, password=password) if collection: outputCollectionXML(collection, conn, output_file) # TODO We could extend this script to iterate through all collections. # else: # create a cursor # cur = conn.cursor() # cur.execute('SELECT collectionid from collections') # row = cur.fetchone() # collection = row[0] # cur.close # while row is not None: # outputCollectionXML(collection, conn, output_file) except (Exception, psycopg2.DatabaseError) as error: print(error) finally: if conn is not None: conn.close() print('Database connection closed.') print("done.")
def initialyze(argv): global HMMSCAN_PROGRAM, RPSBLAST_PROGRAM, RPSBPROC_PROGRAM, TMHMMSCAN_PROGRAM, SEGMASKER_PROGRAM, INPUT_FILE_FASTA, PROCESS_TYPE, OUTPUT_RPSBLAST_OR_HMMSCAN, \ OUTPUT_RPSBPROC, OUTPUT_TMHMMSCAN, OUTPUT_SEGMASKER, HMMSCAN_DB_PATH, RPSBLAST_DB_PATH, RPSBPROC_DB_PATH, DB_NAME, CPU, EVAL_THRESHOLD, HMMSCAN_PROBABILITY, \ GET_TAB, GET_JSON, RUN_SEGMASKER, PROTEIN_TO_DOMAININFO_FILE, DOMAIN_ARCHITECT_TO_COUNT_FILE, PROTEIN_TO_DOMAININFO_JSONFILE, PROTEIN_SITES_INCLUDE try: opts, args = getopt.getopt(argv[1:],"hi:p:r:f:x:a:A:B:C:D:H:R:P:T:S:E:e:y:t:j:u:m:o:n:b:",["ifile=", "iprocess=", "ofourth=", "ofifth=", "osixth=", "oseventh", "hmmscanDbPath=", \ "rpsblastDbPath=", "rpsprocDbPath=", "dbname=", "hmmscanPath=", "rpsblastPath", "rpsbprocPath", "tmhmm2Path", "segmaskerPath", "runSegmasker", "evalue=", "tabformat=", "jsonformat=", "cpu=", "sites=", "ofile=", "osecond=", "othird="]) if len(opts) == 0: raise getopt.GetoptError("Options are required\n") except getopt.GetoptError as e: print "===========ERROR==========\n " + str(e) + USAGE sys.exit(2) try: for opt, arg in opts: if opt == '-h': print USAGE sys.exit() elif opt in ("-i", "--ifile"): INPUT_FILE_FASTA = str(arg).strip() elif opt in ("-p", "--iprocess"): processType = str(arg).strip() if processType in ["hmmscan", "rpsblast"]: PROCESS_TYPE = processType else: raise Exception( "-p || --iprocess argument should be 'hmmscan' or 'rpsblast'" ) elif opt in ("-u", "--cpu"): CPU = str(arg).strip() elif opt in ("-r", "--ofourth"): OUTPUT_RPSBLAST_OR_HMMSCAN = str(arg).strip() elif opt in ("-f", "--ofifth"): OUTPUT_RPSBPROC = str(arg).strip() elif opt in ("-x", "--osixth"): OUTPUT_TMHMMSCAN = str(arg).strip() elif opt in ("-a", "--oseventh"): OUTPUT_SEGMASKER = str(arg).strip() elif opt in ("-A", "--hmmscanDbPath"): HMMSCAN_DB_PATH = str(arg).strip() elif opt in ("-B", "--rpsblastDbPath"): RPSBLAST_DB_PATH = str(arg).strip() elif opt in ("-C", "--rpsprocDbPath"): RPSBPROC_DB_PATH = str(arg).strip() elif opt in ("-D", "--dbname"): DB_NAME = str(arg).strip() elif opt in ("-H", "--hmmscanPath"): HMMSCAN_PROGRAM = str(arg).strip() elif opt in ("-R", "--rpsblastPath"): RPSBLAST_PROGRAM = str(arg).strip() elif opt in ("-P", "--rpsbprocPath"): RPSBPROC_PROGRAM = str(arg).strip() elif opt in ("-T", "--tmhmm2Path"): TMHMMSCAN_PROGRAM = str(arg).strip() elif opt in ("-S", "--segmaskerPath"): SEGMASKER_PROGRAM = str(arg).strip() elif opt in ("-E", "--runSegmasker"): if str(arg).strip() == "true": RUN_SEGMASKER = True elif opt in ("-e", "--evalue"): EVAL_THRESHOLD = float(arg) elif opt in ("-y", "--probability"): HMMSCAN_PROBABILITY = float(arg) elif opt in ("-t", "--tabformat"): if str(arg) in ["no", "n"]: GET_TAB = False elif str(arg) in ["yes", "y"]: GET_TAB = True else: raise Exception( "-t || --tabformat argument should be 'yes||y' or 'no||n'" ) elif opt in ("-j", "--jsonformat"): if str(arg) in ["no", "n"]: GET_JSON = False elif str(arg) in ["yes", "y"]: GET_JSON = True else: raise Exception( "-j || --jsonformat argument should be 'yes||y' or 'no||n'" ) elif opt in ("-o", "--ofile"): PROTEIN_TO_DOMAININFO_FILE = str(arg).strip() elif opt in ("-n", "--osecond"): DOMAIN_ARCHITECT_TO_COUNT_FILE = str(arg).strip() elif opt in ("-b", "--othird"): PROTEIN_TO_DOMAININFO_JSONFILE = str(arg).strip() elif opt in ("-m", "--sites"): PROTEIN_SITES_INCLUDE = True if PROCESS_TYPE == None: raise Exception("-p || --iprocess is a mandatory parameter") if PROCESS_TYPE == RPSBLAST: if OUTPUT_RPSBLAST_OR_HMMSCAN == None or OUTPUT_RPSBPROC == None: raise Exception( "-r and -f (||--ofourth and --ofifth) are both mandatory parameters if -p(||--iprocess) is 'rpsblast'" ) except Exception as e: print "===========ERROR==========\n " + str(e) + USAGE sys.exit(2)
def main(): # arguments data_dir = '' data_file = '' datalocal_dir = '' lexicon_dir = '' lm_order = 0 try: opts, args = getopt.getopt(sys.argv[1:], "ha:d:f:l:o:x:", [ "--datadir=", "--datafile=", "--localdir=", "--lmorder=", "--lexicondir=" ]) if len(opts) != 5: raise getopt.GetoptError("Missing arguments") except (getopt.GetoptError, msg): print(msg) usage() print(sys.argv[1:]) sys.exit(2) for opt, arg in opts: if opt == '-h': usage() sys.exit() elif opt in ("-d", "--datadir"): data_dir = arg print("-d, --datatdir:" + data_dir) elif opt in ("-f", "--datafile"): data_file = arg print("-f,--data_file: " + data_file) elif opt in ("-l", "--localdir"): datalocal_dir = arg print("-l, --localdir: " + datalocal_dir) elif opt in ("-x", "--lexicondir"): lexicon_dir = arg print("-x, --lexicondir: " + lexicon_dir) elif opt in ("-o", "--lmorder"): lm_order = arg print("-o, --lmorder: " + lm_order) else: assert False, "unhandled option" if not os.path.exists(datalocal_dir): os.makedirs(datalocal_dir) datalocal_tmpdir = os.path.join(datalocal_dir, "tmp") if not os.path.exists(datalocal_tmpdir): os.makedirs(datalocal_tmpdir) print("Sanitizing text data ....") initialise_lexicon(lexicon_dir) sanitize_file(os.path.join(data_dir, data_file), datalocal_dir, os.path.join(datalocal_dir, "corpus.txt")) print("Creating ngram-count using SRILM ....") #call(["ngram-count","-version"]) print("text file; " + os.path.join(datalocal_dir, "corpus.txt")) print("vocab file: " + os.path.join(datalocal_tmpdir, "vocab-full.txt")) print("lm file: " + os.path.join(datalocal_tmpdir, "lm.arpa")) ngram_count_call = [ "ngram-count", "-order", lm_order, "-write-vocab", os.path.join(datalocal_tmpdir, "vocab-full.txt"), "-wbdiscount", "-text", os.path.join(datalocal_dir, "corpus.txt"), "-lm", os.path.join(datalocal_tmpdir, "lm.arpa") ] gz_call = ["gzip", os.path.join(datalocal_tmpdir, "lm.arpa")] call(ngram_count_call) call(gz_call) print("Completed creating ngram-count")
for o, a in opts: if o in ("-h", "--help"): print(usage) sys.exit(0) elif o in ("-v", "--version"): print(VERSION) sys.exit(0) elif o in ("--offset"): base = 10 if a[:2].lower() == '0x': base = 16 try: offset = int(a, base) except: raise getopt.GetoptError('Bad offset value') if not args: raise getopt.GetoptError('Input file is not specified') if len(args) > 2: raise getopt.GetoptError('Too many arguments') except getopt.GetoptError: msg = sys.exc_info()[1] # current exception txt = 'ERROR: ' + str( msg) # that's required to get not-so-dumb result from 2to3 tool print(txt) print(usage) sys.exit(2)
def __init__(self, rawOptions, numFiles): self.onlyTables = [] self.skipTables = [] self.fontNumber = -1 for option, value in rawOptions: # general options if option == "-h": from fontTools import version print(__doc__ % version) sys.exit(0) elif option == "-d": if not os.path.isdir(value): raise getopt.GetoptError( "The -d option value must be an existing directory") self.outputDir = value elif option == "-o": self.outputFile = value elif option == "-f": self.overWrite = True elif option == "-v": self.verbose = True elif option == "-q": self.quiet = True # dump options elif option == "-l": self.listTables = True elif option == "-t": # pad with space if table tag length is less than 4 value = value.ljust(4) self.onlyTables.append(value) elif option == "-x": self.skipTables.append(value) elif option == "-s": self.splitTables = True elif option == "-i": self.disassembleInstructions = False elif option == "-z": validOptions = ('raw', 'row', 'bitwise', 'extfile') if value not in validOptions: raise getopt.GetoptError( "-z does not allow %s as a format. Use %s" % (option, validOptions)) self.bitmapGlyphDataFormat = value elif option == "-y": self.fontNumber = int(value) # compile options elif option == "-m": self.mergeFile = value elif option == "-b": self.recalcBBoxes = False elif option == "-a": self.allowVID = True elif option == "-e": self.ignoreDecompileErrors = False elif option == "--unicodedata": self.unicodedata = value elif option == "--recalc-timestamp": self.recalcTimestamp = True elif option == "--flavor": self.flavor = value if self.mergeFile and self.flavor: print("-m and --flavor options are mutually exclusive") sys.exit(2) if self.onlyTables and self.skipTables: raise getopt.GetoptError( "-t and -x options are mutually exclusive") if self.mergeFile and numFiles > 1: raise getopt.GetoptError( "Must specify exactly one TTX source file when using -m")
'no-shading-crosshatch', 'shading-avoid-outline', 'pause-at-start', 'no-pause-at-start', 'min-x=', 'max-x=', 'min-y=', 'max-y=', 'no-shading-avoid-outline', 'shading-darkest=', 'shading-lightest=', 'stroke-all', 'no-stroke-all', 'gcode-pause', 'dump-options', 'tab=', 'extract-color=', 'sort', 'no-sort', 'simulation', 'no-simulation', 'tool-offset=', 'overcut=', 'boolean-shading-crosshatch=', 'boolean-sort=', 'tool-mode=', 'send-and-save=', 'direction=', 'lift-command=', 'down-command=', 'init-code=', 'comment-delimiters=', 'end-code=' ], ) if len(args) + len(opts) == 0: raise getopt.GetoptError("invalid commandline") i = 0 while i < len(opts): opt, arg = opts[i] if opt in ('-r', '--allow-repeats'): doDedup = False elif opt == '--no-allow-repeats': doDedup = True elif opt in ('-w', '--gcode-pause'): gcodePause = arg elif opt in ('-p', '--pens'): pens = {} penFilename = arg with open(arg) as f: for line in f:
-w, --websocket Use MQTT over WebSocket -h, --help Help information """ # Read in command-line parameters useWebsocket = False host = "" rootCAPath = "" certificatePath = "" privateKeyPath = "" try: opts, args = getopt.getopt(sys.argv[1:], "hwe:k:c:r:", ["help", "endpoint=", "key=","cert=","rootCA=", "websocket"]) if len(opts) == 0: raise getopt.GetoptError("No input parameters!") for opt, arg in opts: if opt in ("-h", "--help"): print(helpInfo) exit(0) if opt in ("-e", "--endpoint"): host = arg if opt in ("-r", "--rootCA"): rootCAPath = arg if opt in ("-c", "--cert"): certificatePath = arg if opt in ("-k", "--key"): privateKeyPath = arg if opt in ("-w", "--websocket"): useWebsocket = True except getopt.GetoptError:
import subprocess import sys import time import datetime def man_format(data): data = data.replace("-", "\\-") data = data.replace("\t", " ") return data if len(sys.argv) != 3: import getopt raise getopt.GetoptError("Usage: %s <path-to-blender> <output-filename>" % sys.argv[0]) blender_bin = sys.argv[1] outfilename = sys.argv[2] cmd = [blender_bin, "--help"] print(" executing:", " ".join(cmd)) blender_help = subprocess.check_output(cmd).decode(encoding="utf-8") blender_version = subprocess.check_output([blender_bin, "--version" ]).decode(encoding="utf-8").strip() blender_version = blender_version.split("build")[0].rstrip() blender_version = blender_version.partition(" ")[2] # remove 'Blender' prefix. date_string = datetime.date.fromtimestamp(time.time()).strftime("%B %d, %Y") outfile = open(outfilename, "w") fw = outfile.write
def parse(argv): """ Parses our arguments, providing a named tuple with their values. :param list argv: input arguments to be parsed :returns: a **named tuple** with our parsed arguments :raises: **ValueError** if we got an invalid argument """ args = dict(DEFAULT_ARGS) try: recognized_args, unrecognized_args = getopt.getopt( argv, OPT, OPT_EXPANDED) if unrecognized_args: error_msg = "aren't recognized arguments" if len( unrecognized_args) > 1 else "isn't a recognized argument" raise getopt.GetoptError( "'%s' %s" % ("', '".join(unrecognized_args), error_msg)) except getopt.GetoptError as exc: raise ValueError('%s (for usage provide --help)' % exc) has_port_arg, has_socket_arg = False, False for opt, arg in recognized_args: if opt in ('-i', '--interface'): address = None if ':' in arg: address, port = arg.split(':', 1) else: port = arg if address: if not stem.util.connection.is_valid_ipv4_address(address): raise ValueError("'%s' isn't a valid IPv4 address" % address) else: address = args['control_port'][0] if not stem.util.connection.is_valid_port(port): raise ValueError("'%s' isn't a valid port number" % port) args['control_port'] = (address, int(port)) has_port_arg = True elif opt in ('-s', '--socket'): args['control_socket'] = arg has_socket_arg = True elif opt in ('-c', '--config'): args['config'] = arg elif opt in ('-d', '--debug'): args['debug_path'] = os.path.expanduser(arg) elif opt in ('-l', '--log'): args['logged_events'] = arg elif opt in ('-v', '--version'): args['print_version'] = True elif opt in ('-h', '--help'): args['print_help'] = True # If the user explicitely specified an endpoint then just try to connect to # that. if has_socket_arg and not has_port_arg: args['control_port'] = None elif has_port_arg and not has_socket_arg: args['control_socket'] = None # translates our args dict into a named tuple Args = collections.namedtuple('Args', args.keys()) return Args(**args)
def main(args=None): import getopt output = None start = None end = None write_start_addr = True overlap = 'error' if args is None: args = sys.argv[1:] try: opts, args = getopt.gnu_getopt(args, 'hvo:r:', ['help', 'version', 'output=', 'range=', 'no-start-addr', 'overlap=', ]) for o,a in opts: if o in ('-h', '--help'): print(USAGE) return 0 elif o in ('-v', '--version'): print(VERSION) return 0 elif o in ('-o', '--output'): output = a elif o in ("-r", "--range"): try: l = a.split(":") if l[0] != '': start = int(l[0], 16) if l[1] != '': end = int(l[1], 16) except (ValueError, IndexError): raise getopt.GetoptError('Bad range value(s)') elif o == '--no-start-addr': write_start_addr = False elif o == '--overlap': if a in ('error', 'ignore', 'replace'): overlap = a else: raise getopt.GetoptError('Bad overlap value') if len(args) == 0: raise getopt.GetoptError('You should specify file list') except getopt.GetoptError: e = sys.exc_info()[1] # current exception sys.stderr.write(str(e)+"\n") sys.stderr.write(USAGE+"\n") return 1 import intelhex # TODO: move actual merge code into intelhex package as helper function # and write couple of tests for it. res = intelhex.IntelHex() def end_addr_inclusive(addr): if addr is not None: return addr + 1 return addr for f in args: try: fname, fstart, fend = intelhex._get_file_and_addr_range(f) except intelhex._BadFileNotation: sys.stderr.write('Bad argument: "%s"\n' % f) sys.stderr.write(USAGE+"\n") return 1 if fname == '-': fname = sys.stdin ih = intelhex.IntelHex(fname) if (fstart, fend) != (None, None): ih = ih[fstart:end_addr_inclusive(fend)] try: res.merge(ih, overlap) except intelhex.AddressOverlapError: e = sys.exc_info()[1] # current exception sys.stderr.write('Merging: '+fname+"\n") sys.stderr.write(str(e)+"\n") return 1 if (start, end) != (None, None): res = res[start:end_addr_inclusive(end)] if output is None: output = sys.stdout res.write_hex_file(output, write_start_addr) return 0
opts, args = getopt.getopt( sys.argv[1:], "hvp:r:l:s:", ["help", "version", "pad=", "range=", "length=", "size="]) for o, a in opts: if o in ("-h", "--help"): print(usage) sys.exit(0) elif o in ("-v", "--version"): print(VERSION) sys.exit(0) elif o in ("-p", "--pad"): try: pad = int(a, 16) & 0x0FF except: raise getopt.GetoptError('Bad pad value') elif o in ("-r", "--range"): try: l = a.split(":") if l[0] != '': start = int(l[0], 16) if l[1] != '': end = int(l[1], 16) except: raise getopt.GetoptError('Bad range value(s)') elif o in ("-l", "--lenght", "-s", "--size"): try: size = int(a, 10) except: raise getopt.GetoptError('Bad size value')
def main(): argv = sys.argv[1:] logger = logging.getLogger("scapy") logger.addHandler(logging.StreamHandler()) ignore_globals = list(six.moves.builtins.__dict__) import scapy print(dash + " UTScapy - Scapy %s - %s" % (scapy.__version__, sys.version.split(" ")[0])) # Parse arguments FORMAT = Format.ANSI OUTPUTFILE = sys.stdout LOCAL = 0 NUM = None NON_ROOT = False KW_OK = [] KW_KO = [] DUMP = 0 DOCS = 0 CRC = True BREAKFAILED = True ONLYFAILED = False VERB = 3 GLOB_PREEXEC = "" PREEXEC_DICT = {} MODULES = [] TESTFILES = [] ANNOTATIONS_MODE = False INTERPRETER = False try: opts = getopt.getopt(argv, "o:t:T:c:f:hbln:m:k:K:DRdCiFqNP:s:x") for opt, optarg in opts[0]: if opt == "-h": usage() elif opt == "-b": BREAKFAILED = False elif opt == "-F": ONLYFAILED = True elif opt == "-q": VERB -= 1 elif opt == "-D": DUMP = 2 elif opt == "-R": DOCS = 1 elif opt == "-d": DUMP = 1 elif opt == "-C": CRC = False elif opt == "-i": INTERPRETER = True elif opt == "-x": ANNOTATIONS_MODE = True elif opt == "-P": GLOB_PREEXEC += "\n" + optarg elif opt == "-f": try: FORMAT = Format.from_string(optarg) except KeyError as msg: raise getopt.GetoptError("Unknown output format %s" % msg) elif opt == "-t": TESTFILES.append(optarg) TESTFILES = resolve_testfiles(TESTFILES) elif opt == "-T": TESTFILES.remove(optarg) elif opt == "-c": data = parse_config_file(optarg, VERB) BREAKFAILED = data.breakfailed ONLYFAILED = data.onlyfailed VERB = data.verb DUMP = data.dump CRC = data.crc PREEXEC_DICT = data.preexec GLOB_PREEXEC = data.global_preexec OUTPUTFILE = data.outfile TESTFILES = data.testfiles LOCAL = 1 if data.local else 0 NUM = data.num MODULES = data.modules KW_OK.extend(data.kw_ok) KW_KO.extend(data.kw_ko) try: FORMAT = Format.from_string(data.format) except KeyError as msg: raise getopt.GetoptError("Unknown output format %s" % msg) TESTFILES = resolve_testfiles(TESTFILES) for testfile in resolve_testfiles(data.remove_testfiles): try: TESTFILES.remove(testfile) except ValueError: error_m = "Cannot remove %s from test files" % testfile raise getopt.GetoptError(error_m) elif opt == "-o": OUTPUTFILE = optarg if not os.access(os.path.dirname(os.path.abspath(OUTPUTFILE)), os.W_OK): raise getopt.GetoptError("Cannot write to file %s" % OUTPUTFILE) elif opt == "-l": LOCAL = 1 elif opt == "-n": NUM = [] for v in (x.strip() for x in optarg.split(",")): try: NUM.append(int(v)) except ValueError: v1, v2 = [int(e) for e in v.split('-', 1)] NUM.extend(range(v1, v2 + 1)) elif opt == "-N": NON_ROOT = True elif opt == "-m": MODULES.append(optarg) elif opt == "-k": KW_OK.extend(optarg.split(",")) elif opt == "-K": KW_KO.extend(optarg.split(",")) except getopt.GetoptError as msg: print("ERROR:", msg) raise SystemExit if FORMAT in [Format.LIVE, Format.ANSI]: theme = DefaultTheme() else: theme = BlackAndWhite() # Disable tests if needed # Discard Python3 tests when using Python2 if six.PY2: KW_KO.append("python3_only") if VERB > 2: print(" " + arrow + " Python 2 mode") try: if NON_ROOT or os.getuid() != 0: # Non root # Discard root tests KW_KO.append("netaccess") KW_KO.append("needs_root") if VERB > 2: print(" " + arrow + " Non-root mode") except AttributeError: pass if conf.use_pcap: KW_KO.append("not_pcapdnet") if VERB > 2: print(" " + arrow + " libpcap mode") KW_KO.append("disabled") # Process extras if six.PY3: KW_KO.append("FIXME_py3") if ANNOTATIONS_MODE: try: from pyannotate_runtime import collect_types except ImportError: raise ImportError("Please install pyannotate !") collect_types.init_types_collection() collect_types.start() if VERB > 2: print(" " + arrow + " Booting scapy...") try: from scapy import all as scapy except Exception as e: print("[CRITICAL]: Cannot import Scapy: %s" % e) traceback.print_exc() sys.exit(1) # Abort the tests for m in MODULES: try: mod = import_module(m) six.moves.builtins.__dict__.update(mod.__dict__) except ImportError as e: raise getopt.GetoptError("cannot import [%s]: %s" % (m, e)) autorun_func = { Format.TEXT: scapy.autorun_get_text_interactive_session, Format.ANSI: scapy.autorun_get_ansi_interactive_session, Format.HTML: scapy.autorun_get_html_interactive_session, Format.LATEX: scapy.autorun_get_latex_interactive_session, Format.XUNIT: scapy.autorun_get_text_interactive_session, Format.LIVE: scapy.autorun_get_live_interactive_session, } if VERB > 2: print(" " + arrow + " Discovering tests files...") glob_output = "" glob_result = 0 glob_title = None UNIQUE = len(TESTFILES) == 1 # Resolve tags and asterix for prex in six.iterkeys(copy.copy(PREEXEC_DICT)): if "*" in prex: pycode = PREEXEC_DICT[prex] del PREEXEC_DICT[prex] for gl in glob.iglob(prex): _pycode = pycode.replace( "%name%", os.path.splitext(os.path.split(gl)[1])[0]) # noqa: E501 PREEXEC_DICT[gl] = _pycode pos_begin = 0 runned_campaigns = [] scapy_ses = importlib.import_module(".all", "scapy").__dict__ import_UTscapy_tools(scapy_ses) # Execute all files for TESTFILE in TESTFILES: if VERB > 2: print(theme.green(dash + " Loading: %s" % TESTFILE)) PREEXEC = PREEXEC_DICT[ TESTFILE] if TESTFILE in PREEXEC_DICT else GLOB_PREEXEC with open(TESTFILE) as testfile: output, result, campaign = execute_campaign( testfile, OUTPUTFILE, PREEXEC, NUM, KW_OK, KW_KO, DUMP, DOCS, FORMAT, VERB, ONLYFAILED, CRC, INTERPRETER, autorun_func, theme, pos_begin=pos_begin, ignore_globals=ignore_globals, scapy_ses=copy.copy(scapy_ses)) runned_campaigns.append(campaign) pos_begin = campaign.end_pos if UNIQUE: glob_title = campaign.title glob_output += output if not result: glob_result = 1 if BREAKFAILED: break if VERB > 2: print(checkmark + " All campaigns executed. Writing output...") if ANNOTATIONS_MODE: collect_types.stop() collect_types.dump_stats("pyannotate_results") # Concenate outputs if FORMAT == Format.HTML: glob_output = pack_html_campaigns(runned_campaigns, glob_output, LOCAL, glob_title) # Write the final output # Note: on Python 2, we force-encode to ignore ascii errors # on Python 3, we need to detect the type of stream if OUTPUTFILE == sys.stdout: print(glob_output, file=OUTPUTFILE) else: with open(OUTPUTFILE, "wb") as f: f.write( glob_output.encode("utf8", "ignore") if 'b' in f.mode or six.PY2 else glob_output) # Print end message if VERB > 2: if glob_result == 0: print(theme.green("UTscapy ended successfully")) else: print(theme.red("UTscapy ended with error code %s" % glob_result)) # Check active threads if VERB > 2: import threading if threading.active_count() > 1: print("\nWARNING: UNFINISHED THREADS") print(threading.enumerate()) # Return state return glob_result
def interact(mydict=None,argv=None,mybanner=None,loglevel=20): global session import code,sys,pickle,os,getopt,re from .config import conf conf.interactive = True if loglevel is not None: conf.logLevel=loglevel the_banner = "Welcome to Scapy (%s)" if mybanner is not None: the_banner += "\n" the_banner += mybanner if argv is None: argv = sys.argv import atexit try: import rlcompleter,readline except ImportError: log_loading.info("Can't load Python libreadline or completer") READLINE=0 else: READLINE=1 class ScapyCompleter(rlcompleter.Completer): def global_matches(self, text): matches = [] n = len(text) for lst in [dir(builtins), session.keys()]: for word in lst: if word[:n] == text and word != "__builtins__": matches.append(word) return matches def attr_matches(self, text): m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text) if not m: return expr, attr = m.group(1, 3) try: object = eval(expr) except: object = eval(expr, session) if isinstance(object, Packet) or isinstance(object, Packet_metaclass): #words = filter(lambda x: x[0]!="_",dir(object)) words = [ x for x in dir(object) if x[0]!="_" ] words += [x.name for x in object.fields_desc] else: words = dir(object) if hasattr( object,"__class__" ): words = words + rlcompleter.get_class_members(object.__class__) matches = [] n = len(attr) for word in words: if word[:n] == attr and word != "__builtins__": matches.append("%s.%s" % (expr, word)) return matches readline.set_completer(ScapyCompleter().complete) readline.parse_and_bind("C-o: operate-and-get-next") readline.parse_and_bind("tab: complete") session=None session_name="" STARTUP_FILE = DEFAULT_STARTUP_FILE PRESTART_FILE = DEFAULT_PRESTART_FILE iface = None try: opts=getopt.getopt(argv[1:], "hs:Cc:Pp:d") for opt, parm in opts[0]: if opt == "-h": _usage() elif opt == "-s": session_name = parm elif opt == "-c": STARTUP_FILE = parm elif opt == "-C": STARTUP_FILE = None elif opt == "-p": PRESTART_FILE = parm elif opt == "-P": PRESTART_FILE = None elif opt == "-d": conf.logLevel = max(1,conf.logLevel-10) if len(opts[1]) > 0: raise getopt.GetoptError("Too many parameters : [%s]" % " ".join(opts[1])) except getopt.GetoptError as msg: log_loading.error(msg) sys.exit(1) if PRESTART_FILE: _read_config_file(PRESTART_FILE) scapy_builtins = __import__("scapy.all",globals(),locals(),".").__dict__ builtins.__dict__.update(scapy_builtins) globkeys = list(scapy_builtins.keys()) globkeys.append("scapy_session") scapy_builtins=None # XXX replace with "with" statement if mydict is not None: builtins.__dict__.update(mydict) globkeys += mydict.keys() conf.color_theme = DefaultTheme() if STARTUP_FILE: _read_config_file(STARTUP_FILE) if session_name: try: os.stat(session_name) except OSError: log_loading.info("New session [%s]" % session_name) else: try: try: session = pickle.load(gzip.open(session_name,"rb")) except IOError: session = pickle.load(open(session_name,"rb")) log_loading.info("Using session [%s]" % session_name) except EOFError: log_loading.error("Error opening session [%s]" % session_name) except AttributeError: log_loading.error("Error opening session [%s]. Attribute missing" % session_name) if session: if "conf" in session: conf.configure(session["conf"]) session["conf"] = conf else: conf.session = session_name session={"conf":conf} else: session={"conf": conf} builtins.__dict__["scapy_session"] = session if READLINE: if conf.histfile: try: readline.read_history_file(conf.histfile) except IOError: pass atexit.register(scapy_write_history_file,readline) atexit.register(scapy_delete_temp_files) IPYTHON=False if conf.interactive_shell.lower() == "ipython": try: import IPython IPYTHON=True except ImportError as e: log_loading.warning("IPython not available. Using standard Python shell instead.") IPYTHON=False if IPYTHON: banner = the_banner % (conf.version) + " using IPython %s" % IPython.__version__ # Old way to embed IPython kept for backward compatibility try: args = [''] # IPython command line args (will be seen as sys.argv) ipshell = IPython.Shell.IPShellEmbed(args, banner = banner) ipshell(local_ns=session) except AttributeError as e: pass # In the IPython cookbook, see 'Updating-code-for-use-with-IPython-0.11-and-later' IPython.embed(user_ns=session, banner2=banner) else: code.interact(banner = the_banner % (conf.version), local=session, readfunc=conf.readfunc) if conf.session: save_session(conf.session, session) for k in globkeys: try: del(builtins.__dict__[k]) except: pass
def main(): argv = sys.argv[1:] ignore_globals = list(six.moves.builtins.__dict__) # Parse arguments FORMAT = Format.ANSI OUTPUTFILE = sys.stdout LOCAL = 0 NUM = None KW_OK = [] KW_KO = [] DUMP = 0 CRC = True BREAKFAILED = True ONLYFAILED = False VERB = 3 GLOB_PREEXEC = "" PREEXEC_DICT = {} SCAPY = "scapy" MODULES = [] TESTFILES = [] try: opts = getopt.getopt(argv, "o:t:T:c:f:hbln:m:k:K:DdCFqP:s:") for opt, optarg in opts[0]: if opt == "-h": usage() elif opt == "-b": BREAKFAILED = True elif opt == "-F": ONLYFAILED = True elif opt == "-q": VERB -= 1 elif opt == "-D": DUMP = 2 elif opt == "-d": DUMP = 1 elif opt == "-C": CRC = False elif opt == "-s": SCAPY = optarg elif opt == "-P": GLOB_PREEXEC += "\n" + optarg elif opt == "-f": try: FORMAT = Format.from_string(optarg) except KeyError as msg: raise getopt.GetoptError("Unknown output format %s" % msg) elif opt == "-t": TESTFILES.append(optarg) TESTFILES = resolve_testfiles(TESTFILES) elif opt == "-T": TESTFILES.remove(optarg) elif opt == "-c": data = parse_config_file(optarg, VERB) BREAKFAILED = data.breakfailed ONLYFAILED = data.onlyfailed VERB = data.verb DUMP = data.dump CRC = data.crc SCAPY = data.scapy PREEXEC_DICT = data.preexec GLOB_PREEXEC = data.global_preexec OUTPUTFILE = data.outfile TESTFILES = data.testfiles LOCAL = 1 if data.local else 0 NUM = data.num MODULES = data.modules KW_OK.extend(data.kw_ok) KW_KO.extend(data.kw_ko) try: FORMAT = Format.from_string(data.format) except KeyError as msg: raise getopt.GetoptError("Unknown output format %s" % msg) TESTFILES = resolve_testfiles(TESTFILES) for testfile in resolve_testfiles(data.remove_testfiles): try: TESTFILES.remove(testfile) except ValueError: error_m = "Cannot remove %s from test files" % testfile raise getopt.GetoptError(error_m) elif opt == "-o": OUTPUTFILE = optarg if not os.access(os.path.dirname(os.path.abspath(OUTPUTFILE)), os.W_OK): raise getopt.GetoptError("Cannot write to file %s" % OUTPUTFILE) elif opt == "-l": LOCAL = 1 elif opt == "-n": NUM = [] for v in (x.strip() for x in optarg.split(",")): try: NUM.append(int(v)) except ValueError: v1, v2 = [int(e) for e in v.split('-', 1)] NUM.extend(range(v1, v2 + 1)) elif opt == "-m": MODULES.append(optarg) elif opt == "-k": KW_OK.extend(optarg.split(",")) elif opt == "-K": KW_KO.extend(optarg.split(",")) # Discard Python3 tests when using Python2 if six.PY2: KW_KO.append("python3_only") if VERB > 2: print("### Booting scapy...", file=sys.stderr) try: from scapy import all as scapy except Exception as e: print("[CRITICAL]: Cannot import Scapy: %s" % e, file=sys.stderr) traceback.print_exc() sys.exit(1) # Abort the tests for m in MODULES: try: mod = import_module(m) six.moves.builtins.__dict__.update(mod.__dict__) except ImportError as e: raise getopt.GetoptError("cannot import [%s]: %s" % (m, e)) if WINDOWS: from scapy.arch.windows import route_add_loopback route_add_loopback() # Add SCAPY_ROOT_DIR environment variable, used for tests os.environ['SCAPY_ROOT_DIR'] = os.environ.get("PWD", os.getcwd()) except getopt.GetoptError as msg: print("ERROR:", msg, file=sys.stderr) raise SystemExit autorun_func = { Format.TEXT: scapy.autorun_get_text_interactive_session, Format.ANSI: scapy.autorun_get_ansi_interactive_session, Format.HTML: scapy.autorun_get_html_interactive_session, Format.LATEX: scapy.autorun_get_latex_interactive_session, Format.XUNIT: scapy.autorun_get_text_interactive_session, } if VERB > 2: print("### Starting tests...", file=sys.stderr) glob_output = "" glob_result = 0 glob_title = None UNIQUE = len(TESTFILES) == 1 # Resolve tags and asterix for prex in six.iterkeys(copy.copy(PREEXEC_DICT)): if "*" in prex: pycode = PREEXEC_DICT[prex] del PREEXEC_DICT[prex] for gl in glob.iglob(prex): _pycode = pycode.replace( "%name%", os.path.splitext(os.path.split(gl)[1])[0]) # noqa: E501 PREEXEC_DICT[gl] = _pycode pos_begin = 0 runned_campaigns = [] # Execute all files for TESTFILE in TESTFILES: if VERB > 2: print("### Loading:", TESTFILE, file=sys.stderr) PREEXEC = PREEXEC_DICT[ TESTFILE] if TESTFILE in PREEXEC_DICT else GLOB_PREEXEC with open(TESTFILE) as testfile: output, result, campaign = execute_campaign( testfile, OUTPUTFILE, PREEXEC, NUM, KW_OK, KW_KO, DUMP, FORMAT, VERB, ONLYFAILED, CRC, autorun_func, pos_begin, ignore_globals) runned_campaigns.append(campaign) pos_begin = campaign.end_pos if UNIQUE: glob_title = campaign.title glob_output += output if not result: glob_result = 1 if BREAKFAILED: break if VERB > 2: print("### Writing output...", file=sys.stderr) # Concenate outputs if FORMAT == Format.HTML: glob_output = pack_html_campaigns(runned_campaigns, glob_output, LOCAL, glob_title) # Write the final output # Note: on Python 2, we force-encode to ignore ascii errors # on Python 3, we need to detect the type of stream if OUTPUTFILE == sys.stdout: OUTPUTFILE.write( glob_output.encode("utf8", "ignore") if 'b' in OUTPUTFILE.mode or six.PY2 else glob_output) else: with open(OUTPUTFILE, "wb") as f: f.write( glob_output.encode("utf8", "ignore") if 'b' in f.mode or six.PY2 else glob_output) # Delete scapy's test environment vars del os.environ['SCAPY_ROOT_DIR'] # Return state return glob_result
def get_options(): """To get options and values as a dictionary {'option': value}""" longopts = [ 'help', 'input=', 'output=', 'find-non-finishing', 'minimize', 'case-insensitive' ] opts_dict = {} try: options, args = getopt.getopt(sys.argv[1:], 'fim', longopts) if args: # args list is not empty raise getopt.GetoptError( 'invalid program argument \'' + args[0] + '\'') opts_list = [opt[0] for opt in options] if len(opts_list) != len(set(opts_list)): raise getopt.GetoptError( 'cannot combine two or more same options') if all(opt in opts_list for opt in ['--minimize', '-m']): raise getopt.GetoptError( 'cannot combine two or more same options') if (all(opt in opts_list for opt in ['--find-non-finishing', '-f'])): raise getopt.GetoptError( 'cannot combine two or more same options') if all(opt in opts_list for opt in ['--case-insensitive', '-i']): raise getopt.GetoptError( 'cannot combine two or more same options') if (('-m' in opts_list or '--minimize' in opts_list) and ('-f' in opts_list or '--find-non-finishing' in opts_list)): raise getopt.GetoptError('cannot combine \'minimize\' and ' '\'find-non-finishing\' options') for arg in sys.argv[1:]: arg_input_output = (arg[2:].startswith('output') or arg[2:].startswith('input')) if arg_input_output and re.match(r'\w+=.+', arg[2:]) is None: raise getopt.GetoptError( 'option \'' + arg + '\' requires argument') except getopt.GetoptError as opt_error: print_stderr(sys.argv[0], ': ', end='', sep='') print_stderr(opt_error) sys.exit(exit_codes.ERR_ARGS) for opt in options: opt_str = opt[0][2:] if re.match(r'^\-[^\-]', opt[0]): if opt[0] == '-m': opt_str = 'minimize' elif opt[0] == '-f': opt_str = 'find-non-finishing' else: opt_str = 'case-insensitive' opts_dict[opt_str] = opt[1] return opts_dict
continue return comments if __name__ == '__main__': help_message = 'Usage:\n IssuesAndCommentsProcessor.py -s|--slug=<slug> -t|--tokens=<tokens> -o|--output=<output_dir>' slug = None out_dir = None tokens_file = None logger = initialize_logger(name="SZZ:ISSUES_COMMENTS") try: if not sys.argv[1:]: raise getopt.GetoptError('No arguments passed from the command line. See help instructions.') opts, args = getopt.getopt(sys.argv[1:], "s:t:o:H", ["slug=", "output=", "tokens", "help"]) for opt, arg in opts: if opt in ("-h", "--help"): print(help_message) sys.exit(0) elif opt in ("-o", "--output"): out_dir = arg elif opt in ("-t", "--tokens"): tokens_file = arg elif opt in ("-s", "--slug"): slug = arg else: assert False, "unhandled option" except getopt.GetoptError as err: # print help information and exit:
def interact(mydict=None, argv=None, mybanner=None, loglevel=20): global SESSION global GLOBKEYS console_handler = logging.StreamHandler() console_handler.setFormatter( logging.Formatter("%(levelname)s: %(message)s")) # noqa: E501 log_scapy.addHandler(console_handler) from scapy.config import conf conf.color_theme = DefaultTheme() conf.interactive = True if loglevel is not None: conf.logLevel = loglevel STARTUP_FILE = DEFAULT_STARTUP_FILE PRESTART_FILE = DEFAULT_PRESTART_FILE session_name = None if argv is None: argv = sys.argv try: opts = getopt.getopt(argv[1:], "hs:Cc:Pp:d") for opt, parm in opts[0]: if opt == "-h": _usage() elif opt == "-s": session_name = parm elif opt == "-c": STARTUP_FILE = parm elif opt == "-C": STARTUP_FILE = None elif opt == "-p": PRESTART_FILE = parm elif opt == "-P": PRESTART_FILE = None elif opt == "-d": conf.logLevel = max(1, conf.logLevel - 10) if len(opts[1]) > 0: raise getopt.GetoptError("Too many parameters : [%s]" % " ".join(opts[1])) # noqa: E501 except getopt.GetoptError as msg: log_loading.error(msg) sys.exit(1) # Reset sys.argv, otherwise IPython thinks it is for him sys.argv = sys.argv[:1] init_session(session_name, mydict) if STARTUP_FILE: _read_config_file(STARTUP_FILE, interactive=True) if PRESTART_FILE: _read_config_file(PRESTART_FILE, interactive=True) if not conf.interactive_shell or conf.interactive_shell.lower() in [ "ipython", "auto" ]: try: import IPython from IPython import start_ipython except ImportError: log_loading.warning( "IPython not available. Using standard Python shell " "instead.\nAutoCompletion, History are disabled.") if WINDOWS: log_loading.warning( "IPython not available. On Windows, colors are disabled" ) # noqa: E501 conf.color_theme = BlackAndWhite() IPYTHON = False else: IPYTHON = True else: IPYTHON = False if conf.fancy_prompt: the_logo = [ " ", " aSPY//YASa ", " apyyyyCY//////////YCa ", " sY//////YSpcs scpCY//Pp ", " ayp ayyyyyyySCP//Pp syY//C ", " AYAsAYYYYYYYY///Ps cY//S", " pCCCCY//p cSSps y//Y", " SPPPP///a pP///AC//Y", " A//A cyP////C", " p///Ac sC///a", " P////YCpc A//A", " scccccp///pSP///p p//Y", " sY/////////y caa S//P", " cayCyayP//Ya pY/Ya", " sY/PsY////YCc aC//Yp ", " sc sccaCY//PCypaapyCP//YSs ", " spCPY//////YPSps ", " ccaacs ", " ", ] the_banner = [ "", "", " |", " | Welcome to Scapy", " | Version %s" % conf.version, " |", " | https://github.com/secdev/scapy", " |", " | Have fun!", " |", ] quote, author = choice(QUOTES) the_banner.extend(_prepare_quote(quote, author, max_len=39)) the_banner.append(" |") the_banner = "\n".join( logo + banner for logo, banner in six.moves.zip_longest(( conf.color_theme.logo(line) for line in the_logo), (conf.color_theme.success(line) for line in the_banner), fillvalue="")) else: the_banner = "Welcome to Scapy (%s)" % conf.version if mybanner is not None: the_banner += "\n" the_banner += mybanner if IPYTHON: banner = the_banner + " using IPython %s\n" % IPython.__version__ try: from traitlets.config.loader import Config except ImportError: log_loading.warning( "traitlets not available. Some Scapy shell features won't be " "available.") try: start_ipython(display_banner=False, user_ns=SESSION, exec_lines=["print(\"\"\"" + banner + "\"\"\")"]) except Exception: code.interact(banner=the_banner, local=SESSION) else: cfg = Config() try: get_ipython except NameError: # Set "classic" prompt style when launched from run_scapy(.bat) files # noqa: E501 # Register and apply scapy color+prompt style apply_ipython_style(shell=cfg.TerminalInteractiveShell) cfg.TerminalInteractiveShell.confirm_exit = False cfg.TerminalInteractiveShell.separate_in = u'' if int(IPython.__version__[0]) >= 6: cfg.TerminalInteractiveShell.term_title_format = "Scapy v" + conf.version # noqa: E501 else: cfg.TerminalInteractiveShell.term_title = False cfg.HistoryAccessor.hist_file = conf.histfile cfg.InteractiveShell.banner1 = banner # configuration can thus be specified here. try: start_ipython(config=cfg, user_ns=SESSION) except (AttributeError, TypeError): code.interact(banner=the_banner, local=SESSION) else: code.interact(banner=the_banner, local=SESSION) if conf.session: save_session(conf.session, SESSION) for k in GLOBKEYS: try: del (six.moves.builtins.__dict__[k]) except Exception: pass