Module Posh-SSH is a simple access to SSH and SCP from PowerShell

for powershell is a module Posh-SSH implements the Protocol support SSH, SFTP, SCP in PowerShell. It describes how to install, and base notes work. In fact it is the distillation of English articles is shown below.

for a specific event needed to reset the network port on the switch. The switch has a command line interface cisco. Before you can use command-line putty, it was decided to see whether there are modules for ssh directly from powershell. The search yielded a module Posh-SSH on github.

The module allows you to:

the
    the
  • to establish SSH session and SFTP on creditall or using OpenSSH key
  • the
  • to connect via a SOCKS and HTTP proxy for both SSH and SFTP sessions
  • the
  • execute commands one by one sending them to SSH
  • the
  • to upload and download files using SCP and SFTP protocols

For SSH supported authentication key, login / password input from the keyboard. Supports different encryption algorithms are supported proxies

Minimum requirements — PowerShell 3.0 .NET 4.0. the module Description on the official page.

the

Installing the module


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

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

If you have PowerShell 5:

the
Find-Module Posh-SSH | Install-Module

View the commands in a module, you can:

the
Get-Command-Module Posh-SSH

the

How to work with SSH


1. First create an SSH session:

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

When you first connect the module asks to add the remote host to the trusted list. Can be done once the launch New-SSHSession from the console and click Y. In the next to connect.

Trusted hosts
view and delete trusted hosts, use the cmdlets

the
    the
  • Get-SSHTrustedHost
  • the
  • Get-SSHSession
  • the
  • Remove-SSHSession


2. Create a shell:

the
$SSH = $SSHSession | New-SSHShellStream

It is now possible to send commands and read response:

the
# sends a command to the
$SSH.WriteLine( "enable" )

# read response
$SSH.read()

3. shutdown:

the
$sshSession | Remove-SSHSession

To view the session with the command Get-SSHSession.

Below is example of:

the
    the
  • connect via SSH
  • the
  • go to enable mode
  • the
  • go to the configuration mode interface
  • the
  • will resetin interface

Example of the switch via 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
### session opened successfully, start the reset port
$ssh = $sshSession | New-SSHShellStream
Start-Sleep -Seconds 1

# resetip interface
$ssh.read()

Start-Sleep -Seconds 1
$ssh.WriteLine( "enable" )
$ssh.read()
Write-Host "go to privileged mode" -ForegroundColor Green

Start-Sleep -Seconds 1
$ssh.WriteLine( "password" )
$ssh.read()
Write-Host "enter password" -ForegroundColor Green

Start-Sleep -Seconds 1
$ssh.WriteLine( "configure" )
$ssh.read()
Write-Host "go to configuration mode" -ForegroundColor Green

Start-Sleep -Seconds 1
$ssh.WriteLine( "interface gigabitEthernet 1/0/$SwitchPort" )
$ssh.read()
Write-Host "go to interface configuration interface gigabitEthernet 1/0/$SwitchPort" -ForegroundColor Green

Start-Sleep -Seconds 1
$ssh.WriteLine( "shutdown" )
$ssh.read()
Write-Host "disable the interface" -ForegroundColor Green

Start-Sleep -Seconds 3
$ssh.WriteLine( "no shutdown" )
$ssh.read()
Write-Host "include interface" -ForegroundColor Green

Write-Host "worked, completed" -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, you can get back the console output and parse if necessary

Transferring files using SCP


It's even easier. here is an example from the official page. Upload file:

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

Download file:

the
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: time and dva.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Briefly on how to make your Qt geoservice plugin

Database replication PostgreSQL-based SymmetricDS

Developing for Sailfish OS: notifications for example apps for taking notes