Running Linux commands in exMon

Created by: Gunnar Steinn Magnússon

In this article, you will read about running Linux commands in exMon. You can execute a command on Linux and Unix servers from exMon using PowerShell. This tutorial will show how to execute Linux commands and get correct feedback on both output and errors from the command and set correct exit codes so you can control process flows depending on the results.

 

The tutorial uses the PoshSSH module for PowerShell. Additional information can be found here

Security

The first part of the tutorial uses username and password authentication but it is advisable to use public-key authentication. The latter half of the tutorial shows how to set that up. Also, note that it is advisable to create a user on the Linux system with minimum privileges for the task.

  • Install the PoshSSH module First you need to add support for PoshSSH by running this command in PowerShell:
    iex (New-Object Net.WebClient).DownloadString
    ("https://gist.github.com/darkoperator/6152630/raw/c67de4f7cd780ba367cccbc2593f38d
    18ce6df89/instposhsshdev")
  • Create Linux execution script Create a PowerShell script on your file system, for example under C:\exMon\Scripts\LinuxCommand.ps1 and add the following script, replacing username and password:
    param([String]$command="",[String]$server="")

    $username = "gsm"
    $password = ConvertTo-SecureString "123123" -AsPlainText -Force
    $credential = New-Object -typename System.Management.Automation.PSCredential
    -argumentlist $username, $password

    try {
    $session = New-SshSession -ComputerName $server -Credential $credential -AcceptKey
    -ErrorVariable errorvar -ErrorAction Stop
    }
    catch
    {
    throw $errorvar
    }

    $res = Invoke-SSHCommand -Index 0 -Command $command

    Write-Host $res.Output

    if (0 -ne $res.ExitStatus)
    {
    throw "Exit Code "+ $res.ExitStatus +" - "+ $res.Error
    }
  • Create exMon Package In exMon Data Governance, create a package with the following command line, replacing the two placeholders:
    powershell C:\exMon\Scripts\LinuxCommand.ps1 -server "MY SERVER NAME" 
    -command "MY COMMAND"
  • Execute the package In exMon Data Governance, try to execute the package with the run action.

Setting up public-key authentication

It is advised to use public-key authentication instead of username and password.

  1. Start by downloading PuTTY gen from here
  2. Generate and save the public and private keys.
  3. From the Conversion menu, Export the OpenSSH key to C:\exMon\Scripts\
  4. On the Linux machine, follow these steps, replacing the public key from the output from PuTTY Gen:
    mkdir .ssh
    chmod 700 .ssh
    cd .ssh
    touch authorized_keys2
    echo "ssh-rsa AAAAB3NzaC...............kt7D1bQIVbbH5iZYULrp8mQ==" > authorized_keys2
  5. Change LinuxCommand.ps1 to use the public key credentials, replacing the passphrase to your passphrase.
    param([String]$command="",[String]$server="")

    $username = "gsm"

    $privatekey = "C:\exMon\Scripts\privatekey_openssh.ppk"
    $credential = New-Object System.Management.Automation.PSCredential($username,
    (ConvertTo-SecureString “123123123123” -AsPlainText -Force))


    try {
    $session = New-SshSession -ComputerName $server -Credential $credential
    -KeyFile $privatekey -AcceptKey -ErrorVariable errorvar -ErrorAction Stop
    }
    catch
    {
    throw $errorvar
    }


    $res = Invoke-SSHCommand -Index 0 -Command $command

    Write-Host $res.Output

    if (0 -ne $res.ExitStatus)
    {
    throw "Exit Code "+ $res.ExitStatus +" - "+ $res.Error
    }
Gunnar is the author of this solution article.