-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrv_tx.py
executable file
·58 lines (45 loc) · 1.54 KB
/
drv_tx.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
55
56
57
#!/usr/bin/env python
# generate uni/multicast udp frames in python
import fcntl, sys, socket, struct
def if_addr(ifname):
print 'ifname %s' % ifname
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
def unicast(iface, count, dest='10.10.10.1:9876',
data="some test data"):
"""
send count unicast frames originating on iface
"""
dest_ip, dest_port = dest.split(':')
dest_port = int(dest_port)
ifaddr = if_addr(iface)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.bind((ifaddr, 0))
for i in range(0, int(count)):
sock.sendto(data, (dest_ip, dest_port))
def mcast(iface, count, dest='224.1.1.1:9876',
data="some test data"):
"""
send count mcast frames originating on iface
"""
dest_ip, dest_port = dest.split(':')
dest_port = int(dest_port)
ifaddr = if_addr(iface)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
# select us as outgoing iface
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF,
socket.inet_aton(ifaddr))
for i in range(0, int(count)):
sock.sendto(data, (dest_ip, dest_port))
if __name__ == "__main__":
import __main__
cmd = sys.argv[1]
arglist = sys.argv[2:]
print 'arglist: %s' % arglist
if cmd in dir(__main__):
fn = getattr(__main__, cmd)
fn(*arglist)