今日のネットワーク。完全体

師匠が eth1 って決めつけてるのは良くないからそれも選べるようにしましょう。
とのことだったので、

from socket import *
#from AlPRO import AlPro
import sys
from scapy.all import Ether 


def AlPro():
    with file("/usr/include/linux/if_ether.h") as pc:
        Arr = {}
        for line in pc:
            if line.startswith("#define"):
                Va = line.split()
                if len(Va) > 2:
                    Arr[Va[1]] = Va[2]
                else:
                    continue
    return Arr


capture_start = lambda proto: AlPro()[proto]
hexchr = lambda x: chr(int(x,16))
"""
WTF CODE
def capture_start(proto):
    #AlPro().keys()
    v = AlPro().keys().index(proto)
    return AlPro().values()[v] 
"""

#print capture_start("ETH_P_IP")

def main():
    STR =""
    FLAG = 0
    
    if 'PL' in sys.argv:
        for i in AlPro():
            print i
        print 
        print "'ETH_P_IP' or 'ETH_P_ALL' is famous..."
        exit(0)
    
    elif len(sys.argv) != 3:
        print "Usage:python tpc.py (<PROTO> or 'PL') (-SC or -RAW)\n"
        print "PROTO: example ... 'ETH_P_IP' , 'ETH_P_ARP' or 'ETH_P_ALL'.etc look at 'PL'"
        print "'PL':PL means ProtoList.  U can read Proto's List.  Should check"
        exit(0)
    
    elif len(sys.argv) == 3:
        if sys.argv[1] not in AlPro().keys():
            print "Oh...look at PL..."
            print "Let's python tpc.py PL"
            exit(0)

# int(hex(2048)[2:],16)
    if sys.argv[2] == ("-SC" or "SC"):FLAG = 1

    PROTO = int(capture_start(sys.argv[1])[2:],16)
    try: 
        s = socket(PF_PACKET, SOCK_RAW,PROTO)
        Device = raw_input('Which device do you wanna capture?: ')
        s.bind((Device,PROTO))
    except socket.error,e:
        print e
    while True:
        p = s.recv(0x800)
        pl = len(p)

        if FLAG:
            SCAPACKET = Ether(p)
            print SCAPACKET.show2()
            print "-"*50
            continue
        f = lambda q,w: ":".join(["%02x" % ord(x) for x in q[w:w+6]])
        src = f(p,0)
        dst = f(p,6)
        ty = ntohs(ord(p[12:13]))
        print"%s > %s, ethertype %04x, length %d"%(src,dst,ty,pl)

        print "\t",
        for i in xrange(0,pl,2):
            print"%02x%02x"%(ord(p[i]),ord(p[i+1])),
            STR += "%02d %02d "%(ord(p[i]),ord(p[i+1])) 
            if i % 16 == 14:
                STR = STR.split()
                STR2=''
                for j in STR:
                    if '40' < j < '7F': STR2+=hexchr(j)
                    else: STR2+='.'
                print "|"+STR2+"\n\t",
                STR = ''
        print "\t\t|"

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt :
        exit(0)
    except AttributeError:
        print "The choice is wrong!!"
        print "you should choose from 'ifconfig'"
        exit(0)

Deviceっていうところに raw_input('....') しました!!
例外エラーもちゃんとしてあるので、いくら間違っても良いよ!

広告を非表示にする