Introduction
The following script allows you to execute SSH script using an EXE/Script sensor. It will connect to the server, execute the given command and simply return the output to the sensor. That given, it can be used for either EXE/Script, EXE/Script (Advanced) or a EXE notification.
Please have a look at our guide for installing PowerShell based sensors for installation details.
Requirements
- Configured Windows credentials in the given host
- Configured Linux credentials in the given host
- Security context of the sensor is set to Use Windows credentials of parent device
Parameters
Parameter | Description |
---|---|
ComputerName | The Linux host you want to connect to (can be set to %host) |
Username | The Linux user you want to use (can be set to %linuxuser) |
Password | The password of the above user (can be set to %linuxpassword) |
KeyFilePath | The path of the keyfile, if you want to use key based authenticaion |
Command | The command you want to execute, including parameters |
AutoUpdateFingerPrint | Set this when calling the script to automatically update the SSH fingerprints of known devices. Careful, though! |
Version History
Date | Version | Notes |
---|---|---|
December 29th, 2016 | 1.1 | Bugfix for new Posh-SSH, where the session is stored differently AutoUpdateFingerprint works correctly now Posh-SSH is now installed automatically if not installed already |
August 5th, 2015 | 1.0 | Initial Release |
Script
# ___ ___ _____ ___ #| _ \ _ \_ _/ __| #| _/ / | || (_ | #|_| |_|_\ |_| \___| # NETWORK MONITOR #------------------- # Description: This sensor will use Posh-SSH to connect to the server # Parameters: # -Computername: The linux host you want to connect to # -Username Your linux username # -Password Your linux password # -Command The command to execute on your host, # -AutoUpdateFingerPrint Set this to when calling the script to automatically update the SSH fingerprints of known devices. # Careful, though! # ########## # # Version History # ---------------------------- # Version Date Description # 1.1 29/12/2016 [Fixed] Updated session handling # [Improved] Posh-SSH is now automatically installed # [Fixed] AutoUpdateFingerPrint works correctly now # 1.0 05/2015 Initial Release # # (c) 2015 Stephan Linke | Paessler AG # Parameters param( $Computername = "", $Username = "", $Password = "", $Port = 22, $KeyfilePath = "", $Command = "echo '0:Hello World'", $AutoUpdateFingerprint = $False ) # Check if our module loaded properly if (Get-Module -ListAvailable -Name Posh-SSH) { <# do nothing #> } else { # install the module automatically iex (New-Object Net.WebClient).DownloadString("https://gist.github.com/darkoperator/6152630/raw/c67de4f7cd780ba367cccbc2593f38d18ce6df89/instposhsshdev") } function This-FingerPrintUpdate(){ if($AutoUpdateFingerprint) { Remove-SSHTrustedHost $Computername } } # Includes Import-Module Posh-SSH function This-GetSSHCommandOutput(){ # Create new session and execute the command given if($keyfilePath.Length -eq 0){ # Generate credentials object for authentication $nopasswd = New-Object System.Security.SecureString $Credential = New-Object System.Management.Automation.PSCredential ($Username, $nopasswd) $Session = New-SSHSession -Computername $Computername -Credential $Credential -Acceptkey -KeyFile $keyfilePath -Port $Port } else { $Session = New-SSHSession -Computername $Computername -Acceptkey $true -KeyFile $keyfilePath -Port $Port } # Store the output of the command $Output = (Invoke-SSHCommand -SSHSession $Session -Command $Command).Output # Remove the session after we're done Remove-SSHSession -Name $Session | Out-Null # return the actual output Write-Host $Output.Trim(); } # Automatically update the fingerprint for the given host. This-FingerPrintUpdate; # echo the output to PRTG This-GetSSHCommandOutput;
Notes
The %linuxuser and %linuxpassword placeholders can't be used for notifications, you'll need to use a keyfile for authentication. Otherwise, you'll have to use clear text passwords within the parameters of the notification, which is not safe of course :)
The script is provided as is and may or may not work with your installation. If you need any further customizations for your environment, feel free to implement them. If you encounter any bugs, feel free to share them :)
Add comment