Calling the Connect Tunnel Client API fromPowershell
Has anyone had any success calling the Connect Tunnel API from Powershell and could you share any sample code?
All I am trying to do (initially) is call the API and get status from the client.
The API is a very simple named pipe API. The issue in this case is certainly my lack of skill/knowledge. But I was hoping I could get a sample that I could build on.
Maybe Sonicwall could provide a sample Powershell script to get status from Connect Tunnel?
If you need details of the API you can request it from support or send me a message.
Best Answer
-
Doug_Daniel Newbie ✭
I have gotten this working (I can call the API and get a result) - I think the problem was the format of my JSON requests but I have tried and abandoned many snippets of powershell along the way.
I used IONinja to monitor the pipe during normal operations and saw that the JSON request does not need anything pre-pended to it. I am gettting valid results now with the JSON request below:
$vpnmsg = '
{
"Mode":"auto",
"Action":"status",
"PipeName":"CTCB"
}
'
# for testing write to screen
write-host $vpnmsg
# Convert the JSON request to Base64 per API documentation
$vpnmsg = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($vpnmsg))
write-host $vpnmsg
0
Answers
@Doug_Daniel , when you send an API request, make sure that the request contains the name of named pipe for receiving the status from Connect Tunnel client.
Here's a simple PS script that spawns a named pipe for receiving status from client.
$pipeName = "CTCB"
$pipeServer = New-Object System.IO.Pipes.NamedPipeServerStream($pipeName)
try {
while ($true) {
"Waiting for connection on '$pipeName'"
$pipeServer.WaitForConnection()
"Connection established"
$pipeReader = New-Object System.IO.StreamReader($pipeServer)
$response = $pipeReader.ReadLine()
"Received response: `n$response"
Start-Sleep -Seconds 1
$pipeServer.Disconnect()
"Disconnected"
}
}
finally {
$pipeServer.Dispose()
}
When you send the JSON request, make sure that the request contains "CTCB" so that CT client can send the status back.
Thank you! I will try this in the morning. I appreciate the response.
I am still not seeing anything in the callback. I may need another example showing how to build and submit the JSON request
The key two things I should need to do are:
1 - properly format the JSON request - (see code below)
2 - properly submit the JSON request to the SnwlConnect-{Name of the user account} named pipe
Here is my attempt at formatting the JSON request - Can you see if the request is valid? Then I will focus on submitting the request and monitoring for callback on the other pipe
# build JSON reequest to send to SNWLConnect-[userid] pipe
# callback pipe name shoudl be a variable
# are there any other required fields in the request?
# do I need the square brackets?
$JSONRequest = '
[
{
"Mode":"auto",
"Action":"status",
"PipeName":"CTCB"
}
]
'
# for testing write to screen
write-host $JSONRequest
# Convert the JSON request to Base64 per API documentation
$JSONRequest64 = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($JSONRequest))
# Prepend required data to encrypted JSON request per API documentation
$JSONRequestFull = "ConnectTunnel://" + $JSONRequest64
write-host $JSONRequestFull