#!/usr/bin/env ruby # lifx-snoop is a utility that snoops on LIFX UDP traffic on both the broadcast port # and the peer broadcast port. # # Usage: # lifx-snoop [regexp, ...] # # By default, it will show all messages. Any arguments passed in will become # regular expressions, which will then match on the string representation of the # message. # # Example: # lifx-snoop Light::Get # Shows only Light::Get messages # lifx-snoop Light::State site=d073d5000000 # Shows only Light::State messages matching site d073d5000000 $LOAD_PATH << File.join(File.dirname(__FILE__), "..", "lib") require 'lifx' require 'time' matchers = ARGV.map do |arg| Regexp.new(arg, Regexp::IGNORECASE) end if matchers.empty? matchers << // end begin light_udp = LIFX::Transport::UDP.new('0.0.0.0', 56700) light_udp.add_observer(self, :message_received) do |message: nil, ip: nil, transport: nil| if matchers.all? { |m| message.to_s =~ m } puts "#{Time.now.iso8601(5)} BROADCAST: #{ip} #{message}" end end light_udp.listen peer_udp = LIFX::Transport::UDP.new('0.0.0.0', 56750) peer_udp.add_observer(self, :message_received) do |message: nil, ip: nil, transport: nil| if matchers.all? { |m| message.to_s =~ m } puts "#{Time.now.iso8601(5)} PEER: #{ip} #{message}" end end peer_udp.listen rescue LIFX::Message::UnsupportedProtocolVersion end puts "Listening on 56700 and 56750..." puts "^C to quit." sleep