📜 ⬆️ ⬇️

Posh-SSH module for easy access to SSH and SCP from PowerShell

For powershell, there is a Posh-SSH module that implements support for SSH, SFTP, and SCP protocols in PowerShell. It describes how to install, and basic notes on the work. In fact, it is a squeeze of the English article below.

on a specific event, it was necessary to reset the network port on the switch. The switch has a command line interface cisco. Before using putty from the command line, it was decided to see if there are modules for working on ssh directly from powershell. Searches gave the Posh-SSH module on github .

The module allows:
')

For SSH, authentication by key, login / password, keyboard input is supported. Different encryption algorithms are supported, proxies are supported

Minimum requirements are PowerShell 3.0 and .NET 4.0. Module description on the official page .

Module installation


The easiest way to install from the admin console is to run the command:

iex (New-Object Net.WebClient).DownloadString("https://gist.github.com/darkoperator/6152630/raw/c67de4f7cd780ba367cccbc2593f38d18ce6df89/instposhsshdev") 

If you have PowerShell 5:

 Find-Module Posh-SSH | Install-Module 

You can view the commands in the module as follows:

 Get-Command -Module Posh-SSH 

How to work with SSH


1. First, create an SSH session:

 Import-Module Posh-SSH $SSHSession = New-SSHSession -ComputerName 192.168.1.1 -Credential $(Get-Credential) -Verbose 

At the first connection, the module will ask whether the remote host should be added to the trusted list. You can run New-SSHSession from the console once and press Y. In the future will connect without question.

Trusted Hosts
Use cmdlets to view and delete trusted hosts.

  • Get-sshtrustedhost
  • Get-SSHSession
  • Remove-SSHSession


2. Create a shell:

 $SSH = $SSHSession | New-SSHShellStream 

Everything, now you can send commands and read the answer:

 #   $SSH.WriteLine( "enable" ) #   $SSH.read() 

3. Completion of work:

 $sshSession | Remove-SSHSession 

You can view sessions using the Get-SSHSession command .

Below is an example of work:


An example of working with a switch over SSH
 $SwitchIP = '10.10.3.2' $SwitchPort = 4 $Cred = Get-Credential admin $SSHSession = New-SSHSession -ComputerName $SwitchIP -Credential $Cred -Verbose if ($($sshSession.Connected) -eq $true) { Write-Host "SSH session opened" -ForegroundColor Green Write-Host " " Write-Host " open shell" -ForegroundColor Green ###   ,    $ssh = $sshSession | New-SSHShellStream Start-Sleep -Seconds 1 #   $ssh.read() Start-Sleep -Seconds 1 $ssh.WriteLine( "enable" ) $ssh.read() Write-Host "    " -ForegroundColor Green Start-Sleep -Seconds 1 $ssh.WriteLine( "password" ) $ssh.read() Write-Host "  " -ForegroundColor Green Start-Sleep -Seconds 1 $ssh.WriteLine( "configure" ) $ssh.read() Write-Host "    " -ForegroundColor Green Start-Sleep -Seconds 1 $ssh.WriteLine( "interface gigabitEthernet 1/0/$SwitchPort" ) $ssh.read() Write-Host "     interface gigabitEthernet 1/0/$SwitchPort" -ForegroundColor Green Start-Sleep -Seconds 1 $ssh.WriteLine( "shutdown" ) $ssh.read() Write-Host "  " -ForegroundColor Green Start-Sleep -Seconds 3 $ssh.WriteLine( "no shutdown" ) $ssh.read() Write-Host "  " -ForegroundColor Green Write-Host " , " -ForegroundColor Green } else { Write-Host "SSH session cannot be established" -ForegroundColor Red Write-Host "script terminate" -ForegroundColor Red exit } if ( $($sshSession | Remove-SSHSession) -eq $true) { Write-Host "SSH session closed" -ForegroundColor Green } else{ Write-Host "SSH session NOT closed" -ForegroundColor Red Write-Host "please check manual" -ForegroundColor Red Get-SSHSession } 

As you can see from the example, you can get the console output back and parse if necessary.
sending method 2 - Write and WriteLine, the first type in the console, the second type, respectively, and press Enter.

SCP file transfer


It's still easier. I give an example from the official page. Upload file:

 Set-SCPFile -LocalFile .\Downloads\VMware-PowerCLI-5.5.0-1671586.exe -RemoteFile "/tmp/powercliinstaller.exe" -ComputerName 192.168.10.3 -Credential (Get-Credential root) 

File Download:

 Get-SCPFile -LocalFile .\Downloads\VMware-PowerCLI.exe -RemoteFile "/tmp/powercliinstaller.exe" -ComputerName 192.168.10.3 -Credential (Get-Credential root) 

» Official page from the creator of the module

Useful links: one and two .

Source: https://habr.com/ru/post/314990/


All Articles