From adb8436667f62b421315e53c60e1368f0f35caa5 Mon Sep 17 00:00:00 2001
From: Phil <phil@secdev.org>
Date: Tue, 5 Jan 2010 21:59:13 +0100
Subject: [PATCH] Added IPython support (ticket #329, patch from D. Loss)

Add the following line to ~/.scapy_startup.py :
conf.interactive_shell="ipython
---
 scapy/config.py |  2 ++
 scapy/main.py   | 19 +++++++++++++++++--
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/scapy/config.py b/scapy/config.py
index af1b1e0c..ad73f68e 100644
--- a/scapy/config.py
+++ b/scapy/config.py
@@ -287,6 +287,7 @@ def _prompt_changer(attr,val):
 class Conf(ConfClass):
     """This object contains the configuration of scapy.
 session  : filename where the session will be saved
+interactive_shell : If set to "ipython", use IPython as shell. Default: Python 
 stealth  : if 1, prevents any unwanted packet to go out (ARP, DNS, ...)
 checkIPID: if 0, doesn't check that IPID matches between IP sent and ICMP IP citation received
            if 1, checks that they either are equal or byte swapped equals (bug in some IP stacks)
@@ -314,6 +315,7 @@ extensions_paths: path or list of paths where extensions are to be looked for
     version = "2.1.0-dev"
     session = ""
     interactive = False
+    interactive_shell = ""
     stealth = "not implemented"
     iface = None
     readfunc = None
diff --git a/scapy/main.py b/scapy/main.py
index 2aa16146..d50763f2 100644
--- a/scapy/main.py
+++ b/scapy/main.py
@@ -296,8 +296,23 @@ def interact(mydict=None,argv=None,mybanner=None,loglevel=20):
     
     atexit.register(scapy_delete_temp_files)
     conf.color_theme = DefaultTheme()
-    code.interact(banner = the_banner % (conf.version), 
-                  local=session, readfunc=conf.readfunc)
+    
+    IPYTHON=False
+    if conf.interactive_shell.lower() == "ipython":
+        try:
+            import IPython
+            IPYTHON=True
+        except ImportError, 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__
+        ipshell = IPython.Shell.IPShellEmbed(banner = banner)
+        ipshell(local_ns=session)
+    else:
+        code.interact(banner = the_banner % (conf.version),
+                      local=session, readfunc=conf.readfunc)
 
     if conf.session:
         save_session(conf.session, session)
-- 
GitLab