I've created this blog to catalog software ideas, code examples and everything in between. I've been in the industry for over 3 years and have worked on a variety of projects from Identity and Access Management, to Web Application Security, to Java/Ruby application development.

Read More

Ruby Connection Notifier

Lately I’ve been spending a lot of time at coffee shops in the morning, and because of that have spent a lot of time on wireless networks that are not exactly trustworthy. I always thought it would be nice to get a little notification on incoming established tcp/udp connections. Just to know. So I created this simple ruby script that uses Growl to notify you when an inbound connection is established. Instead of using ruby/pcap to do the job, this script simply relies on the output of the Unix netstat command. The code is below, it requires the Growl Ruby binding which is available here and RubyCocoa which is available here.

#!/usr/bin/ruby
#
#  connection_notifier.rb
#  ConnectionNotifier
#
#  Created by Paul Codding on 2008-09-27.
#  Copyright 2008 id3al Solutions
. All rights reserved.
#
#  Released under the BSD license.
require 'rubygems'
require File.dirname(__FILE__) + '/Growl'

class Connection
  include Comparable
  attr_accessor :protocol, :local_ip, :remote_ip, :local_port, :remote_port, :id

  def initialize(connection_entry)
    pieces = connection_entry.split(" ")
    @protocol = pieces[0]
    @local_ip = pieces[3].split(".")[0..3].join(".")
    @remote_ip = pieces[4].split(".")[0..3].join(".")
    @local_port = pieces[3].split(".").last.to_i
    @remote_port = pieces[4].split(".").last.to_i
    @id = pieces[4].split(".").join()
  end

  def (other_connection)
    @id  other_connection.id
  end

  def to_s
    "#{@protocol} connection established from #{@remote_ip} to port #{@local_port}"
  end
end

class ConnectionNotifier
  GROWL_APP_NAME="Connection Notifier"

  def initialize
    @ports = Array.new(1024)
    @ports.fill { |port| port += 1}
    @connections = Array.new
    @growl = GrowlNotifier.new(GROWL_APP_NAME,['Ruby Connection Notifier'],nil,
              OSX::NSWorkspace.sharedWorkspace().iconForFileType_('rb'))
    @growl.register()

    # Pre-populate connections array so we're not automatically notified about
    # existing established connections
    check_for_new_connections(false)
    build_list_of_listening_ports()
  end

  # Poll for changes in netstat output
  def poll
    while true do
      sleep(2)
      check_for_new_connections(true)
    end
  end

  # Check for a new connection in the output of netstat
  def check_for_new_connections(notify)
    netstat_output = `netstat -na`
    for connection_entry in netstat_output do
      if connection_entry.include?("ESTABLISHED")
        connection = Connection.new(connection_entry)

        if connection.local_port != nil && @ports.include?(connection.local_port.to_i) \
          && !@connections.include?(connection)
          @connections << connection
          if (notify)
            @growl.notify('Ruby Connection Notifier', 'Connection Established',
              connection.to_s)
          end
        end
      end
    end
  end

  def build_list_of_listening_ports
    netstat_output = `netstat -na`
    for connection_entry in netstat_output do
      if connection_entry.include?("LISTEN")
        connection = Connection.new(connection_entry)
        if !@ports.include?(connection.local_port)
          @ports << connection.local_port
        end
      end
    end
  end
end

conn = ConnectionNotifier.new
conn.poll

I’ve created a launch agent configuration for the script as well so it can start when you login. The configuration and source code is located in the ConnectionNotifier.zip file.

To run the script, either execute the script directly, or place the com.paulcodding.connection_notifier.agent.plist file in your ~/Library/LaunchAgents directory and log in again. Just note that you need to update the path to the script within the agent file before it will work. Once the script is running and you attempt a connection from an external machine, or the localhost you should see a growl notification like the image below.

Growl Notification

Growl Notification

Tags: ,

Filed under:Software

Leave a Reply