Powershell脚本:SSH到服务器

时间:2018-10-28 18:52:33

标签: windows powershell ssh

我想知道是否有可能编写通过ssh连接到服务器并在该服务器上执行某些操作的powershell脚本。

谢谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我正建议您使用PuTTY。实际上,您应该使用plink

如果查看SSH(Linux的命令),则可以通过以下方式execute remote commands

单个命令:

ssh $HOST ls

几个命令:

ssh $HOST ls; pwd; cat /path/to/remote/file

如果要在Windows计算机上执行此操作,则需要使用plink.exe或类似的工具。

显然还有some library for powershell to execute remote commands via ssh,但这是我花30秒钟时间去Google remote command ssh powershell的结果,所以我不知道它是否对您有用,或者是否需要遥控器上的Powershell (您可以在Linux上获得它)

答案 1 :(得分:0)

有关该主题的网络文章很多,而且由于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