]>
git.defcon.no Git - hermes/blob - guc-clients/listphones
8 use Net
::LDAP
::Control
::Paged
;
9 use Net
::LDAP
::Constant
qw( LDAP_CONTROL_PAGED );
18 my $configfile = undef;
21 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
22 my ($user, $sipuser, $mac);
24 my $config = AppConfig
->new({ CREATE
=> 1 });
26 $config->define("api_url=s");
27 $config->define("api_keyfile=s");
30 "/usr/local/etc/hermes/hermes_config",
31 "/usr/local/etc/hermes/config",
33 $ENV{"HOME"} . "/.hermes/config",
34 $ENV{"HOME"} . "/.hermes_config",
35 ) { $configfile = $_ if ( -f
$_ ); }
39 "macaddress=s" => \
$mac,
40 "username=s" => \
$username,
41 "configfile=s" => \
$configfile,
45 (( $configfile ) && ( not -f
$configfile ))
51 $config->file( $configfile );
53 if ( ( not $config->api_url ) ||
54 ( not $config->api_keyfile ) ||
55 ( $config->api_keyfile && not -f
$config->api_keyfile ) )
62 WARNING: This tool assumes that only one domain
63 is registered with Kamailio. For Multidomain-setup,
64 this tool must be rewritten!
66 Verify that the following options are set:
67 --configfile=s|--config|-c
68 --macaddress=s|--mac|-m
70 Verify the contents of the configuration file.
71 Verify that the key-file exists.
75 open KEY, "<" . $config->api_keyfile;
76 chomp( $api_key = <KEY> );
81 # Do stuff to the MAC adress.
82 $mac =~ s/[:-]//g if ( $mac =~ m/((?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})/i);
84 if ( not $mac =~ m/^[a-f0-9]{12}/ )
86 printf("Malformed MAC adress.\n");
91 $g_ua = LWP::UserAgent->new;
92 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
96 # First: fetch a supported domain from the API...
97 $data = exec_apinode("domain/list", undef);
98 if ( $data->{'response'} eq 'ok' )
100 $domain = $data->{'list'}[0];
104 printf("Unable to get domain name. Aborting\n");
109 $data = exec_apinode("phone/list", undef );
110 if ( not $data->{'response'} eq 'ok' )
112 printf("Failed to query list of phones. Aborting\n");
117 my $t = $data->{'list'};
120 foreach my $p ( @phones )
123 $data = exec_apinode("phone/get", { 'mac' => $p });
124 if ( $data->{'response'} eq 'ok' )
126 my $t = $data->{'list'};
128 foreach my $u ( @users )
130 printf("%-24s%-24s\n", $p, $u);
135 printf("%-24s-24s\n", $p, "unassigned");
140 ################################################################################################
146 my ( $response, $data );
148 $session = "" if not defined $session;
149 $auth_key = "" if not defined $auth_key;
150 my $url = $config->api_url . "/" . $node;
152 $param->{'session'} = $session;
153 $param->{'auth_key'} = $auth_key;
155 $response = $g_ua->post( $url, $param );
156 if ( $response->is_success )
158 if ( $response->content =~ m/\s*{/ )
160 $data = decode_json( $response->content);
164 $data = $response->content;
173 my $response = $g_ua->post( $config->api_url . "/auth/login",
174 [ "api_key" => $api_key ] );
176 my $data = decode_json( $response->content) if $response->is_success;
177 die("HTTP error") unless $response->is_success;
179 if ( $data->{'response'} eq "ok" )
181 $session = $data->{'session'};
182 $auth_key = $data->{'auth_key'};
186 print "Unable to log in to Hermes API\n";
189 undef $data; undef $response;
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;