bash - 使用WinSCP在freeSSHD服务器上运行命令失败并显示 “Your shell is probably incompatible with the application (BASH is recommended)”

标签 bash powershell ssh winscp winscp-net

我正在使用下面的脚本来针对本地文件验证远程文件的校验和。我在计算机上安装的服务器是freeSSHd。

当我尝试使用PowerShell ISE执行以下脚本时,出现一条错误消息:

Your shell is probably incompatible with the application (BASH is recommended)



Screenshot of error

我已经在FreeSSHd Server用户属性中授予了Shell访问权限:
FreeSSHd settings

脚本:
param (
    # Use Generate URL function to obtain a value for -sessionUrl parameter.
    $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xx-xx-xx@example.com/",
    [Parameter(Mandatory = $True)]
    $localPath,
    [Parameter(Mandatory = $True)]
    $remotePath,
    [Switch]
    $pause = $False
)

try
{
    Write-Host $localPath -foregroundcolor Gray


# Calculate local file checksum
$localChecksum = ((CertUtil -hashfile $localPath SHA1)[1] -replace " ","")

# Write-Host "Local Checksum:"
Write-Host $localChecksum

# Load WinSCP .NET assembly
#Add-Type -Path (Join-Path $PSScriptRoot "WinSCPnet.dll")
[Reflection.Assembly]::LoadFrom("\\c:\Program Files (x86)\WinSCP\WinSCPnet.dll") | Out-Null

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.ParseUrl($sessionUrl)

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    Write-Host $remotePath -foregroundcolor Gray

    # Calculate remote file checksum
    $sha1Command = "bash sha1sum -b $remotePath | awk '{print `$1}'"

    $result = $session.ExecuteCommand($sha1Command)
    $result.Check()
    $remoteChecksum = $result.Output;
    #$remoteChecksum =
        [System.BitConverter]::ToString($session.CalculateFileChecksum("sha-1", $remotePath))

    # Write-Host "Remote Checksum:"
    Write-Host $remoteChecksum
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}

# Compare cheksums
if ($localChecksum -eq $remoteChecksum)
{
    Write-Host
    Write-Host "Match" -foregroundcolor "green"
    $result = 0
}
else
{
    Write-Host
    Write-Host "Does NOT match" -foregroundcolor "red"
    $result = 1
}
}
catch [Exception]
{
Write-Host $_.Exception.Message
$result = 1
}

# Pause if -pause switch was used
if ($pause)
{
    Write-Host "Press any key to exit..."
    [System.Console]::ReadKey() | Out-Null
}

exit $result

最佳答案

FreeSSHd服务器不支持任何“bash”。它的“ shell ”是Windows cmd.exe

您的代码无法正常工作。 Windows cmd.exe与WinSCP不兼容。

此外,FreeSSHd相当容易出错,请不要使用它。

您应该使用其他Windows SSH服务器。

  • 您可以使用Windows build of OpenSSH。它将允许您在服务器上执行PowerShell脚本以计算校验和。

    如果安装Windows Subsystem for Linux,甚至可以获取sha1sum(但我不确定)。
  • 如果需要在Windows上模拟* nix环境,则可以使用Cygwin
  • 您可以免费使用Bitvise SSH Server个人使用。它的SFTP服务器自己支持校验和计算,因此您可以直接使用WinSCP Session.CalculateFileChecksum method

  • 还有很多其他选择。

    关于bash - 使用WinSCP在freeSSHD服务器上运行命令失败并显示 “Your shell is probably incompatible with the application (BASH is recommended)”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47262972/

    相关文章:

    bash - 如何获取wget下载的文件的文件名

    powershell - 我可以在 PowerShell 脚本中禁用选择模式吗?

    linux - 无法使用 jenkins 在 ubuntu 上运行远程脚本

    linux - 通过 ssh 运行持久化进程

    java - 从 Java 调用带有重定向的 bash 脚本

    python - rc.local shell 脚本启动时缺少文件

    bash - 适用于 awk v4.0.2 但不适用于 >= 4.2.1 的 awk 表达式

    powershell - 在 PowerShell 中将多行文本粘贴为变量不起作用

    html - 转换为 HTML Azure Powershell "Get-VM"

    shell - 使用Plink,如何自动输入密码进入Ubuntu ssh?