PowerShellからSSHとSCPに簡単にアクセスできるPosh-SSHモジュール

PowerShellには、PowerShellでSSH、SFTP、SCPプロトコルのサポートを実装するPosh-SSHモジュールがあります。 インストール方法、および作業に関する基本的な注意事項について説明します。 実際、これは以下の英語の記事からの抜粋です。



特定のイベントでは、スイッチのネットワークポートをリセットする必要がありました。 スイッチにはciscoコマンドラインインターフェイスがあります。 コマンドラインからputtyを使用する前に、powershellから直接sshで作業するためのモジュールがあるかどうかを確認することにしました。 検索はgithubでPosh-SSHモジュールを提供しました



このモジュールでは次のことができます。





SSH、キーによる認証、ログイン\パスワード、キーボード入力がサポートされています。 さまざまな暗号化アルゴリズムがサポートされ、プロキシがサポートされています



最小要件はPowerShell 3.0および.NET 4.0です。 公式ページのモジュールの説明



モジュールのインストール



管理コンソールからインストールする最も簡単な方法は、次のコマンドを実行することです。



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





PowerShell 5を使用している場合:



 Find-Module Posh-SSH | Install-Module
      
      





モジュール内のコマンドは次のように表示できます。



 Get-Command -Module Posh-SSH
      
      





SSHの使用方法



1.最初に、SSHセッションを作成します。



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





最初の接続で、モジュールは信頼できるホストのリストにリモートホストを追加するかどうかを尋ねます。 コンソールからNew-SSHSessionを1回起動してYを押すことができます その後、問題なく接続します。



信頼できるホスト
コマンドレットは、信頼できるホストを表示および削除するために使用されます。



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




2.シェルを作成します。



 $SSH = $SSHSession | New-SSHShellStream
      
      





これでコマンドを送信して答えを読むことができます:



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





3.作業の完了:



 $sshSession | Remove-SSHSession
      
      





Get-SSHSessionコマンドを使用してセッションを表示できます。



以下は作業の例です。





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 }
      
      





例からわかるように、コンソール出力を取得し、必要に応じて解析できます

sendメソッド2-最初の印刷をコンソールに書き込み、WriteLineで2番目の印刷を実行し、Enterキーを押します。



ファイル転送SCP



ここではまだ簡単です。 公式ページから例を挙げます。 ファイルのアップロード:



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





ファイルのダウンロード:



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





» モジュールの作成者からの公式ページ



便利なリンク: 1つと 2つ



All Articles