Using REST API to update Sonus SBA on SBC1000/2000

Summary

SBA required different type of update:

  • SbcComms update
  • Lync Cumulative Update
  • Windows Update RollUp

All this update can be done trough WebUI but they can also be trigger using the Sonus REST API that can be scripted with Powershell. This article describe all the steps required to update an SBA. It also contains a “All In One” script that will completely update an ASM automatically.

Step by Step

Why Curl ?

For this article, we will need to use curl. cURL is a computer software project providing a library and command-line tool for transferring data using HTTP. In our example. cURL is used to upload the file into the SBC since the current Powershell command (Invoke-RESTrequest) doesn’t support multi-part upload. CURL doesn’t require any installation. Found it here: http://curl.haxx.se/download.html

Login with Powershell

Defines the Login resource used to login to the Sonus SBC REST API. This is the first request that needs to be made before you can access any of the other REST Resources on the Sonus SBC 1000/2000 system. Using Resource – login.

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=restuser&Password=restpass"
 
$url = "https://$NodeUrl/rest/login"
$Result = Invoke-RestMethod -Uri $url -Method Post -Body $BodyValue -SessionVariable ps

Login with curl

This step allow to login to the SBC with curl, and store the PHP Session ID into Powershell in order to reuse it later.

1
2
3
4
5
6
7
8
9
$login = ./curl.exe -k --data "Username=restuser&Password=restpass" -i -v https://192.168.123.53/rest/login
$PHPSESSID = 0
foreach ($line in $login) {
    if ($line.contains("PHPSESSID=")) {
        $correctline = $line.split(";")
        $splitedline = $correctline.split("=")
        $PHPSESSID = $splitedline[1]
    }
}

Update SbcComms

This operation allows to install Sonus SBC Communication service updates on the ASM. Using Resource – sbaconfig.

Query

1
curl.exe --cookie PHPSESSID=$PHPSESSID -k -i https://192.168.1.15/rest/sbaconfig?action=sbaupgrade -F sbaInstallFilename=@setup.msi

Response

1
2
3
4
5
6
<?xml version="1.0"?>
<root>
 <status>
  <http_code>200</http_code>
 </status>
</root>

Action end on status

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$url = "https://192.168.1.15/rest/sbaactionstatus"
Invoke-RestMethod -Uri $url -Method GET -WebSession $MyPs
 
<?xml version="1.0"?>
<root>
 <status>
  <http_code>200</http_code>
 </status>
 <sbaactionstatus href="https://192.168.1.15/rest/sbaactionstatus">
  <rt_Success>1</rt_Success>
  <rt_ActionType>15</rt_ActionType>
  <rt_PercentComplete>100</rt_PercentComplete>
  <rt_Duration>5</rt_Duration>
  <rt_StatusText>setup.msi: Update Complete</rt_StatusText>
  <rt_AllActionsBitmap>0</rt_AllActionsBitmap>
  <rt_ActionStartTime>0</rt_ActionStartTime>
 </sbaactionstatus>
</root>

Install the Latest Lync SBA Cumulative Update (CU)

To ensure the Lync SBA is running the latest Lync Server patches, administrators should install the latest Cumulative Update. Using Resource – sbaconfig.

Query

1
2
3
4
5
6
7
8
9
curl.exe --cookie PHPSESSID=$PHPSESSID -k -i https://192.168.1.15/rest/sbaconfig?action=sbaupgrade -F sbaInstallFilename=@ASM_Lync_2013_CU4.pkg
<pre>
<h3>Response</h3>
<pre lang="xml" line="1">
<?xml version="1.0"?><root>
 <status>
  <http_code>200</http_code>
 </status>
</root>

Action end on status

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$url = "https://192.168.1.15/rest/sbaactionstatus"
Invoke-RestMethod -Uri $url -Method GET -WebSession $MyPs
 
<?xml version="1.0"?>
<root>
 <status>
  <http_code>200</http_code>
 </status>
 <sbaactionstatus href="https://192.168.1.15/rest/sbaactionstatus">
  <rt_Success>1</rt_Success>
  <rt_ActionType>15</rt_ActionType>
  <rt_PercentComplete>100</rt_PercentComplete>
  <rt_Duration>0</rt_Duration>
  <rt_StatusText>updates installed</rt_StatusText>
  <rt_AllActionsBitmap>127</rt_AllActionsBitmap>
  <rt_ActionStartTime>0</rt_ActionStartTime>
 </sbaactionstatus>
</root>

Install the Latest Sonus Windows Update pack

To ensure the ASM is running the latest Windows Updates patches, administrators should install the latest RollUp Pack. Using Resource – sbaconfig.

Command to upload the Windows updates

Query

1
curl.exe --cookie PHPSESSID=$PHPSESSID -k -i https://192.168.1.15/rest/sbaconfig?action=sbaupgrade -F sbaInstallFilename=@ASM_Rollup_2014-01.pkg

Response

1
2
3
4
5
<?xml version="1.0"?><root>
 <status>
  <http_code>200</http_code>
 </status>
</root>

Action end on status

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$url = "https://192.168.1.15/rest/sbaactionstatus"
Invoke-RestMethod -Uri $url -Method GET -WebSession $MyPs
 
<?xml version="1.0"?>
<root>
 <status>
  <http_code>200</http_code>
 </status>
 <sbaactionstatus href="https://192.168.1.15/rest/sbaactionstatus">
  <rt_Success>1</rt_Success>
  <rt_ActionType>15</rt_ActionType>
  <rt_PercentComplete>100</rt_PercentComplete>
  <rt_Duration>0</rt_Duration>
  <rt_StatusText>updates installed</rt_StatusText>
  <rt_AllActionsBitmap>127</rt_AllActionsBitmap>
  <rt_ActionStartTime>0</rt_ActionStartTime>
 </sbaactionstatus>
</root>

Command to apply the Windows updates

Query the Readiness

Query
1
2
$url = "https://192.168.1.15/rest/sbaconfig"
Invoke-RestMethod -Uri $url -Method GET  -WebSession $ps
Response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?xml version="1.0"?>
<root>
 <status>
  <http_code>200</http_code>
</status>
 <sbaconfig href="https://192.168.123.53/rest/sbaconfig">
  <AclEnable>1</AclEnable>
  <DHCPEnabled>0</DHCPEnabled>
  <DNSServer1>192.168.123.101</DNSServer1>
  <DNSServer2>192.168.123.102</DNSServer2>
  <RemoteDesktopEnabled>1</RemoteDesktopEnabled>
  <ipv4Address>192.168.123.54</ipv4Address>
  <ipv4Gateway>192.168.123.1</ipv4Gateway>
  <ipv4Netmask>255.255.255.0</ipv4Netmask>
  <rt_AsmReachable>1</rt_AsmReachable>
  <rt_AsmSupportMode>1</rt_AsmSupportMode>
  <rt_DHCPEnabled>0</rt_DHCPEnabled>
  <rt_Domain>sonuslab.ads</rt_Domain>
  <rt_GpoDetected>2</rt_GpoDetected>
  <rt_HostName>SBA2000C</rt_HostName>
  <rt_IPv4Address>192.168.123.54</rt_IPv4Address>
  <rt_IPv4Gateway>192.168.123.1</rt_IPv4Gateway>
  <rt_IPv4Netmask>255.255.255.0</rt_IPv4Netmask>
  <rt_ImageDescription>Lync Server 2013 SBA</rt_ImageDescription>
  <rt_Licensed>1</rt_Licensed>
  <rt_LoginNames></rt_LoginNames>
  <rt_MediationCpuTime>6</rt_MediationCpuTime>
  <rt_MediationMemUsage>48002608</rt_MediationMemUsage>
  <rt_MediationSvcState>0</rt_MediationSvcState>
  <rt_MediationVersion>5.0.8308.0</rt_MediationVersion>
  <rt_NumEthPorts>2</rt_NumEthPorts>
  <rt_PendingRestartNeeded>2</rt_PendingRestartNeeded>
  <rt_QoEReportFailure>0</rt_QoEReportFailure>
  <rt_QoEReportSuccess>0</rt_QoEReportSuccess>
  <rt_RDPEnabled>1</rt_RDPEnabled>
  <rt_RegistrarCpuTime>0</rt_RegistrarCpuTime>
  <rt_RegistrarMemUsage>23866632</rt_RegistrarMemUsage>
  <rt_RegistrarSvcState>0</rt_RegistrarSvcState>
  <rt_RegistrarVersion>5.0.8308.0</rt_RegistrarVersion>
  <rt_ReplicationErrors></rt_ReplicationErrors>
  <rt_ReplicationFQDN>SBA2000C.SONUSLAB.ADS</rt_ReplicationFQDN>
  <rt_ReplicationLastStatusReport>1418394867</rt_ReplicationLastStatusReport>
  <rt_ReplicationLastUpdateCreation>1418394863</rt_ReplicationLastUpdateCreation>
  <rt_ReplicationProductVersion>5.0.8308.0</rt_ReplicationProductVersion>
  <rt_ReplicationUpToDate>1</rt_ReplicationUpToDate>
  <rt_SQLCpuTime>0</rt_SQLCpuTime>
  <rt_SQLMemUsage>233693796</rt_SQLMemUsage>
  <rt_SQLSvcState>0</rt_SQLSvcState>
  <rt_SQLVersion>2011.110.2100.60</rt_SQLVersion>
  <rt_StatusText>none</rt_StatusText>
  <rt_UXComVersion>3.2.0.315</rt_UXComVersion>
  <rt_UXSBACpuTime>15</rt_UXSBACpuTime>
  <rt_UXSBAMemUsage>108333744</rt_UXSBAMemUsage>
  <rt_UpTime>537</rt_UpTime>
  <rt_UpdateState>1</rt_UpdateState>
  <rt_WinCpuUsage>23</rt_WinCpuUsage>
  <rt_WinMemFree>2047868928</rt_WinMemFree>
  <rt_WindowsVersion>6.1.0.7601</rt_WindowsVersion>
  <rt_ASM_BIOSManufacturer>American Megatrends Inc.</rt_ASM_BIOSManufacturer>
  <rt_ASM_BIOSVersion>10.03.02</rt_ASM_BIOSVersion>
  <rt_ASM_ComputerModel>Calpella Platform</rt_ASM_ComputerModel>
  <rt_ASM_HDDSize>160GB</rt_ASM_HDDSize>
  <rt_ASM_ImageType>Lync Server 2013 SBA</rt_ASM_ImageType>
  <rt_ASM_PhysicalMemory>3950MB RAM</rt_ASM_PhysicalMemory>
  <rt_ASM_ProcessorCPUs>4</rt_ASM_ProcessorCPUs>
  <rt_ASM_ProcessorClockSpeed>~2.5GHz</rt_ASM_ProcessorClockSpeed>
  <rt_ASM_ProcessorName>Intel(R) Core(TM) i7 CPU         610  @ 2.53GHz</rt_ASM_ProcessorName>
  <CardType>11</CardType>
  <LineCardLicenseType>0</LineCardLicenseType>
  <LineCardNumLicensedPorts>0</LineCardNumLicensedPorts>
  <Location>11</Location>
  <MfgWeek>36</MfgWeek>
  <MfgYear>10</MfgYear>
  <PartNumber>423-40007</PartNumber>
  <SerialNumber>C4000710360046</SerialNumber>
  <Vendor>Sonus</Vendor>
  <VersionNumber>101</VersionNumber>
 </sbaconfig>
</root>

Query the installation

Query
1
2
$url = "https://192.168.1.15/rest/sbaconfig?action=installwindowsupdates"
Invoke-RestMethod -Uri $url -Method POST -Body "" -WebSession $ps
Response
1
2
3
4
5
6
<?xml version="1.0"?>
<root>
 <status>
  <http_code>200</http_code>
 </status>
</root>
Action end on status
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$url = "https://192.168.1.15/rest/sbaactionstatus"
Invoke-RestMethod -Uri $url -Method GET -WebSession $MyPs
 
<?xml version="1.0"?>
<root>
 <status>
  <http_code>200</http_code>
 </status>
 <sbaactionstatus href="https://192.168.1.15/rest/sbaactionstatus">
  <rt_Success>1</rt_Success>
  <rt_ActionType>35</rt_ActionType>
  <rt_PercentComplete>100</rt_PercentComplete>
  <rt_Duration>7050</rt_Duration>
  <rt_StatusText>Install Updates complete</rt_StatusText>
  <rt_AllActionsBitmap>127</rt_AllActionsBitmap>
  <rt_ActionStartTime>0</rt_ActionStartTime>
 </sbaactionstatus>
</root>

All in one

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
### 1. Login
$NodeUrl = "192.168.1.15"
$NodeLogin = "user"
$NodePassword = "pass"
 
### 3. Update SbcComms
$SbcCommsFilePath = "C:\Demo REST\setup.msi"
 
### 4. Install the Latest Lync SBA Cumulative Update (CU)
$LyncCU = "C:\Demo REST\ASM_Lync_2013_CU8.pkg"
 
### 5. Install the Latest Sonus Windows Update pack
$WindowsUpdate = "C:\Demo REST\ASM_Rollup_2014-10.pkg"
 
###################################
#### Define some function Helper
###################################
### Return the result of the request
Function BasicHandler {
    Param($MyResult)
 
    [xml]$XmlResult = $MyResult.Substring(5)
    if($XmlResult.root.status.http_code.contains("200")) {
        return 1
    } else {
        return "Error Code:<"+$XmlResult.root.status.app_status.app_status_entry.code+"> Param:<"+$XmlResult.root.status.app_status.app_status_entry.params+">"
    }
}
 
### Return the result of the request
Function BasicHandlerCurl {
    Param($MyResult)
    $FiltredMyResult = ""
    foreach ($line in $MyResult) {
        if ($line.StartsWith("<")) {
            $FiltredMyResult += $line
        } elseif ($line.StartsWith(" ")) {
            $FiltredMyResult += $line
        }
    }
    [xml]$XmlResult = $FiltredMyResult
    if($XmlResult.root.status.http_code.contains("200")) {
        return 1
    } else {
        return "Error Code:<"+$XmlResult.root.status.app_status.app_status_entry.code+"> Param:<"+$XmlResult.root.status.app_status.app_status_entry.params+">"
    }
}
 
### Return the result of the last action
function LookCompletionStatus {
    Param($MyNodeUrl, $MyPs)
    $url = "https://$MyNodeUrl/rest/sbaactionstatus"
    $timer = 0
    while($timer -lt 3600) {
        $Result = Invoke-RestMethod -Uri $url -Method GET -WebSession $MyPs
        if ((BasicHandler $Result) -ne 1) { return 0}
        [xml]$FinalResult= $Result.Substring(5)
        if ($FinalResult.root.sbaactionstatus.rt_Success -eq 1) {
            if ($FinalResult.root.sbaactionstatus.rt_PercentComplete -eq 100) { return 1 }
        } else {
            return $FinalResult.root.sbaactionstatus.rt_StatusText
        }
        sleep 10
        $timer += 10
        write-host "- "$FinalResult.root.sbaactionstatus.rt_PercentComplete"% "$FinalResult.root.sbaactionstatus.rt_StatusText
    }
    return "Timout out"
}
 
###################################
#### Main Code
###################################
 
$ActualStep = 0
 
### 1. Log in with Powershell
$ActualStep = "1. Log in with Powershell"
 
### 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=$NodeLogin&Password=$NodePassword"
 
$url = "https://$NodeUrl/rest/login"
$Result = Invoke-RestMethod -Uri $url -Method Post -Body $BodyValue -SessionVariable ps
 
### 2. Log in with Curl
$ActualStep = "2. Log in with Curl"
 
$login = ./curl.exe -k --data "Username=$NodeLogin&Password=$NodePassword" -i -v https://$NodeUrl/rest/login
$PHPSESSID = 0
foreach ($line in $login) {
    if ($line.contains("PHPSESSID=")) {
        $correctline = $line.split(";")
        $splitedline = $correctline.split("=")
        $PHPSESSID = $splitedline[1]
    }
}
 
### 3. Update SbcComms
$ActualStep = "3. Update SbcComms"
 
$Result = ./curl.exe --cookie PHPSESSID=$PHPSESSID -k -i https://$NodeUrl/rest/sbaconfig?action=sbaupgrade -F sbaInstallFilename=@$SbcCommsFilePath
if ((BasicHandlerCurl $Result) -ne 1) {
    write-host $ActualStep" Failed: "(BasicHandler $Result)
    return 0
}
if ((LookCompletionStatus $NodeUrl $ps) -ne 1) {
    write-host $ActualStep" Failed: "(BasicHandlerCurl $Result)
    return 0
}
 
 
### 4. Install the Latest Lync SBA Cumulative Update (CU)
$ActualStep = "4. Install the Latest Lync SBA Cumulative Update (CU)"
 
$Result = ./curl.exe --cookie PHPSESSID=$PHPSESSID -k -i https://$NodeUrl/rest/sbaconfig?action=sbaupgrade -F sbaInstallFilename=@$LyncCU
if ((BasicHandlerCurl $Result) -ne 1) {
    write-host $ActualStep" Failed: "(BasicHandler $Result)
    return 0
}
 
# We need to log again since we may timeout
### Login
$BodyValue = "Username=$NodeLogin&Password=$NodePassword" 
$url = "https://$NodeUrl/rest/login"
$Result = Invoke-RestMethod -Uri $url -Method Post -Body $BodyValue -SessionVariable ps
 
# Check for the completion of the CU install
if ((LookCompletionStatus $NodeUrl $ps) -ne 1) {
    write-host $ActualStep" Failed: "(BasicHandlerCurl $Result)
    return 0
}
 
# We need to log again since we may timeout
### Log in with Curl
$login = ./curl.exe -k --data "Username=$NodeLogin&Password=$NodePassword" -i -v https://$NodeUrl/rest/login
$PHPSESSID = 0
foreach ($line in $login) {
    if ($line.contains("PHPSESSID=")) {
        $correctline = $line.split(";")
        $splitedline = $correctline.split("=")
        $PHPSESSID = $splitedline[1]
    }
}
 
### 5. Install the Latest Sonus Windows Update pack
$ActualStep = "5. Install the Latest Sonus Windows Update pack"
 
$Result = ./curl.exe --cookie PHPSESSID=$PHPSESSID -k -i https://$NodeUrl/rest/sbaconfig?action=sbaupgrade -F sbaInstallFilename=@$WindowsUpdate
if ((BasicHandlerCurl $Result) -ne 1) {
    write-host $ActualStep" Failed: "(BasicHandler $Result)
    return 0
}
if ((LookCompletionStatus $NodeUrl $ps) -ne 1) {
    write-host $ActualStep" Failed: "(BasicHandlerCurl $Result)
    return 0
}
 
while (1) {
    $url = "https://$NodeUrl/rest/sbaconfig"
    $ResultGeneral = Invoke-RestMethod -Uri $url -Method GET  -WebSession $ps
 
    if ((BasicHandler $ResultGeneral) -ne 1) {
        write-host $ActualStep" Failed: "(BasicHandler $ResultGeneral)
        return 0
    }
 
    [xml]$XmlResultGeneral = $ResultGeneral.Substring(5)
    switch ($XmlResultGeneral.root.sbaconfig.rt_UpdateState) {
        0 {write-host "Status is Unknown"; return 0; break;}
        1 {write-host "Status is UpToDate";
            $url = "https://$NodeUrl/rest/sbaconfig"
            $Result = Invoke-RestMethod -Uri $url -Method GET  -WebSession $ps
            if ((BasicHandler $Result) -ne 1) {
                write-host $ActualStep" Failed: "(BasicHandler $Result)
                return 0
            }
            [xml]$XmlResult = $Result.Substring(5)
            if($XmlResult.root.sbaconfig.rt_PendingRestartNeeded.contains("1")) {
                # Restart the ASM to apply the Update
                $url = "https://$NodeUrl/rest/sbaconfig?action=asmreboot"
                $Result = Invoke-RestMethod -Uri $url -Method POST -Body "" -WebSession $ps
                if ((BasicHandler $Result) -ne 1) {
                    write-host $ActualStep" Failed: "(BasicHandler $Result)
                    return 0
                }
                if ((LookCompletionStatus $NodeUrl $ps) -ne 1) {
                    write-host $ActualStep" Failed: "(BasicHandlerCurl $Result)
                    return 0
                }
                break;
            }
            return 0;
            break;}
        5 {write-host "Status is Error"; return 0; break;}
        2 {
              ### 6. Apply the update include into the Latest Sonus Windows Update pack
              $url = "https://$NodeUrl/rest/sbaconfig?action=installwindowsupdates"
              $Result = Invoke-RestMethod -Uri $url -Method POST -Body "" -WebSession $ps
              if ((BasicHandler $Result) -ne 1) {
                  write-host $ActualStep" Failed: "(BasicHandler $Result)
                  return 0
              }
              if ((LookCompletionStatus $NodeUrl $ps) -ne 1) {
                  write-host $ActualStep" Failed: "(BasicHandlerCurl $Result)
                  return 0
              }
              break;
          }
        3 {write-host "Wait for a install to end"; Start-Sleep -m 3; break;}
        4 {write-host "Wait for a final status"; Start-Sleep -m 3; break;}
        default {write-host "Found an unpredictable state for the Update. Shutdown..."; return 0}
    }
}

One thought on “Using REST API to update Sonus SBA on SBC1000/2000

  1. Hi Adrien,
    Thank you for this helpful page ! but could you please attached curl.exe to avoid downloading elsewhere !

Leave a Reply

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