Skip to content

Instantly share code, notes, and snippets.

@bryanvine
Last active May 12, 2018 21:07
Show Gist options
  • Save bryanvine/f8dd15e5998755f095ad536e652f7a49 to your computer and use it in GitHub Desktop.
Save bryanvine/f8dd15e5998755f095ad536e652f7a49 to your computer and use it in GitHub Desktop.
Quick Script - Linux SSH Remoting Wrapper
<#
BV-LinuxSSHRemoting.ps1
https://www.bryanvine.com/2018/05/powershell-script-invoke.html
Author: Bryan Vine
Last updated: 05/1/2018
Description: This function simplifies running linux shell commands over SSH while returning the output
#>
#Requires -version 2
Function Invoke-RemoteShellCommand{
<#
.SYNOPSIS
Uses Putty's Plink to remotely execute commands via SSH and sudo
.DESCRIPTION
Wrapper function that simplifies sending remote shell commands to run as sudo and returning the output.
New servers will need to have their key accepted upon the first connection, but won't prompt again after.
Obviously requires SSH server is enabled on the target host(s).
.PARAMETER Host
Target Server(s) or IP(s) to execute the command on.
.PARAMETER Username
Username on the linux server(s).
.PARAMETER Password
Password for the linux user (not ideal, better to use Credentials instead).
.PARAMETER Credentials
Secure system credentials for the linux server(s).
.PARAMETER Command
Shell command to be ran.
.PARAMETER NoSudo
By default, commands run under sudo. Including this switch makes the command run without sudo
.PARAMETER Options
Additional Parameters for plink (see putty plink documentation)
.EXAMPLE
Invoke-RemoteShellCommand -Credentials $creds -Target ubuntu01 -Command 'apt-get install powershell'
This will connect to ubuntu01 uses the $creds user & password and install powershell core.
.LINK
https://www.bryanvine.com/2018/05/powershell-script-invoke.html
.LINK
https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
.NOTES
Author: Bryan Vine
Last updated: 05/1/2018
#>
[cmdletbinding()]
Param(
[ValidateNotNullorEmpty()][Parameter(Position=0,Mandatory)][String[]]$Host,
[Parameter(Position=1,Mandatory,ParameterSetName='ClearTextAuth')][String]$Username,
[Parameter(Position=2,Mandatory,ParameterSetName='ClearTextAuth')][String]$Password,
[Parameter(Position=1,Mandatory,ParameterSetName='SecureAuth')][String]$Credentials,
[ValidateNotNullorEmpty()][Parameter(Position=4,Mandatory,ValueFromPipeline=$true)][String[]]$Command,
[Parameter(Position=3)][String]$Options,
[switch]$NoSudo
)
Begin{
#Test if putty plink exists
if(!(Test-Path 'C:\Program Files\PuTTY\plink.exe') -and !(Test-Path '.\plink.exe')){
Write-Output 'No Putty utilities installed! Please install from https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html'
Write-Error "Please install plink.exe utility from putty or place the plink.exe file in the same directory"
exit "No Plink"
}
if($Credentials){
$Username = $Credentials.Username.replace("\","")
$Password = $Credentials.GetNetworkCredential().password
}
$Commands = @()
$Commands += $Command
}
Process{
$Commands += $_
}
End{
$Host | %{
$runcommand = "plink -pw $Password -ssh $Username@$_ $Options "
if(!$NoSudo){
$runcommand += "echo -e `'$Password\n`' | sudo -S "
}
Write-Host -foregroundcolor Green "Executing on $_ as $Username"
$Commands | %{
$runthis = $runcommand + $_
. $runthis
}
}
}
}
Set-Alias -Name 'IRSC' -Value 'Invoke-RemoteShellCommand'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment