]> git.defcon.no Git - hermes/blob - guc-clients/listphones
Updates: showsipuser tool to show a sip user, listphones to list registered phones...
[hermes] / guc-clients / listphones
1 #!/usr/bin/perl
2 use strict;
3
4 use Data::Dumper;
5
6 use Getopt::Long;
7 use Net::LDAP;
8 use Net::LDAP::Control::Paged;
9 use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED );
10 use LWP;
11 use JSON;
12 use AppConfig;
13
14 my $api_key;
15
16 my $help;
17 my $username;
18 my $configfile = undef;
19 my $remove = 0;
20 my $dryrun = 0;
21 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
22 my ($user, $sipuser, $mac);
23
24 my $config = AppConfig->new({ CREATE => 1 });
25
26 $config->define("api_url=s");
27 $config->define("api_keyfile=s");
28
29 foreach (
30 "/usr/local/etc/hermes/hermes_config",
31 "/usr/local/etc/hermes/config",
32 "/etc/hermes/config",
33 $ENV{"HOME"} . "/.hermes/config",
34 $ENV{"HOME"} . "/.hermes_config",
35 ) { $configfile = $_ if ( -f $_ ); }
36
37 GetOptions(
38 "help" => \$help,
39 "macaddress=s" => \$mac,
40 "username=s" => \$username,
41 "configfile=s" => \$configfile,
42 );
43
44 if (
45 (( $configfile ) && ( not -f $configfile ))
46 )
47 {
48 $help = 1;
49 }
50
51 $config->file( $configfile );
52
53 if ( ( not $config->api_url ) ||
54 ( not $config->api_keyfile ) ||
55 ( $config->api_keyfile && not -f $config->api_keyfile ) )
56 {
57 $help = 1;
58 }
59
60 if ( $help ) {
61 print <<END_HELP;
62 WARNING: This tool assumes that only one domain
63 is registered with Kamailio. For Multidomain-setup,
64 this tool must be rewritten!
65
66 Verify that the following options are set:
67 --configfile=s|--config|-c
68 --macaddress=s|--mac|-m
69
70 Verify the contents of the configuration file.
71 Verify that the key-file exists.
72 END_HELP
73 exit; }
74
75 open KEY, "<" . $config->api_keyfile;
76 chomp( $api_key = <KEY> );
77 close KEY;
78
79 if ( $mac )
80 {
81 # Do stuff to the MAC adress.
82 $mac =~ s/[:-]//g if ( $mac =~ m/((?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})/i);
83 $mac = lc $mac;
84 if ( not $mac =~ m/^[a-f0-9]{12}/ )
85 {
86 printf("Malformed MAC adress.\n");
87 exit;
88 }
89 }
90
91 $g_ua = LWP::UserAgent->new;
92 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
93
94 login_apikey();
95
96 # First: fetch a supported domain from the API...
97 $data = exec_apinode("domain/list", undef);
98 if ( $data->{'response'} eq 'ok' )
99 {
100 $domain = $data->{'list'}[0];
101 }
102 else
103 {
104 printf("Unable to get domain name. Aborting\n");
105 logout();
106 exit;
107 }
108
109 $data = exec_apinode("phone/list", undef );
110 if ( not $data->{'response'} eq 'ok' )
111 {
112 printf("Failed to query list of phones. Aborting\n");
113 logout();
114 exit;
115 }
116
117 my $t = $data->{'list'};
118 my @phones = @$t;
119
120 foreach my $p ( @phones )
121 {
122 $data = undef;
123 $data = exec_apinode("phone/get", { 'mac' => $p });
124 if ( $data->{'response'} eq 'ok' )
125 {
126 my $t = $data->{'list'};
127 my @users = @$t;
128 foreach my $u ( @users )
129 {
130 printf("%-24s%-24s\n", $p, $u);
131 }
132 }
133 else
134 {
135 printf("%-24s-24s\n", $p, "unassigned");
136 }
137 }
138
139 logout();
140 ################################################################################################
141 sub exec_apinode($$)
142 {
143 my $node = shift;
144 my $param = shift;
145
146 my ( $response, $data );
147
148 $session = "" if not defined $session;
149 $auth_key = "" if not defined $auth_key;
150 my $url = $config->api_url . "/" . $node;
151
152 $param->{'session'} = $session;
153 $param->{'auth_key'} = $auth_key;
154
155 $response = $g_ua->post( $url, $param );
156 if ( $response->is_success )
157 {
158 if ( $response->content =~ m/\s*{/ )
159 {
160 $data = decode_json( $response->content);
161 }
162 else
163 {
164 $data = $response->content;
165 }
166
167 }
168 return $data;
169 }
170
171 sub login_apikey
172 {
173 my $response = $g_ua->post( $config->api_url . "/auth/login",
174 [ "api_key" => $api_key ] );
175
176 my $data = decode_json( $response->content) if $response->is_success;
177 die("HTTP error") unless $response->is_success;
178
179 if ( $data->{'response'} eq "ok" )
180 {
181 $session = $data->{'session'};
182 $auth_key = $data->{'auth_key'};
183 }
184 else
185 {
186 print "Unable to log in to Hermes API\n";
187 exit;
188 }
189 undef $data; undef $response;
190 }
191
192 sub logout
193 {
194 my $response = $g_ua->post( $config->api_url . "/auth/logout",
195 [ "session" => $session ] );
196 die("HTTP error") unless $response->is_success;
197 undef $session; undef $auth_key;
198 }