Skip to content

Instantly share code, notes, and snippets.

@seungwon0
Created July 30, 2012 07:04
Show Gist options
  • Save seungwon0/3205437 to your computer and use it in GitHub Desktop.
Save seungwon0/3205437 to your computer and use it in GitHub Desktop.
looks up MAC vendor information
#!/usr/bin/env perl
#
# mac-lookup-gui.pl - looks up MAC vendor information
#
# Looks up MAC vendor information using Net::MAC::Vendor module.
#
# Seungwon Jeong <[email protected]>
#
# Copyright (C) 2012 by Seungwon Jeong
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
use perl5i::2;
use Glib qw< TRUE FALSE >;
use Gtk2 -init;
use Net::MAC::Vendor;
my $entry = Gtk2::Entry->new;
$entry->signal_connect( changed => \&mac_address_changed_callback );
$entry->signal_connect( activate => \&look_up );
my $mac_address_label = Gtk2::Label->new_with_mnemonic('_MAC Address');
$mac_address_label->set_mnemonic_widget($entry);
my $button = Gtk2::Button->new('_Look up');
$button->set_sensitive(FALSE);
$button->signal_connect( clicked => \&look_up );
my $hbox = Gtk2::HBox->new( FALSE, 0 );
$hbox->pack_start( $mac_address_label, FALSE, FALSE, 5 );
$hbox->pack_start( $entry, TRUE, TRUE, 5 );
$hbox->pack_start( $button, FALSE, FALSE, 5 );
my $organization_label = Gtk2::Label->new;
$organization_label->set_markup('<b>Organization</b>');
$organization_label->set_alignment( 1.0, 0.0 );
my $organization_info_label = Gtk2::Label->new;
$organization_info_label->set_markup('<i>None</i>');
$organization_info_label->set_alignment( 0.0, 0.0 );
$organization_info_label->set_selectable(TRUE);
my $address_label = Gtk2::Label->new;
$address_label->set_markup('<b>Address</b>');
$address_label->set_alignment( 1.0, 0.0 );
my $address_info_label = Gtk2::Label->new;
$address_info_label->set_markup('<i>None</i>');
$address_info_label->set_alignment( 0.0, 0.0 );
$address_info_label->set_selectable(TRUE);
my $table = Gtk2::Table->new( 2, 2 );
$table->attach( $organization_label, 0, 1, 0, 1, [qw< shrink fill >],
[qw< shrink fill >], 5, 5 );
$table->attach( $organization_info_label, 1, 2, 0, 1, [qw< expand fill >],
[qw< shrink fill >], 5, 5 );
$table->attach( $address_label, 0, 1, 1, 2, [qw< shrink fill >],
[qw< expand fill >], 5, 5 );
$table->attach( $address_info_label, 1, 2, 1, 2, [qw< expand fill >],
[qw< expand fill >], 5, 5 );
my $vbox = Gtk2::VBox->new( FALSE, 0 );
$vbox->pack_start( $hbox, FALSE, FALSE, 5 );
$vbox->pack_start( $table, FALSE, FALSE, 5 );
my $window = Gtk2::Window->new;
$window->set_title('mac-lookup-gui.pl - looks up MAC vendor information');
$window->signal_connect( destroy => sub { Gtk2->main_quit; } );
$window->add($vbox);
$window->show_all;
my $tooltips = Gtk2::Tooltips->new;
my $tip = <<'END_TIP';
MAC Address Examples:
* usual form: 00:0d:93:29:f6:c2
* with hyphens: 00-0d-93-29-f6-c2
* first three bytes: 00:0d:93
* missing leading zero: 0:d:93
* missing all leading zeros: :d:93
END_TIP
$tooltips->set_tip( $entry, $tip->rtrim );
$tooltips->set_tip( $button, 'Look up MAC address' );
Gtk2->main;
sub mac_address_changed_callback {
my $mac_address = $entry->get_text;
$button->set_sensitive( length $mac_address > 0 );
return;
}
sub look_up {
my $mac_address = $entry->get_text;
return if length $mac_address == 0;
$button->set_sensitive(FALSE);
my $info_ref = Net::MAC::Vendor::lookup($mac_address);
if ( @{$info_ref} == 0 ) {
my $dialog = Gtk2::MessageDialog->new( $window, 'modal', 'info', 'ok',
'No vendor information.' );
$dialog->run;
$dialog->destroy;
$organization_info_label->set_markup('<i>None</i>');
$address_info_label->set_markup('<i>None</i>');
return;
}
$organization_info_label->set_text( shift @{$info_ref} );
$address_info_label->set_text( join "\n", @{$info_ref} );
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment