Using Powershell 3.0 and REST to access Sonus SBC1000/2000

Summary

Since release 3.0 of Sonus SBC 1000/2000 firmware, REST license is free of charge and you can use it for multiple scenarios.

REST APIs are provided for developers who want to programmatically integrate the Sonus SBC 1000/2000 into their applications, and for administrators who want to script interactions with the Sonus SBC 1000/2000.

One of these scenarios is scripting provisioning, that allows you to program multiple SBCs with same parameters, in a one shot execution instead of having to program manually one by one each SBCs.

This is often the case when you need to deploy multiple SBCs in a same country, creating the same routing and transformation tables manually each time.

Here are some quick start tips to let you easily jump into REST programming through Microsoft Powershell.

Prerequisite

  • Make sure you loaded 3.x base licenses into the SBC to have the REST license as “unlimited
  • You need to have an SBC account with REST or Administrator access level
  • Windows Management Framework 3.0: http://www.microsoft.com/en-us/download/details.aspx?id=34595  (Windows 7 download: Windows6.1-KB2506143-x64.msu)
  • To Check your Version:
1
$host.version

Login

Login using a public signed certificate

1
2
3
4
5
6
7
8
9
### Load the Certificate x509
$CertPathDer = "Desktop\Rest\Cert.der"
$certDer = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($CertPath)
 
### Login
$BodyValue = "Username=user&Password=pass"
 
$url = "https://192.168.123.50/rest/login"
Invoke-RestMethod -Uri $url -Certificate $certDer -Method Post -Body $BodyValue -SessionVariable ps

Login without certificate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
### Allow self Sign Cert
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
 
    public class IDontCarePolicy : ICertificatePolicy {
        public IDontCarePolicy() {}
        public bool CheckValidationResult(
            ServicePoint sPoint, X509Certificate cert,
            WebRequest wRequest, int certProb) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = new-object IDontCarePolicy
 
### Login
$BodyValue = "Username=user&Password=pass"
 
$url = "https://192.168.123.50/rest/login"
Invoke-RestMethod -Uri $url -Method Post -Body $BodyValue -SessionVariable ps

Basic requests

Create resources (PUT)

This action allows the user to create a new resource

1
2
3
$BodyValue = "Description=Default SIP Server"
$url = "https://192.168.123.50/rest/sipservertable/8"
Invoke-RestMethod -Uri $url -Method PUT -Body $BodyValue -WebSession $ps

Modify resources (POST)

This action allows the user to modify an existing resource

1
2
3
$BodyValue = "Description=Default SIP TOTO"
$url = "https://192.168.123.50/rest/sipservertable/8"
Invoke-RestMethod -Uri $url -Method POST -Body $BodyValue -WebSession $ps

Get resources (GET)

This action allows the user to read the content of an existing resource

1
2
$url = "https://192.168.123.50/rest/sipservertable/8"
Invoke-RestMethod -Uri $url -Method GET -WebSession $ps

Delete resources (DELETE)

1
2
$url = "https://192.168.123.50/rest/sipservertable/8"
Invoke-RestMethod -Uri $url -Method DELETE -Body "" -WebSession $ps

Advanced Requests

Backup Configuration

1
2
3
$DestFile = "Desktop\Rest\backup.tar.gz"
$url = "https://192.168.123.50/rest/system?action=backup"
Invoke-RestMethod -Uri $url -Method POST -Body "" -WebSession $ps -OutFile $DestFile

Scripting with REST

Basic Handler (PUT, POST, GET, DELETE)

This function allows the user to analyze the result of a request

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Function BasicHandler {
    Param($ResultIn)
    [xml]$XmlResultIn = $ResultIn.Substring(5)
    if($XmlResultIn.root.status.http_code.contains("200")) {
        return "Success"
    } else {
        return "Error Code:<"+$XmlResultIn.root.status.app_status.app_status_entry.code+"> Param:<"+$XmlResultIn.root.status.app_status.app_status_entry.params+">"
    }
}
 
####### Expected
PS C:> BasicHandler $Result
Success
or
Error Code: Param:<8|SIPServerTable>

Content Handler (GET)

This function allows the user to read the content of a specific field

1
2
3
4
5
[xml]$FinalResult= $Result.Substring(5)
 
####### Expected Result
PS C:> $FinalResult.root.sipservertable.Description
Default SIP TOTO

Conclusion

Looking at this API and the capability of Powershell there could be great thing that can be done.I am still looking at a solution that allow to upload a config or firmware through multi-part upload on Powershell. If you have any solution feel free to comment this post.

There is few example of usage for PS and REST:

One thought on “Using Powershell 3.0 and REST to access Sonus SBC1000/2000

Leave a Reply

Your email address will not be published. Required fields are marked *