Changeset 899:733193ef0717

Show
Ignore:
Timestamp:
08/31/08 00:34:17 (4 months ago)
Author:
Phil <phil@secdev.org>
Message:

Improved temp file management

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • scapy/config.py

    r892 r899  
    269269    stats_classic_protocols = [] 
    270270    stats_dot11_protocols = [] 
     271    temp_files = [] 
    271272    netcache = NetCache() 
    272273    load_layers = ["l2", "inet", "dhcp", "dns", "dot11", "gprs", "hsrp", "ip6", "ir", "isakmp", "l2tp", 
  • scapy/main.py

    r897 r899  
    77from __future__ import generators 
    88import os,sys 
     9import __builtin__ 
    910from error import * 
    10 import __builtin__ 
     11import utils 
    1112     
    1213 
     
    7374        fname = conf.session 
    7475        if not fname: 
    75             conf.session = fname = os.tempnam("","scapy"
     76            conf.session = fname = utils.get_temp_file(keep=True
    7677            log_interactive.info("Use [%s] as session file" % fname) 
    7778    if session is None: 
     
    121122##### Main ##### 
    122123################ 
     124 
     125def scapy_delete_temp_files(): 
     126    for f in conf.temp_files: 
     127        try: 
     128            os.unlink(f) 
     129        except: 
     130            pass 
    123131 
    124132def scapy_write_history_file(readline): 
     
    129137            try: 
    130138                warning("Could not write history to [%s]\n\t (%s)" % (conf.histfile,e)) 
    131                 tmp = os.tempnam("","scapy"
     139                tmp = utils.get_temp_file(keep=True
    132140                readline.write_history_file(tmp) 
    133141                warning("Wrote history to [%s]" % tmp) 
     
    287295        atexit.register(scapy_write_history_file,readline) 
    288296     
     297    atexit.register(scapy_delete_temp_files) 
    289298    sys.ps1 = ColorPrompt() 
    290299    code.interact(banner = the_banner % (conf.version), local=session) 
  • scapy/utils.py

    r898 r899  
    55 
    66import os,sys,socket,types 
    7 from config import conf 
    8 from data import MTU 
    97import random,time 
    108import gzip,zlib,cPickle 
    119import re,struct,array 
     10 
     11import warnings 
     12warnings.filterwarnings("ignore","tempnam",RuntimeWarning, __name__) 
     13 
     14from config import conf 
     15from data import MTU 
    1216from error import log_runtime,log_loading,log_interactive 
    1317from base_classes import BasePacketList 
     
    1721## Tools ## 
    1822########### 
     23 
     24def get_temp_file(keep=False): 
     25    f = os.tempnam("","scapy") 
     26    if not keep: 
     27        conf.temp_files.append(f) 
     28    return f 
    1929 
    2030def sane_color(x): 
     
    683693def wireshark(pktlist): 
    684694    """Run wireshark on a list of packets""" 
    685     f = os.tempnam("scapy"
     695    f = get_temp_file(
    686696    wrpcap(f, pktlist) 
    687697    os.spawnlp(os.P_NOWAIT, conf.prog.wireshark, conf.prog.wireshark, "-r", f) 
     
    690700def hexedit(x): 
    691701    x = str(x) 
    692     f = os.tempnam("scapy"
     702    f = get_temp_file(
    693703    open(f,"w").write(x) 
    694704    os.spawnlp(os.P_WAIT, conf.prog.hexedit, conf.prog.hexedit, f)