有時會需要和Linux機器進行交互。所以這時就需要在Powershell中使用SSH。
0x01 查找Powershell中的SSH功能模塊
如圖,顯示沒有find-module的命令,需要安裝PackageManagement:
下載地址:https://www.microsoft.com/en-us/download/details.aspx?id=51451
0x02 安裝、使用SSH模塊
A) Posh-SSH
Install-Module -Name Posh-SSH 安裝Posh-SSH
可以通過下面的命令,查看安裝的模塊包含什么命令:
get-command -Module posh-ssh
在PowerShell中使用SSH
添加SSH會話命令:
New-SSHSession -ComputerName "192.168.190.148" -Credential (Get-Credential root)
獲取SSH會話命令:
Get-SSHSession
刪除SSH會話命令:
Remove-SSHSession -Index 0 -Verbose
執行SSH命令:
Invoke-SSHCommand -Index 0 -Command“uname -a”
添加SFTP會話命令:
New-SFTPSession -ComputerName 192.168.190.148 -Credential(Get-Credential root)
獲取SFTP會話命令:
GET-SFTPSession
獲取當前目錄命令:
Get-SFTPCurrentDirectory -Index 0
切換到其他目錄命令:
Set-SFTPDirectoryPath -Index 0 -Path / usr / bin
也可以一起寫到腳本執行,比如我執行uname -a和df -k兩個命令
$username = "root" $password = "123456" $secure = $password | ConvertTo-SecureString -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential($username,$secure) New-SSHSession -ComputerName 192.168.190.148 -Credential $cred -AcceptKey Invoke-SSHCommand -SessionId 0 -Command "uname -a" Invoke-SSHCommand -SessionId 0 -Command "df -k"
執行腳本 注意替換腳本里的主機地址、賬號、密碼。
B)SSHSessions
Install-module -Name SSHSessions 安裝SSHSessions
get-command -Module sshsessions 查看命令
建立一個新的ssh會話 New-SshSession -ComputerName 192.168.190.148 -Username root -Password 123456 Enter-SshSession -ComputerName 192.168.190.148 進入交互模式
也可以使用invoke-sshcommand的模式實現命令 Invoke-SshCommand -ComputerName 192.168.190.148 -Command "ifconfig"
大家還可以安裝一下其他的ssh模塊,實現在Powershell中的ssh功能。
0x03 刪除SSH模塊
例如刪除posh-ssh模塊
remove-module -name posh-ssh -Force -Verbose -Debug
同樣還需要刪除模塊的目錄
C:\Program Files\WindowsPowerShell\Modules 目錄下為powershell安裝的模塊目錄
刪除即可
其他刪除模塊的方法也是一樣的。