代码之家  ›  专栏  ›  技术社区  ›  Ashar

无法在远程linux主机上使用powershell的sshArgs调用简单的ssh命令

  •  0
  • Ashar  · 技术社区  · 2 年前

    下面是我的github操作手册,它应该调用ssh命令 hostname 在远程linux主机上使用windowsrunner的powershell。

    name: Check Disk Space on Remote Hosts
    
    on:
      push:
        branches:
          - main
    
    jobs:
      check_disk_space:
        runs-on: windows-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
    
      - name: Run PowerShell script
        shell: PowerShell
        run: |
    
          $remoteHosts = "80.240.24.192,80.240.24.193".Split(",")
          $user="root"
          Set-Content -Path "C:\id_rsa" -Value '{{ secrets.myunixkey }}'
          icacls "C:\id_rsa" /inheritance:r /grant $env:USERNAME`:`R"
    
          foreach ($eachHost in $remoteHosts) {
            echo $eachHost
    
            $sshArgs = @(
              "-o", "StrictHostKeyChecking=no",
              "-o", "UserKnownHostsFile=/dev/null",
              "-i", "C:\id_rsa",
              "$user@$eachHost",
              "hostname"
            )
    
            Write-Output "$sshArgs"
            #$sshCommand = "ssh.exe $sshArgs"
            #$output = Invoke-Expression $sshCommand
            #Write-Output $output
          }
    

    我在执行时得到以下错误:

    Run #$remoteHosts = "".Split(",")
      
    At D:\a\_temp\01fae33c-005b-4e08-a955-7adb1085c97f.ps1:24 char:25
    +   Write-Output "$sshArgs"
    +                         ~
    The string is missing the terminator: ".
        + CategoryInfo          : ParserError: (:) [], ParseException
        + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
     
    Error: Process completed with exit code 1.
    

    注意:它打印 echo $eachHost 好吧,每个主持人一次。

    此处的测试用例: https://github.com/knowyrtech/invokebashfrompowershell/blob/main/.github/workflows/main.yml

    运行的错误“https://github.com/knowyrtech/invokebashfrompowershell/actions/runs/5875518287/job/15931923850“快照如下:

    enter image description here

    您可以随意在我的公共存储库中进行更改。

    你能建议我在PowerShell中正确构建ssh参数时缺少什么吗?

    0 回复  |  直到 2 年前
        1
  •  1
  •   stackprotector    2 年前

    此行使用了不正确的引号:

    icacls "C:\id_rsa" /inheritance:r /grant $env:USERNAME`:`R"
    

    因此,以下所有引用都将被解释为错误。将其更改为:

    icacls "C:\id_rsa" /inheritance:r /grant "$($env:USERNAME):R"