我有这样的代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import tkMessageBox as box
import time
from tkFileDialog import askopenfilename,asksaveasfilename
import pcapy
from impacket import ImpactDecoder, ImpactPacket
import thread
import sys
import tkMessageBox
from Tkinter import Tk, Text, BOTH, W, N, E, S, StringVar, END, Listbox
from ttk import Frame, Button, Label, Style
class Program(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Wąchacz")
self.style = Style()
self.style.theme_use("default")
self.pack(fill=BOTH, expand=1)
devices = pcapy.findalldevs()
acts = devices
lb = Listbox(self)
for i in acts:
lb.insert(END, i)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
lbl = Label(self, text="Wybierz interface:")
lbl.grid(sticky=W, pady=4, padx=5)
lb.bind("<<ListboxSelect>>", self.onSelect)
lb.grid(row=1, column=0, columnspan=2, rowspan=4,
padx=5, sticky=E+W+S+N)
self.var = StringVar()
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
abtn = Button(self, text="Start", command=self.start)
abtn.grid( row=1, column=3)
bbtn = Button(self, text="Stop", command=self.threadinterrupt)
bbtn.grid(row=2, column=3, pady=4)
cbtn = Button(self, text="Otwórz", command=self.OpenFile)
cbtn.grid(row=3, column=3, pady=4)
dbtn = Button(self, text="Zapisz", command=self.SaveFile)
dbtn.grid(row=4, column=3, pady=4)
ebtn = Button(self, text="O programie...", command=self.onAbout)
ebtn.grid( row=5, column=3, pady=4)
fbtn = Button(self, text="Zamknij", command=self.quitGui)
fbtn.grid( row=5, column=0, pady=10)
def start(self):
try:
thread.start_new_thread(eth_addr,())
except (KeyboardInterrupt, SystemExit):
cleanup_stop_thread();
sys.exit()
def threadinterrupt(self):
thread.interrupt_main()
def OpenFile(self):
name = askopenfilename(filetypes=[("Pliki PCAP","*.pcap")],title="Otwórz plik pcap")
def SaveFile(self):
asksaveasfilename(filetypes=[("Pliki PCAP","*.pcap")],title="Zapisz plik pcap")
def quitGui(self):
answer = box.askquestion('wyjscie', "Napewno chcesz wyjść? ")
if answer == 'yes':
self.quit()
def onAbout(self):
box.showinfo("O programie", '''monitorowanie ruchu sieciowego''')
def onSelect(self, val):
interface1 = val.widget
idx = interface1.curselection()
value = interface1.get(idx)
self.var.set(value)
def eth_addr(a) :
b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]) , ord(a[1]) , ord(a[2]), ord(a[3]), ord(a[4]) , ord(a[5]))
return b
global interface1
interface1 = str(var1.get())
try:
s = socket.socket( socket.AF_PACKET , socket.SOCK_RAW , socket.ntohs(0x0003))
except socket.error, msg:
print 'Kod błędu : ' + str(msg[0]) + ' Wiadomość ' + msg[1]
sys.exit()
while True:
packet = s.recvfrom(interface1 , 65536, True, 1000)
packet = packet[0]
eth_length = 14
eth_header = packet[:eth_length]
eth = unpack('!6s6sH' , eth_header)
eth_protocol = socket.ntohs(eth[2])
print 'Docelowy adres MAC : ' + eth_addr(packet[0:6]) + ' Źródłowy adres MAC : ' + eth_addr(packet[6:12]) + ' Protokół : ' + str(eth_protocol)
if eth_protocol == 8 :
ip_header = packet[eth_length:20+eth_length]
iph = unpack('!BBHHHBBH4s4s' , ip_header)
version_ihl = iph[0]
version = version_ihl >> 4
ihl = version_ihl & 0xF
iph_length = ihl * 4
ttl = iph[5]
protocol = iph[6]
s_addr = socket.inet_ntoa(iph[8]);
d_addr = socket.inet_ntoa(iph[9]);
print 'Wersja : ' + str(version) + ' Długość nagłówka IP : ' + str(ihl) + ' TTL : ' + str(ttl) + ' Protokół : ' + str(protocol) + ' Adres źródłowy : ' + str(s_addr) + ' Adres docelowy : ' + str(d_addr)
if protocol == 6 :
t = iph_length + eth_length
tcp_header = packet[t:t+20]
tcph = unpack('!HHLLBBHHH' , tcp_header)
source_port = tcph[0]
dest_port = tcph[1]
sequence = tcph[2]
acknowledgement = tcph[3]
doff_reserved = tcph[4]
tcph_length = doff_reserved >> 4
print 'Port źródłowy : ' + str(source_port) + ' Port docelowy : ' + str(dest_port) + ' Numer sekwencji : ' + str(sequence) + ' Potwierdzenie : ' + str(acknowledgement) + ' Długość nagłówka TCP : ' + str(tcph_length)
h_size = eth_length + iph_length + tcph_length * 4
data_size = len(packet) - h_size
data = packet[h_size:]
print 'Dane : ' + data
elif protocol == 1 :
u = iph_length + eth_length
icmph_length = 4
icmp_header = packet[u:u+4]
icmph = unpack('!BBH' , icmp_header)
icmp_type = icmph[0]
code = icmph[1]
checksum = icmph[2]
print 'Typ : ' + str(icmp_type) + ' Kod : ' + str(code) + ' Suma kontrolna : ' + str(checksum)
h_size = eth_length + iph_length + icmph_length
data_size = len(packet) - h_size
data = packet[h_size:]
print 'Dane : ' + data
elif protocol == 17 :
u = iph_length + eth_length
udph_length = 8
udp_header = packet[u:u+8]
udph = unpack('!HHHH' , udp_header)
source_port = udph[0]
dest_port = udph[1]
length = udph[2]
checksum = udph[3]
print 'Port źródłowy : ' + str(source_port) + ' Port docelowy : ' + str(dest_port) + ' Długość : ' + str(length) + ' Suma kontrolna : ' + str(checksum)
h_size = eth_length + iph_length + udph_length
data_size = len(packet) - h_size
data = packet[h_size:]
print 'Dane : ' + data
else :
print 'Protokół inny niż TCP/UDP/ICMP'
print
def main():
root = Tk()
root.geometry("350x300+300+300")
app = Program(root)
root.mainloop()
if __name__ == '__main__':
main()
但在按下开始按钮后,我有一个追溯:
Tkinter回调中的异常回溯(最近一次调用最后一次):
在电话中输入文件“/usr/lib/python2.7/lib-tk/Tkinter.py”,第1489行 return self.func(* args)文件“example.py”,第80行,开头 thread.start_new_thread(eth_addr,())NameError:未定义全局名称'eth_addr'
任何人都可以帮我解决这个全局名称问题吗?
答案 0 :(得分:1)
在start
方法中,您引用了一个名为eth_addr
的变量,该变量未在任何地方定义。它也正是您的错误消息告诉您的内容。 (你可能正在寻找self.eth_addr
)
(此外,停止使用start_new_thread
- 它是一个永远不应该直接调用的低级原语。请改用threading.Thread
个对象。)