当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell:使用 SSH 连接到远程主机 (Posh-SSH)

作者:精品下载站 日期:2024-12-14 07:20:12 浏览:16 分类:玩电脑

PowerShell:使用 SSH 连接到远程主机 (Posh-SSH)


Posh-SSH 是适用于 Windows 10 的 PowerShell 模块。该模块使您能够建立与远程计算机的 SSH 连接。作为先决条件,必须安装 Windows Management Framework 5。在本文中,我将逐步展示如何安装 Posh-SSH 并与运行 Linux 的计算机建立远程连接。

在 Windows 中,SSH 连接有些不寻常,因为 PowerShell 有其自己的功能,但 SSH 对于访问非 Windows 设备(例如交换机、路由器以及最后但并非最不重要的 Linux 计算机)是必不可少的。

安装

运行 Find-Module 以查找新的 PowerShell 模块 Posh-SSH。如果找不到,请从此源安装 .NET Framework 5.0:https://www.microsoft.com/en-us/download/details.aspx?id=50395


Find-Module Posh-SSH

[玩转系统] PowerShell:使用 SSH 连接到远程主机 (Posh-SSH)

通过运行一个简单的 One-Liner 来安装它。


Find-Module Posh-SSH | Install-Module

[玩转系统] PowerShell:使用 SSH 连接到远程主机 (Posh-SSH)

完成后,请随意探索新的 Posh-SSH 命令。对于下一步,我们只需要其中两个。


Get-Command -Module Posh-SSH

[玩转系统] PowerShell:使用 SSH 连接到远程主机 (Posh-SSH)

在 Windows PowerShell 上运行 SSH

现在使用 New-SSHSession 连接到远程主机。


New-SSHSession -ComputerName Server01

[玩转系统] PowerShell:使用 SSH 连接到远程主机 (Posh-SSH)

运行 Get-SSHSession 以显示所有当前连接。我的连接的会话 ID 为 0。我们在下一步中需要此会话 ID。


Get-SSHSession

[玩转系统] PowerShell:使用 SSH 连接到远程主机 (Posh-SSH)

通过运行 Invoke-SSHCommand 和会话 ID 输入远程命令。


Invoke-SSHCommand -SessionId 0 -Command cut -d: -f1 /etc/passwd

[玩转系统] PowerShell:使用 SSH 连接到远程主机 (Posh-SSH)

进一步的想法

如果您更喜欢 Windows PowerShell 内置功能“PowerShell Remoting”,请查看我的文章:PowerShell Remoting:如何连接到域和非域环境中的远程主机(受信任的主机)

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯