forked from p4lang/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.py
executable file
·54 lines (45 loc) · 1.63 KB
/
send.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
import argparse
import socket
from time import sleep
from scapy.all import IP, TCP, UDP, Ether, get_if_hwaddr, get_if_list, sendp
def get_if():
iface = None
for i in get_if_list():
if "eth0" in i:
iface = i
break
if not iface:
print("Cannot find eth0 interface")
exit(1)
return iface
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--p", help="Protocol name To send TCP/UDP etc packets", type=str)
parser.add_argument("--des", help="IP address of the destination", type=str)
parser.add_argument("--m", help="Raw Message", type=str)
parser.add_argument("--dur", help="in seconds", type=str)
args = parser.parse_args()
if args.p and args.des and args.m and args.dur:
addr = socket.gethostbyname(args.des)
iface = get_if()
if args.p == 'UDP':
pkt = Ether(src=get_if_hwaddr(iface), dst="ff:ff:ff:ff:ff:ff") / IP(dst=addr, tos=1) / UDP(dport=4321, sport=1234) / args.m
pkt.show2()
try:
for i in range(int(args.dur)):
sendp(pkt, iface=iface)
sleep(1)
except KeyboardInterrupt:
raise
elif args.p == 'TCP':
pkt = Ether(src=get_if_hwaddr(iface), dst="ff:ff:ff:ff:ff:ff") / IP(dst=addr, tos=1) / TCP() / args.m
pkt.show2()
try:
for i in range(int(args.dur)):
sendp(pkt, iface=iface)
sleep(1)
except KeyboardInterrupt:
raise
if __name__ == '__main__':
main()