【问题标题】:Powershell Script: SSH into ServerPowershell 脚本:SSH 进入服务器
【发布时间】:2019-04-01 18:20:47
【问题描述】:

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

感谢您的帮助。

【问题讨论】:

  • 这是一个非常抽象的问题。答案是您可以通过 ssh 拨打电话,并通过 powershell/batch 文件运行(非交互式)命令,但您需要第三方 SSH 客户端 AFAIK
  • Concret 我有一个覆盆子,它用于播放音乐。安装了两个软件。 Apple (Airplay) 和 on 的一个仅用于模拟 Spotify 连接设备。我需要一个通过 ssh 连接到树莓派的脚本。它应该使用 alsamixer 增加音量。
  • 当然,你需要一个可以在命令行上使用的 ssh 客户端,有些是可用的,最突出的是 openSSH 客户端,还有 PuTTY 自带的命令行可执行文件。您可以从脚本中调用该可执行文件并指定一个选项,以允许指定要在远程站点上执行的脚本。但是,您可以在服务器端执行 powershell 脚本,因为这很可能是 Linux 系统,所以 unixoid。 MS-Windows 系统上的 ssh 服务器是完全陌生的,您只能在该系统上使用 powershell,其他操作系统提供更好的 shell 解决方案。

标签: windows powershell ssh


【解决方案1】:

我正要建议你使用 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秒谷歌remote command ssh powershell的结果,所以我不知道它是否对你有用,或者它是否需要遥控器上的powershell(你可以得到它适用于linux)

【讨论】:

    【解决方案2】:

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

    示例:

    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
    

    【讨论】:

      猜你喜欢
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 2018-12-09
      • 2015-07-16
      • 1970-01-01
      • 2018-09-13
      • 2018-12-28
      相关资源
      最近更新 更多