#!/usr/bin/perl
#Copyright (C) 2007 Fonality Inc. (fonality.com)
#
#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 2
#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.
#
use URI;
use strict;
my $asterisk = '/usr/sbin/asterisk';
chomp( my @peers = `$asterisk -rx 'sip show peers'` );
shift @peers; #Remove first header line
for( @peers )
{
my @peer = split' ', $_;
if( $peer[0] =~ m#/# )
{
$peer[0] =~ s#/.+$##; #Remove everything after the first slash.
}
my $peer_info = fetch_peer_info( $peer[0] );
next unless $peer_info;
$peer_info->{Status} =~ s/\(\d+[^)]+\)\s*$//;
my $contact = $peer_info->{'Reg. Contact'} || $peer_info->{'Full Contact'};
next unless $contact;
my $contact_uri = URI->new( $contact );
my $contact_host = $contact_uri->host;
my $arp_out = `/sbin/arp -Da $contact_host`;
$arp_out =~ /at (\S+)/ or next;
my $mac = $1;
if( $mac )
{
print "UA: ", $peer_info->{Useragent}, "\n";
print "Contact: ", $contact, "\n";
print "Status: ", $peer_info->{'Status'}, "\n";
print "MAC: $mac\n";
print "\n";
}
}
sub fetch_peer_info
{
my( $id ) = @_;
my @peer_info_lines = `$asterisk -rx 'sip show peer $id'`;
chomp @peer_info_lines;
return unless @peer_info_lines;
my $peer_info = {};
for( @peer_info_lines )
{
my( $k, $v ) = split /\s*:\s*/, $_, 2;
for( $k, $v ) { s/^\s+//; s/\s+$//; }
$peer_info->{$k} = $v;
}
return $peer_info;
}