1

大家好,
我希望你们能帮助我。对不起我的英语,这不是我的第一语言。我是编程新手,我被困在我的第一个项目中。我尝试使用 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()

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

4

1 回答 1

2

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

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

于 2016-05-10T10:44:00.597 回答