Example Perl Script - List Information and Shutdown

Here is an example Perl script which uses the API to print some information about the gateway and shut it down. The completed script (with shutdown code commented out for safety) is available as an attachment on this page.

Requirements

  • Perl

  • OpenSSL

  • Perl Modules

    • LWP::UserAgent

      • Crypt::SSLeay

    • HTTP::Cookies

    • JSON or JSON::XS

Code

Download the full source code:

  1. Start a new perl script.

    #!/usr/bin/perl   use strict; use HTTP::Cookies; use LWP::UserAgent; use Data::Dumper; use JSON;
  2. Create a new LWP::UserAgent object, and disable SSL certificate hostname verification, which is on by default in recent versions. Create a new HTTP::Cookies object and tell your user agent about it.

    # Must set this *before* creation of LWP::UserAgent object $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0; my $ua = new LWP::UserAgent;   # Sometimes both this and the VERIFY_HOSTNAME environment variable are required $ua->ssl_opts({'verify_hostname' => 0});   my $cookies = new HTTP::Cookies(); $ua->cookie_jar($cookies);
  3. Configure the script for your particular gateway

    my $GATEWAY_IP = 'CHANGEME'; my $USERNAME = 'admin'; my $PASSWORD = 'admin';
  4. Log in to your gateway. This will save your authentication cookies in your HTTP::Cookies object so that subsequent calls will be allowed.

  5. Build a call to the gateway.list method to get information about the gateway.

  6. Shut the gateway down remotely using system_shutdown.save.

Return to Documentation Home I Return to Sangoma Support