A web form to launch your bash and powershell scripts

March 17, 2024
bash ctfreak devops powershell sysadmin

What’s the point of using a web interface to run scripts when you’re a terminal master?

Well, among other things:

I propose addressing these two use cases using the newly added parameterizable tasks in the latest release of Ctfreak.

Prerequisites

We need:

Configuration of the scriptsrv server

Connect to your Ctfreak instance with an admin account.

Start by adding the SSH key to connect to the server via SSH CredentialNew SSH Credential:

Adding SSH key

Then add the server itself via NodesInternal NodesNew node:

Adding the server

Make sure that the SSH Key for scriptsrv is selected as the Credential, then validate to create the scriptsrv node.

Ctfreak can now connect to the server with the specified SSH key to execute various tasks.

1st case: Generate a report for a given date range and send it by email

For our example, let’s assume that you have an executable generatereport on the scriptsrv server, allowing you to generate and send a sales report by email for a given period, recipient, and format. If we were to run this executable directly in a terminal, it might look something like this:

generatereport --from 2024-03-04 --to 2024-03-10 --recipient nigel@mycompany.zzz --format pdf

So, we’ll create a task in Ctfreak that, when executed, will dynamically generate a web form to enter these 4 parameters before launching the generatereport executable.

Creating the Reporting project

Let’s start by creating a project dedicated to reporting tasks.

Go to ProjectsNew Project

Creating the project

Validate to create the project.

Creating the task Generate and send sales report

Go to ProjectsReportingNew task

Selecting task type

Choose Bash Script as the task type.

New bash script

Fill in the Name, Script, and Node filter fields as indicated.

In the content of our script, the 4 expected values are provided through environment variables, all prefixed by CTP_:

#!/bin/bash
generatereport --from $CTP_FROM --to $CTP_TO --recipient $CTP_RECIPIENT --format $CTP_FORMAT

These variables correspond to the parameters that we will create by clicking the Add parameter button.

Let’s start with the start and end dates of our period:

Start and end dates

Regarding the emails of authorized recipients to receive the report, we will limit the possible choices to 2 emails via the Selector parameter type:

Recipient

For each option, when the Label field is filled, it substitutes Value during the execution form generation. We could have used it here to display Nigel and John in the form instead of their respective emails.

Finally, another Selector parameter type will allow choosing the format of the generated file (here PDF or Excel, but it’s pdf or xlsx that will be passed to our script):

Format

With all the necessary information filled in, all that’s left is to validate to create the task.

Executing the task

Go to ProjectsReportingGenerate and send sales reportExecute to bring up the execution form.

Generate and send a sales report

As you can see, our 4 parameters are well taken into account in the form generation.

Adding a business user

With our task operational, the next step is to hand it over to a business user for execution.

Go to UsersNew User

Creating a user

Validate to create the user.

By default, a non-admin user has no access to anything (except for logging in), so you need to give them the necessary rights.

Go to ProjectsReportingAccessEdit

Add Jack by giving him the Executor role.

Project access

Jack now has access to the Reporting project, but only to execute the tasks it contains.

2nd case: Check the status or restart / stop a Linux service

Creating the Sysadmin project

Let’s create a project dedicated to system administration tasks.

Go to ProjectsNew Project

Creating the project

Validate to create the project.

Creating the task Systemctl

Go to ProjectsSysadminNew task and like our previous case, choose Bash Script as the task type.

New bash script

Fill in the Name, Script, and Node filter fields as indicated.

Click the Add parameter button to add the list of services concerned (here Postgresql and Apache)

Service

As well as the commands to check the status, restart, or stop the service.

Command

Validate to create the task.

Executing the task

Go to ProjectsSysadminSystemctlExecute to bring up the execution form.

Service status

Execute the task and check the logs by clicking on the eye icon.

Logs

Running Powershell scripts instead of Bash

If your scriptsrv server is running Windows, you just need to make a few adjustments to run Powershell scripts instead of Bash:

The principle of injecting parameters as environment variables remains exactly the same. If we adapted the script from our first case, it would look like this:

generatereport --from $env:CTP_FROM --to $env:CTP_TO --recipient $env:CTP_RECIPIENT --format $env:CTP_FORMAT

Conclusion

As you have noticed, simply adding a web interface to a script offers many possibilities.

Start by adapting the examples presented in this article to your needs; they will already allow you to offer greater autonomy to your users.