root/scapy/autorun.py

Revision 944:906064148321, 4.1 kB (checked in by Phil <phil@secdev.org>, 3 weeks ago)

Fixed missing import in autorun.py (ticket #158)

Line 
1 ## This file is part of Scapy
2 ## See http://www.secdev.org/projects/scapy for more informations
3 ## Copyright (C) Philippe Biondi <phil@secdev.org>
4 ## This program is published under a GPLv2 license
5
6 import code,sys
7 from config import conf
8 from themes import *
9 from error import Scapy_Exception
10 from utils import tex_escape
11
12
13 #########################
14 ##### Autorun stuff #####
15 #########################
16
17 class StopAutorun(Scapy_Exception):
18     code_run = ""
19
20 class ScapyAutorunInterpreter(code.InteractiveInterpreter):
21     def __init__(self, *args, **kargs):
22         code.InteractiveInterpreter.__init__(self, *args, **kargs)
23         self.error = 0
24     def showsyntaxerror(self, *args, **kargs):
25         self.error = 1
26         return code.InteractiveInterpreter.showsyntaxerror(self, *args, **kargs)
27     def showtraceback(self, *args, **kargs):
28         self.error = 1
29         exc_type, exc_value, exc_tb = sys.exc_info()
30         if isinstance(exc_value, StopAutorun):
31             raise exc_value
32         return code.InteractiveInterpreter.showtraceback(self, *args, **kargs)
33
34
35 def autorun_commands(cmds,my_globals=None,verb=0):
36     sv = conf.verb
37     import __builtin__
38     try:
39         try:
40             if my_globals is None:
41                 my_globals = __import__("scapy.all").all.__dict__
42             conf.verb = verb
43             interp = ScapyAutorunInterpreter(my_globals)
44             cmd = ""
45             cmds = cmds.splitlines()
46             cmds.append("") # ensure we finish multiline commands
47             cmds.reverse()
48             __builtin__.__dict__["_"] = None
49             while 1:
50                 if cmd:
51                     sys.stderr.write(sys.__dict__.get("ps2","... "))
52                 else:
53                     sys.stderr.write(str(sys.__dict__.get("ps1",ColorPrompt())))
54                    
55                 l = cmds.pop()
56                 print l
57                 cmd += "\n"+l
58                 if interp.runsource(cmd):
59                     continue
60                 if interp.error:
61                     return 0
62                 cmd = ""
63                 if len(cmds) <= 1:
64                     break
65         except SystemExit:
66             pass
67     finally:
68         conf.verb = sv
69     return _
70
71 def autorun_get_interactive_session(cmds, **kargs):
72     class StringWriter:
73         def __init__(self):
74             self.s = ""
75         def write(self, x):
76             self.s += x
77            
78     sw = StringWriter()
79     sstdout,sstderr = sys.stdout,sys.stderr
80     try:
81         try:
82             sys.stdout = sys.stderr = sw
83             res = autorun_commands(cmds, **kargs)
84         except StopAutorun,e:
85             e.code_run = sw.s
86             raise
87     finally:
88         sys.stdout,sys.stderr = sstdout,sstderr
89     return sw.s,res
90
91 def autorun_get_text_interactive_session(cmds, **kargs):
92     ct = conf.color_theme
93     try:
94         conf.color_theme = NoTheme()
95         s,res = autorun_get_interactive_session(cmds, **kargs)
96     finally:
97         conf.color_theme = ct
98     return s,res
99
100 def autorun_get_ansi_interactive_session(cmds, **kargs):
101     ct = conf.color_theme
102     try:
103         conf.color_theme = DefaultTheme()
104         s,res = autorun_get_interactive_session(cmds, **kargs)
105     finally:
106         conf.color_theme = ct
107     return s,res
108
109 def autorun_get_html_interactive_session(cmds, **kargs):
110     ct = conf.color_theme
111     to_html = lambda s: s.replace("<","&lt;").replace(">","&gt;").replace("#[#","<").replace("#]#",">")
112     try:
113         try:
114             conf.color_theme = HTMLTheme2()
115             s,res = autorun_get_interactive_session(cmds, **kargs)
116         except StopAutorun,e:
117             e.code_run = to_html(e.code_run)
118             raise
119     finally:
120         conf.color_theme = ct
121    
122     return to_html(s),res
123
124 def autorun_get_latex_interactive_session(cmds, **kargs):
125     ct = conf.color_theme
126     to_latex = lambda s: tex_escape(s).replace("@[@","{").replace("@]@","}").replace("@`@","\\")
127     try:
128         try:
129             conf.color_theme = LatexTheme2()
130             s,res = autorun_get_interactive_session(cmds, **kargs)
131         except StopAutorun,e:
132             e.code_run = to_latex(e.code_run)
133             raise
134     finally:
135         conf.color_theme = ct
136     return to_latex(s),res
137
138
Note: See TracBrowser for help on using the browser.