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模块。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· Open-Sora 2.0 重磅开源!