Powershell使用SSH


在編寫Powershell腳本時,有時會需要和Linux機器進行交互。所以這時就需要在Powershell中使用SSH。

本文將介紹如何在Powershell中安裝SSH模塊,以及如何使用ssh命令。

一 Posh-SSH模塊

1 安裝

在Windows中點擊搜索,powershell_ise

在PowerShell_ise的環境中運行

PS C:\windows\system32> find-module *ssh*

Version    Name                                Repository           Description                                                                             
-------    ----                                ----------           -----------                                                                             
2.0.2      Posh-SSH                            PSGallery            Provide SSH and SCP functionality for executing commands against remote hosts.          
1.0.0      SSH                                 PSGallery            Provides a PowerShell-based SSH client based on SSH.net  http://sshnet.codeplex.com/    
1.9        SSHSessions                         PSGallery            Svendsen Tech's SSH-Sessions module provides SSH session creation, management and int...
1.1.3      PowerSSH                            PSGallery            This module detects the first use of an SSH command, automatically runs the SSH agent...
1.0        cEPRSSharepoint                     PSGallery            DSCModule helps in installing & configuring the sharepoint site, Farm etc.,             
1.0.4      PSShortcut                          PSGallery            This module eases working with Windows shortcuts (LNK and URL) files.                   

如果沒有find-module的命令,需要安裝PackageManagement:

https://www.microsoft.com/en-us/download/details.aspx?id=51451

本文采用Posh-SSH的模塊:

Install-Module Posh-SSH

可以通過下面的命令,查看安裝的模塊包含什么命令:

PS C:\Users\hengwei> get-command -Module posh-ssh 
 
CommandType Name ModuleName 
----------- ---- ---------- 
Function Get-PoshSSHModVersion posh-ssh 
Function Get-SFTPChildItem posh-ssh 
......

2 在PowerShell中使用SSH

可以用下面的腳本進行測試:

$username = "hengwei" 
$password = "xxxx" 
$secure = $password | ConvertTo-SecureString -AsPlainText -Force 
$cred = New-Object System.Management.Automation.PSCredential($username,$secure) 
 
New-SSHSession -ComputerName 40.125.173.212 -Credential $cred -AcceptKey 
 
Invoke-SSHCommand -SessionId 0 -Command "cat /etc/redhat-release" 
Invoke-SSHCommand -SessionId 0 -Command "df -k" 

 

Host : 40.125.173.212 
Output : {CentOS Linux release 7.3.1611 (Core) } 
ExitStatus : 0 
 
Host : 40.125.173.212 
Output : {Filesystem 1K-blocks Used Available Use% Mounted on, /dev/sda2 30929148 1200340 29728808 4% /, devtmpfs 847688 0 847688 0% /dev, tmp 
fs 858264 0 858264 0% /dev/shm...} 
ExitStatus : 0 

 

二 SSHSessions模塊

1 安裝

和Posh-SSH類似,安裝采用install-module的方式:

Install-module SSHSessions

其命令比Posh-SSH要少,但都是SSH相關的:

PS C:\Users\hengwei> get-command -Module sshsessions 
 
CommandType Name ModuleName 
----------- ---- ---------- 
Function ConvertFrom-SecureToPlain SSHSessions 
Function Enter-SshSession SSHSessions 
Function Get-SshSession SSHSessions 
Function Invoke-SshCommand SSHSessions 
Function New-SshSession SSHSessions 
Function Remove-SshSession SSHSessions 

 

2 在PowerShell中使用SSH

New-SshSession -ComputerName 40.125.173.212 -Username hengwei -Password xxxx
 
PS C:\Users\hengwei> Get-SshSession 
 
ComputerName Connected 
------------ --------- 
40.125.173.212 

SSHSessions中有一個enter-sshsession的命令,可以直接進入交互式模式,進行一些交互的操作:

PS C:\Users\hengwei> Enter-SshSession -ComputerName 40.125.173.212 
[40.125.173.212]: /home/hengwei # : pwd 
/home/hengwei 
[40.125.173.212]: /home/hengwei # : mkdir test 
[40.125.173.212]: /home/hengwei # : ls 
test 
[40.125.173.212]: /home/hengwei # : ls -l 
total 0 
drwxrwxr-x. 2 hengwei hengwei 6 Nov 20 07:29 test 
[40.125.173.212]: /home/hengwei # : 

也可以使用invoke-sshcommand的模式實現命令

PS C:\Users\hengwei> Invoke-SshCommand -ComputerName 40.125.173.212 -Command "sudo cat /etc/fstab" 
[40.125.173.212] 
# UUID=b7559ac5-b3a4-4b00-b98a-a2a2611806d0 / xfs defaults 0 0 
UUID=b98659b2-5f8c-493e-9304-658905ef1391 /boot xfs defaults 0 0 

 

大家還可以安裝一下其他的ssh模塊,實現在Powershell中的ssh功能。

三 刪除模塊

以Posh-SSH模塊為例,刪除命令:

PS C:\windows\system32> remove-module -name posh-ssh -Force -Verbose -Debug 
VERBOSE: Removing the imported "Get-PoshSSHModVersion" function. 
VERBOSE: Removing the imported "Get-SFTPChildItem" function. 
VERBOSE: Removing the imported "Get-SFTPContent" function. 
......

同時,刪除Posh-SSH的目錄:

C:\Program Files\WindowsPowerShell\Modules\Posh-SSH

這樣就徹底刪除了Posh-SSH模塊。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM