Tkinter中的数据包生成器

时间:2016-05-10 09:59:21

标签: python function variables tkinter dpkt

大家好,我希望你能帮助我。对不起我的英语,它不是我的第一语言。我是编程的新手,我被困在第一个项目中。我尝试用Tkinter和Python 2.7编写一个简单的数据流量生成器。
我使用dpkt库作为发送数据包的基础。在初学者我写了一个脚本,我可以在终端运行,它完全正常。

import dpkt
import socket,  random

# input
ip_dst = raw_input('IP-address of destination: ')
n = input('number of packets 2^n: ')
num =2**(n)
# packet size
b = input('size of packets (number between 1 and 5950): ')
while (b<1 or b>5950):
    print('Invalid number!')
    b = input('size of packets (number between 1 and 5950): ')
# script for sending packets
z=0
while z<=num:
    print z
    echo = dpkt.icmp.ICMP.Echo()
    echo.id = random.randint(0,0xffff)
    echo.seq = random.randint(0,0xffff)

    echo.data  = 'hello world'*b                    # changing size with factor b

    icmp = dpkt.icmp.ICMP()
    icmp.type = dpkt.icmp.ICMP_ECHO
    icmp.data = echo

    s = socket.socket(socket.AF_INET, socket.SOCK_RAW, dpkt.ip.IP_PROTO_ICMP)
    s.connect((ip_dst,1))                           # ip-address of destination
    sent = s.send(str(icmp))
    print 'sent %d bytes' %sent
    z=z+1

在我的下一步中,我尝试创建一个GUI,使其更方便,也可用于其他用户。所以我在Tkinter中创建了一个掩码,在那里我可以输入IP地址,选择数据包的大小和数量。此外,我想要一个选项,我可以选择数据包的类型(ICMP / UDP)。发送UDP数据包的代码我想稍后作为函数添加。

from Tkinter import *
import ttk
import tkFont
import dpkt
import socket, random

sent_packets = 0

# send icmp
def send_icmp(sent_packets, s1,ip_dst):
     while (sent_packets<=n):
         print sent_packets
         echo = dpkt.icmp.ICMP.Echo()
         echo.id = random.randint(0,0xffff)
         echo.seq = random.randint(0,0xffff)

         echo.data  = 'hello world'*(s1)

         icmp = dpkt.icmp.ICMP()
         icmp.type = dpkt.icmp.ICMP_ECHO
         icmp.data = echo

         s = socket.socket(socket.AF_INET, socket.SOCK_RAW, dpkt.ip.IP_PROTO_ICMP)
         s.connect((ip_dst,1))
         sent = s.send(str(icmp))
         print 'sent %d bytes' %sent
         sent_packets += 1
         mainWin.update()


mainWin= Tk()
mainWin.title('Traffic Generator')
mainWin.columnconfigure (0, weight=1)
mainWin.rowconfigure(0, weight=1)

ip_dst = StringVar()

# Frame settings
mainFrame = Frame(mainWin, width=200, height=100)
mainFrame.grid_propagate(0)
mainFrame.grid()
mainFrame.columnconfigure(0, weight=1)
mainFrame.columnconfigure(1, weight=1)
mainFrame.columnconfigure(2, weight=1)
mainFrame.rowconfigure(0, weight=1)
mainFrame.rowconfigure(1, weight=1)
mainFrame.rowconfigure(2, weight=1)
mainFrame.rowconfigure(3, weight=1)


# Headline
label_1 = Label(mainWin, text="Traffic Generator", font="Helvetica 11 bold")
label_1.grid(column=0, row=0, padx=15, pady=5, sticky=(N,W), columnspan=2)

# Input

# IP-address:
label_2 = Label(mainWin, text="Please enter IP-address of destination: ", font=tkFont.Font(family="Helvetica", size=10))
label_2.grid(column=0, row=1, padx=15, pady=5, sticky=(N,W))

entry_1 = Entry(mainWin, textvariable=ip_dst, width=13)
entry_1.grid(column=1, row=1, padx=15, pady=5, sticky=(N,E))
entry_1.focus()

# type:
label_4 = Label(mainWin, text="Select packet type: ", font=tkFont.Font(family="Helvetica", size=10))
label_4.grid(column=0, row=2, padx=15, pady=5, sticky=(N,W))
type = IntVar()
Radiobutton(mainWin, text="ICMP", variable=type, value="send_icmp").grid(row=2, column=1, sticky=(N,W), padx=15, pady=5, columnspan=2)
Radiobutton(mainWin, text="UDP", variable=type, value="send_udp").grid(row=2, column=1, sticky=(N,W), padx=70, pady=5, columnspan=2)

# size:
label_3 = Label(mainWin, text="Select packet size: ", font=tkFont.Font(family="Helvetica", size=10))
label_3.grid(column=0, row=3, padx=15, pady=5, sticky=(N,W))

size1 = IntVar()
size_slide1 = Scale(mainWin, from_=1, to=5900, orient='horizontal', variable= size1, length=200, resolution=10, showvalue=0)
size_slide1.set(3000)
size_slide1.grid(column=1, row=3, padx=15, pady=5, sticky=(N,E))

s1= str(size1)

Label(mainWin, textvariable= size1).grid(column=2, row=3, padx=15, pady=5, sticky=(N,E)) 


# number:
label_4 = Label(mainWin, text="Select number of packets: ",font=tkFont.Font(family="Helvetica", size=10))
label_4.grid(column=0, row=4, padx=15, pady=5, sticky=(N,W))

num1 = int()
Label(mainWin, text="2^n packets: ", font=tkFont.Font(family="Helvetica", size=10)).grid(column=1, row=4, padx=15, pady=5, sticky=(W,E))
num_slide1 = Scale(mainWin, from_=1, to=500, orient='horizontal', variable = num1, length=200, resolution=2, showvalue=0)
num_slide1.set(32)
num_slide1.grid(column=1, row=5, padx=15, pady=5, sticky=(N,E))
Label(mainWin, textvariable= num1).grid(column=2, row=5, padx=15, pady=5, sticky=(N,E))
number1 = 2**(num1)

# send:
Button(mainWin, text="Send packets", command=type, width=13).grid(column=2, row=6) # radiobuttons select value of variable 'type' which executes selected function (e.g. def send_icmp) of selected type


mainWin.mainloop()

问题是,当我点击发送时,终端没有反应。我不明白脚本为什么不运行。这是我的第一个代码,我已经有很长一段时间了。我可能会绕道而行,但我希望你能帮助我,并告诉我问题是什么。 非常感谢你。 亚历

1 个答案:

答案 0 :(得分:2)

脚本运行得非常好,它只是你需要调整的按钮。可能你需要在鼠标悬停时突出显示该按钮,使用类似activebackgroundactiveforeground

的内容

我通过tcpdump进行了检查,或者您可以在发送数据包的远程IP上使用wireshark。