Salut les gars,
J'espère que vous pouvez m'aider. Désolé pour mon anglais, ce n'est pas ma première langue. Je suis nouveau à la programmation et je suis coincé dans mon premier projet. J'essaie de programmer un simple générateur de trafic de données avec Tkinter et Python 2.7.
J'utilise la bibliothèque dpkt comme base pour l'envoi de paquets. Au début, j'ai écrit un script que je peux utiliser dans le terminal et ça marche très bien.
Générateur de paquets dans Tkinter
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
Dans ma prochaine étape j'ai essayé de créer une interface graphique pour le rendre plus pratique et utilisable aussi pour les autres utilisateurs. J'ai donc créé un masque dans Tkinter, où je peux entrer l'adresse IP, sélectionner la taille et le nombre de paquets. En outre je veux l'option que je peux choisir le type du paquet (ICMP/UDP). Le code pour l'envoi de paquets UDP que je veux ajouter plus tard en tant que fonction.
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()
Le problème est que quand je frappe envoyer il n'y a pas de réaction dans le terminal. Je ne comprends pas pourquoi le script ne fonctionne pas. C'est mon premier code et j'y travaille déjà assez souvent. Je fais probablement un petit détour mais j'espère que vous pourrez m'aider et que vous pourrez me dire quel est le problème. Merci beaucoup. Alex
Sry pour ma réponse tardive. Je n'ai pas eu le temps de continuer mon projet. Même lorsque je modifie le bouton avec le fond actif, je n'obtiens aucune réaction lorsque j'appuie sur envoyer. Est-il possible que vous ne pouvez pas exécuter un tel script (pour l'envoi de paquets) dans une fonction? Ou dois-je travailler avec des cours? – junker1248
vous pouvez, juste identifier les blocs de travail principaux et créer 'def' pour eux .. et comme vous avez déjà utilisé la fonction' send_icmp' c'est déjà modulaire, donc vous n'avez pas besoin de faire beaucoup de travail dessus –
Merci ! Je l'ai eu en cours d'exécution. Maintenant, je n'utilise que la bibliothèque de socket pour créer des paquets, c'est beaucoup plus rapide. Mon seul problème est que je ne peux pas vraiment changer le type de paquets avec les radiobuttons. Est-il préférable d'utiliser différentes fonctions pour chaque type, ou seulement une fonction avec des variables modifiables via radiobutton? – junker1248