【问题标题】:Compare a file size between 2 servers (Unix & Windows)比较 2 个服务器(Unix 和 Windows)之间的文件大小
【发布时间】:2019-05-28 21:11:29
【问题描述】:

我需要编写一个 Windows 批处理文件来解决以下问题。

我需要确保文件已从其原始服务器 (Unix) 完全传输到目标服务器 (Windows)。

我正在尝试比较两台服务器之间的文件大小,解决方案需要在 Windows 批处理文件中(我无法在 DOS 批处理文件中使用 SSH 连接到远程 Unix 服务器)

【问题讨论】:

  • 如何传输文件? ftp?
  • 我不确定,因为我只能访问 Unix 和 Windows 服务器;由第三方工具在不同服务器上完成的传输 (CFT...)
  • 如果文件是文本,则传输工具可能会将行尾从 UNIX 转换为 DOS/Windows。文件大小会有所不同。你在 Windows 上使用什么 SSH?
  • 文件格式和类型每次都会改变。我在文件完全传输后测试了文件大小,并且它在 Unix 和 Windows 服务器上的八位字节大小相同。我正在考虑使用 DOS 命令 SSH。这可能吗?
  • 好的,看来文件是以二进制模式传输的(FTP 术语)。你在 DOS/Windows 上找到ssh 命令了吗?您可能需要 Cygwin 或 PuTTY plink。

标签: unix batch-file ssh


【解决方案1】:

鉴于您可以使用 SSH,我假设您也有 SFTP 访问权限。

您可以通过WinSCP .NET assembly 使用以下 PowerShell 脚本。

param (
    $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxx...=@example.com/",
    $localPath = "C:\path\file.dat",
    $remotePath = "/path/file.dat"
)

$localSize = (Get-Item $localPath).Length
Write-Host "$localPath has $localSize bytes"

# Load WinSCP .NET assembly
Add-Type -Path (Join-Path $PSScriptRoot "WinSCPnet.dll")

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

$session = New-Object WinSCP.Session

# Connect
$session.Open($sessionOptions)

$remoteSize = $session.GetFileInfo($remotePath).Length
$session.Dispose()

Write-Host "$remotePath has $remoteSize bytes"

# Compare cheksums
if ($localSize -eq $remoteSize)
{
    Write-Host "Match"
    $result = 0
}
else
{
    Write-Host "Does NOT match"
    $result = 1
}

exit $result

或者更好的是,比较文件校验和,而不仅仅是大小,如果您的服务器支持,请参阅: Verify checksum of a remote file against a local file over SFTP/FTP protocol


(我是 WinSCP 的作者)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    相关资源
    最近更新 更多