windows - Powershell 脚本 : SSH into Server

标签 windows powershell ssh

我想知道是否可以编写一个 powershell 脚本,通过 ssh 连接到服务器,然后在该服务器上执行某些操作。

感谢您的帮助。

最佳答案

网络上有很多关于这个主题的文章,而且由于 PowerShell Core 现在是开源的并且可以安装在 Windows/Linux/OSX 上,因此 PowerShell 的 SSH 已经存在了一段时间。

示例:

Using SSH to Access Linux Servers in PowerShell

Using SSH with PowerShell

Managing Windows Powershell from Linux terminal

MS PowerShellGallery 上有几个模块专门用于此用例。

Find-Module -Name '*ssh*'

Version              Name                                Repository           Description                                                                                  
-------              ----                                ----------           -----------                                                                                  
2.0.2                Posh-SSH                            PSGallery            Provide SSH and SCP functionality for executing commands against remote hosts.               
2.1.3                SSHSessions                         PSGallery            Svendsen Tech's SSH-Sessions module provides SSH session creation, management and interact...
0.0.2.0              OpenSSHUtils                        PSGallery            Utilities and functions for configuring OpenSSH on Windows.                                  
1.0.0                SSH                                 PSGallery            Provides a PowerShell-based SSH client based on SSH.net  http://sshnet.codeplex.com/         
1.1.3                PowerSSH                            PSGallery            This module detects the first use of an SSH command, automatically runs the SSH agent, kee...
0.9.4                WinSSH                              PSGallery            Install OpenSSH-Win64, optionally install ssh-agent and sshd Services. Also includes funct...
0.0.30               PSSharedGoods                       PSGallery            Module covering functions that are shared within multiple projects                           
1.0.1                ssh-wrapper                         PSGallery            Exposes ssh from WSL by wrapping: bash -c "ssh $args". Requires Windows Subsystem for Linu...
1.0.4                PSShortcut                          PSGallery            This module eases working with Windows shortcuts (LNK and URL) files.                        
1.0                  cEPRSSharepoint                     PSGallery            DSCModule helps in installing & configuring the sharepoint site, Farm etc.,                  
2.0.1.8              SkypeForBusinessHybridHealth        PSGallery            Uses on-premises modules such as Skype For Business and SkypeOnlineConnector to validate b...
0.3.1                posh-sshell                         PSGallery            Provides integration with ssh-agent and pageant from within Powershell                       
1.1.4                PowerSSH-Legacy                     PSGallery            This module detects the first use of an SSH command, automatically runs the SSH agent, kee...

SSH From Windows Server to Linux Server - Invoke-SSHCommand

Invoke-SSHCommand $IndexID.SessionID -command "curl -v telnet://WindowsServerA:4750& sleep 2; kill $!"

# Results

Host       : LinuxServerA
Output     : {}
ExitStatus : 0



Invoke-SSHCommand $IndexID.SessionID -command "curl -v telnet://LinuxServerB:4750& sleep 2; kill $!"

# Results
Host       : LinuxServerA
Output     : {}
ExitStatus : 0



Invoke-SSHCommand $IndexID.SessionID -command "curl -v telnet://WindowsServerA:4750 2>&1 & sleep 2; kill $!"

# Results
Host       : LinuxServerA
Output     : {* About to connect() to WindowsServerA port 4750, *   Trying 10.10.10.10... connected, * Connected to
             WindowsServerA (10.10.10.10) port 4750}
ExitStatus : 0



Invoke-SSHCommand $IndexID.SessionID -command "curl -v telnet://LinuxServerB:4750 2>&1 & sleep 2; kill $!"

# Results
Host       : LinuxServerA
Output     : {* About to connect() to LinuxServerB port 4750, *   Trying 10.10.10.11... connected, * Connected to
             LinuxServerB (10.10.10.11) port 4750}
ExitStatus : 0

关于windows - Powershell 脚本 : SSH into Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53034988/

相关文章:

windows - 安装驱动器时执行批处理脚本

c++ - 为什么在使用 ReadFile() 函数时会发生访问冲突?

powershell - 如何使用PowerShell在AD LDS中删除用户?

c - 未定义引用?但是我已经实现了这个功能

windows - 警告 : cannot find entry symbol nable-stdcall-fixup; defaulting

sql - 是否可以使用 "Invoke-SqlCmd"作为 Visual Studio Online 构建步骤的一部分?

c# - 用 C# 编写批处理脚本的好方法是什么?

linux - 制作脚本回答 svn+ssh passkey

mysql - 错误 1193 (HY000) : Unknown system variable 'userstat'

jenkins - 如何为 Jenkins 设置 ssh key 以通过 ssh 发布