forked from tmpbci/sniffeal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsniffealmidi.py
87 lines (69 loc) · 2.52 KB
/
sniffealmidi.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Sniff packets from interface en0 using python module scapy (2.3.1)
and generate led color for bhoreal in usb midi mode depending on packet port number
Led are midi notes (0 to 63)
Color is a midi parameter (velocity) so 0 to 127.
To modify the network interface (wifi/ethernet), change it in the last code line.
Uses mido to send MIDI messages directly in python.
v0.3
By Sam Neurohack
LICENCE : CC
'''
import argparse
import mido
import sys
from time import sleep
from scapy.all import *
def print_summary(pkt):
global counter
out = 0
if IP in pkt:
ip_src=pkt[IP].src
ip_dst=pkt[IP].dst
if TCP in pkt:
tcp_sport=pkt[TCP].sport
tcp_dport=pkt[TCP].dport
if tcp_sport < 50000:
print " IP src " + str(ip_src) + " TCP sport " + str(tcp_sport)
out = tcp_sport
if tcp_dport < 50000:
print " IP dst " + str(ip_dst) + " TCP dport " + str(tcp_dport)
out = tcp_dport
if UDP in pkt:
udp_sport=pkt[UDP].sport
udp_dport=pkt[UDP].dport
if udp_sport < 50000:
print " IP src " + str(ip_src) + " UDP sport " + str(udp_sport)
out = udp_sport
if udp_dport < 50000:
print " IP dst " + str(ip_dst) + " UDP dport " + str(udp_dport)
out = udp_dport
if ARP in pkt and pkt[ARP].op in (1,2):
print " ARP"
out = 67
outport.send(mido.Message('note_on', note=counter, velocity=out%127))
sleep(0.001)
counter += 1
if counter > 63:
counter = 0
if __name__ == "__main__":
parse = argparse.ArgumentParser(description="Sniff traffic on a network interface and send it to BHOREAL MIDI controller")
parse.add_argument("-l", "--list-devices", help="list usable MIDI devices", action="store_true")
parse.add_argument("-i", "--interface", help="network interface to be sniffed", default="en0")
parse.add_argument("-d", "--device", help="MIDI device", default="Arduino Leonardo")
args = parse.parse_args()
if args.list_devices:
print "Available MIDI devices:"
for i in mido.get_output_names():
print i
sys.exit(0)
try:
outport = mido.open_output(args.device)
except IOError:
print "error: MIDI device not found"
sys.exit(1)
global counter
counter = 0
sniff(iface=args.interface, prn=print_summary, store=0)