📄 cli.py
字号:
# Copyright (c) 2007, Neville-Neil Consulting# All rights reserved.## Redistribution and use in source and binary forms, with or without# modification, are permitted provided that the following conditions are# met:## Redistributions of source code must retain the above copyright notice,# this list of conditions and the following disclaimer.## Redistributions in binary form must reproduce the above copyright# notice, this list of conditions and the following disclaimer in the# documentation and/or other materials provided with the distribution.## Neither the name of Neville-Neil Consulting nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission.## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.## File: $Id: $## Author: George V. Neville-Neil## Description: This module contains all the code which implementes the# CLI for the Packet Debuggerimport sysimport cmd # Used to implement our CLIimport packetstreamclass Command(cmd.Cmd): """The top level Command Line Interpreter for the packet debugger. This class uses the built in command interpreter from Python to build up the full set of commands implemented by the debugger. Each command is written as a do_xxx function in this class Each command takes a string which should be parsed as arguments. Help follows the command.""" def __init__(self, current, options, streams): self.current = current self.options = options self.streams = streams self.prompt = "pdb> " cmd.Cmd.__init__(self) def help_help(self): print "help [command]" print "print out the help message, with [command] get help on that comamnd" def do_quit(self, message): print "Bye" sys.exit() def help_quit(self): print "Quit the debugger" # # Stream Management Commands # def do_load(self, args): """Load a new stream from a file or network interface. Acceptable argument lists are: file filename interface interfacename """ try: arg_list = args.split() except: self.help_load() return if (len(arg_list) != 2): self.help_load() return self.current = packetstream.Stream(self.options, arg_list[1]) self.streams.append(self.current) def help_load(self): print "load filename|interface" print "Load a stream from a file or open an interface" def do_unload(self, args): """Unload a stream. If N is present unload that stream.""" if ((self.current == None) or (len(self.streams) <= 0)): print "No streams. Use the load command to create one." return if (args == ""): self.streams.remove(self.current) if (len(self.streams) > 0): self.current = self.streams[0] else: self.current = None else: index = self.numarg(args) if (index == None): help_unload() return else: if (self.streams[index] == self.current): self.current = None del self.streams[index] if (len(self.streams) > 0): self.current = self.streams[0] def help_unload(self): print "unload (N)" print "Unload a Stream. If N is given unload a specific stream otherwise unload the current one." def do_run(self, args): """Run the current stream or the one given in the index.""" if (args == ""): if (self.current != None): self.current.run(0) else: print "No current stream. Use the load or set commands" else: numarg = self.numarg(args) if (numarg == None): self.help_run() return if ((numarg < 0) or (numarg >= len(self.streams))): print "Stream must be between 0 and %d." % (len(self.streams) - 1) self.help_run() return self.streams[numarg].run(0) def help_run(self): print "run (N)" print "run the current stream or the stream given by the index" def complete_run(self, text, line, begidx, endidx): stream_list = [] for i in range(0, len(self.streams)): stream_list.append(str(i)) return stream_list def do_continue(self, args): """Continue the current stream or the one given in the index.""" if (args == ""): if (self.current != None): self.current.run(self.current.position, self.current.position) else: print "No current stream. Use the load or set commands" else: numarg = self.numarg(args) if (numarg == None): self.help_run() return if ((numarg < 0) or (numarg >= len(self.streams))): print "Stream must be between 0 and %d." % (len(self.streams) - 1) self.help_run() return self.streams[numarg].run(self.streams[numarg].position, self.streams[numarg].position) def help_continue(self): print "continue (N)" print "continue the current stream or the stream given by the index" def do_info(self, args): if (len (args) <= 0): if (self.current == None): print "No current stream. Use the load or set commands" return print "Stream %d" % self.streams.index(self.current) print "---------" print self.current else: arg_list = args.split() if (arg_list[0] == "all"): for stream in self.streams: print "Stream %d" % self.streams.index(stream) print "---------" print stream elif (arg_list[0] == "break"): print "Stream %d breakpoints %s" % (self.streams.index(self.current), self.current.breakpoints) elif (arg_list[0].isdigit() == True): index = int(arg_list[0]) try: stream = self.streams[index] except: print "No stream %d" % index return print "Stream %d" % index print "---------" print self.streams[index] else: self.help_info() return def complete_info(self, text, line, begidx, endidx): """Completion of the arguments for the info command""" if (text in ["a" , "al", "all"]): return ["all"] elif (text in ["b", "br", "bre", "brea"]): return ["break"] else: stream_list = [] for i in range(0, len(self.streams)): stream_list.append(str(i)) return stream_list def help_info(self): print "info [N | all]" print "Print out all the information on the current stream, a specific stream (N), or all streams." # # Stream and Packet Inspection # def do_list(self, args):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -