You can execute a command on Linux and Unix server from exMon using PowerShell. This tutorial will show how to execute Linux commands and get correct feedback both output and errors from the command and set correct exit codes so you can control process flows depended on the results.

The end results. Click for larger image.

  The tutorial uses the PoshSSH module for PowerShell. Additional information can be found here: http://www.powershellmagazine.com/2014/07/03/posh-ssh-open-source-ssh-powershell-module 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 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/c67de4f7cd780ba367cccbc2593f38d18ce6df89/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 Administrator, 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 Administrator, 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: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html 2. Generate and save public and private key. 3. From the Conversion menu, Export 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    
}