SonicWall SSL VPN Session Counter
Just created a ds to count SonicWall SSL VPN sessions. Locator FMN27M. Would love some feedback and code improvements as this is my first groovy script.
- Not a fan of just dropping the exit code as I have. There must be a better way to implement validations/error checking and output appropriate exit codes
- Would like to see other methods for counting the users. Maybe matching on the string "User Name" and counting the lines that follow?
- Could see this growing to include user session length
- Would be nice to only apply the ds to a resource if ssl vpn server was running
For ease here is sample output from the firewall and the script.
<span class="pln">=======================
Active SSLVPN Sessions:
=======================
User Name Client Virtual IP Client WAN IP Login Time Inactivity Time Logged In
user1 10.10.10.10 6.6.6.6 1799 Minutes 0 Minutes 01/23/2020 09:29:52
user2 10.10.10.11 5.5.5.5 460 Minutes 0 Minutes 01/24/2020 07:49:31
user3 10.10.10.12 4.4.4.4 368 Minutes 0 Minutes 01/24/2020 09:22:08
user4 10.10.10.13 3.3.3.3 224 Minutes 0 Minutes 01/24/2020 11:45:54
user5 10.10.10.14 2.2.2.2 170 Minutes 0 Minutes 01/24/2020 12:39:37
user6 10.10.10.15 1.1.1.1 13 Minutes 0 Minutes 01/24/2020 15:15:49 </span>
<span class="pln">import com.santaba.agent.groovyapi.expect.Expect;
hostname = hostProps.get("system.hostname");
userid = hostProps.get("ssh.user");
passwd = hostProps.get("ssh.pass");
// initialize a variable to contain the actual host prompt
def actualPrompt = "";
def sslvpn_user_count = 0;
// open an ssh connection and wait for the prompt
ssh_connection = Expect.open(hostname, userid, passwd);
ssh_connection.expect(">");
// capture full prompt e.g. user@host
ssh_connection.before().eachLine
{ line ->
actualPrompt = line;
}
// display the ssl vpn sessions
ssh_connection.send("show ssl-vpn sessions \n");
ssh_connection.expect(actualPrompt + ">");
cmd_output = ssh_connection.before();
// read thru multiline output
// rows with 9 columns are user sessions
// increment to total user sessions
cmd_output.eachLine
{ line ->
row_length = line.split(/\s+/);
if ( row_length.size() == 9 )
{
sslvpn_user_count++
}
}
ssh_connection.send("exit");
println(sslvpn_user_count);
return 0;</span>
pperreault
2 comments
pperreault
OPThanks for the suggestions and pointers @Michael Rodrigues.
Michael Rodrigues
@pperreault good stuff.
If I were to implement this in product I would probably write a regex with capture groups to match a valid user line. That way you can be more sure than the 9 column line is actually a user entry, and you can use the capture groups to easily dump out other items of interest.
This is a pretty common pattern in our core DataSources, most of the Nimble Storage stuff follows this pattern.
The regexes in those scripts might look scary but it's pretty easy to make in something like https://regexr.com
It's pretty solid looking for a first Groovy script. I would consider exiting the SSH session before you process the output, no need to keep that connection open after you have the data in a variable.
As far as error handling, you could wrap the initial Expect session setup in a try/catch. You can also use exitValue() on your Expect client object to ensure that your `show` command returned a successful return code before parsing the output.
Here's a pretty extreme example of a Groovy script with multiple return codes for specific issues: