繁体   English   中英

Powershell 脚本:通过 SSH 连接到服务器

[英]Powershell Script: SSH into Server

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

感谢您的帮助。

我正要建议你使用 PuTTY。 事实上,你应该使用plink

如果您查看 SSH(Linux 的命令),您可以以这种方式执行远程命令

单个命令:

ssh $HOST ls

几个命令:

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

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

显然,powershell有一些库可以通过 ssh 执行远程命令,但这是我花了 30 秒来谷歌remote command ssh powershell所以我不知道它是否适合你,或者它是否需要 powershell 上的remotet(你可以在 linux 上得到它)

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

例子):

在 PowerShell 中使用 SSH 访问 Linux 服务器

在 PowerShell 中使用 SSH

从 Linux 终端管理 Windows Powershell

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 从 Windows 服务器到 Linux 服务器 - 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM