How To Execute Script On A Server From Mule Or CloudHub?

Streamlining Shell Script Execution in Mule Flows

Accessing shell scripts hosted on remote servers can be critical for executing Mule flows. Fortunately, there are two effective ways to accomplish this effortlessly. Let’s look into each approach:

PowerShell Connector

PowerShell Connector is available in exchange, which executes the script on the external server. A bash script or PowerShell script can be executed on the system using this connector.

Note: The system on which the script will be executed should have PowerShell installed.

For details on the connector, check the below link:

https://docs.mulesoft.com/windows-powershell-connector/latest/

PowerShell connector can be used in following 3 use cases- 1) executing PowerShell script, 2) executing bash script and 3) To run the command directly

Executing a PowerShell Script:

a. Place the PS script that you want to execute at a particular location on server. In this scenario, place the script named “sample2.ps1” at the designated location

For instance, consider this script, which is essentially a basic “hello world” program, designed to accept two parameters.

param (

……..[string]$firstname,

……..[string]$lastname

… )

“Hello World ” + $firstname + ” ” + $lastname | Out-File -FilePath “/home/ec2-user/dummy.txt”

  1. Create a flow in mule with accepts 2 query parameters a and b from a http listener.

  b.  Look for PowerShell connector in Mule exchange and add it to palette and POM.xml.

POM.xml:

<dependency>

                  <groupId>com.mulesoft.connectors</groupId>

                  <artifactId>mule-powershell-connector</artifactId>

                  <version>2.1.3</version>

                  <classifier>mule-plugin</classifier>

            </dependency>

  c.  Drag the PowerShell execute script with exception handling to your project

   d. Configure the connector by providing the username and password details of the system you intend to connect to

   e. For the PowerShell connector, add the file you want to execute and the params you want to pass to the connector in the File Content.

   File Content:  “/home/ec2-user/sample2.ps1 ” ++ attributes.queryParams.a default “”++ ” ” ++ attributes.queryParams.b default

   f.  Run the project to obtain the output specified within the ps1 file.

  Running a Bash Script Using PowerShell Connector:

 The process is similar to executing a PowerShell script, with the key difference being the inclusion of bash commands in the File Content of the connector.

   File Content: bash /home/ec2-user/sample2.sh

Executing Commands with PowerShell Connectors:

You can also run commands directly from the Mule code, which will directly execute on the external system using PowerShell execute command connector.

 In the command section, the requisite command for execution must be written.

Invoke Static Connector:

For systems that do not have PowerShell installed and only have the bash shell installed, you can use the following Java code using the Invoke static connector.

1. Create a Java file in the java folder. In that, write the following method:
You can give your command to the server using the script path variable. The parameters host, username, and password contain the configuration of the server you wish to call.

public class Callssh {

               public static void executeScript(String host, String username, String  password, String scriptPath) {

        try {

            JSch jsch = new JSch();

            Session session = jsch.getSession(username, host, 22);//username host and password and port take from property file.

            session.setPassword(password);

            session.setConfig(“StrictHostKeyChecking”, “no”);

            session.connect();

            ChannelExec channel = (ChannelExec) session.openChannel(“exec”);

            channel.setCommand(“bash ” + scriptPath); // or whatever shell you need to execute the script

            channel.setErrStream(System.err);

            channel.connect();

            // You can handle the output if needed

            channel.disconnect();

            session.disconnect();

        } catch (JSchException e) {

            e.printStackTrace();

        }

    }

}

  1. Configure Invoke static to call the method:

 2.  So, your overall flow looks like this:

Following these guidelines makes executing scripts on Mule or CloudHub servers a more simplified and efficient operation, assuring optimal performance and dependability in your workflows.

To summarize, optimizing script execution in Mule and CloudHub is critical for increasing integration workflow efficiency and dependability. Organizations can improve automation in their Mule projects by employing tools and connectors like the PowerShell Connector and Invoke Static Connector. Organizations may maximize the potential of their integration platforms and promote commercial success by using best practices and remaining current with evolving trends.

Author: Rohit Sharma

Author: Rohit Sharma

Senior Manager

Contributor:  Anand Challa

Contributor: Anand Challa

Software Engineer