Skip to content

Commit bdcbba5

Browse files
authored
Merge pull request #2833 from gpotter2/autorun-log
Catch logging in autorun
2 parents 9a9853f + 1554361 commit bdcbba5

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

scapy/autorun.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import code
1212
import sys
1313
import importlib
14+
import logging
15+
1416
from scapy.config import conf
1517
from scapy.themes import NoTheme, DefaultTheme, HTMLTheme2, LatexTheme2
16-
from scapy.error import Scapy_Exception
18+
from scapy.error import log_scapy, Scapy_Exception
1719
from scapy.utils import tex_escape
1820
import scapy.modules.six as six
1921

@@ -109,6 +111,9 @@ def autorun_get_interactive_session(cmds, **kargs):
109111
"""
110112
sstdout, sstderr = sys.stdout, sys.stderr
111113
sw = StringWriter()
114+
h_old = log_scapy.handlers[0]
115+
log_scapy.removeHandler(h_old)
116+
log_scapy.addHandler(logging.StreamHandler(stream=sw))
112117
try:
113118
try:
114119
sys.stdout = sys.stderr = sw
@@ -118,6 +123,8 @@ def autorun_get_interactive_session(cmds, **kargs):
118123
raise
119124
finally:
120125
sys.stdout, sys.stderr = sstdout, sstderr
126+
log_scapy.removeHandler(log_scapy.handlers[0])
127+
log_scapy.addHandler(h_old)
121128
return sw.s, res
122129

123130

test/regression.uts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,12 @@ assert(ret == ("\\textcolor{blue}{{\\tt\\char62}{\\tt\\char62}{\\tt\\char62} }IP
784784
ret = autorun_get_text_interactive_session("scapy_undefined")
785785
assert "NameError" in ret[0]
786786

787+
= Test autorun with logging
788+
789+
cmds = """log_runtime.info(hex_bytes("446166742050756e6b"))\n"""
790+
ret = autorun_get_text_interactive_session(cmds)
791+
assert "Daft Punk" in ret[0]
792+
787793
= Test utility TEX functions
788794

789795
assert tex_escape("{scapy}\\^$~#_&%|><") == "{\\tt\\char123}scapy{\\tt\\char125}{\\tt\\char92}\\^{}\\${\\tt\\char126}\\#\\_\\&\\%{\\tt\\char124}{\\tt\\char62}{\\tt\\char60}"

0 commit comments

Comments
 (0)