首先,確保您的遠程服務器完全支持校驗和計算。許多人不知道。我相信甚至沒有標準的FTP命令來計算遠程文件的校驗和。有很多建議,並有許多專有解決方案。
最新的建議是:
https://tools.ietf.org/html/draft-bryan-ftpext-hash-02
所以,即使你的服務器支持校驗和計算,你必須找到一個支持相同的命令的客戶端。
一些可以用於計算校驗和的命令是:XSHA1
,XSHA256
,XSHA512
,XMD5
,MD5
,XCRC
和HASH
。
你可以用WinSCP測試。 WinSCP支持前面提到的所有命令。測試其checksum calculation function或checksum
scripting command。如果他們工作,enable logging並檢查,什麼命令和WinSCP對您的服務器使用什麼語法。
無論是ftp
(無論是Windows還是* nix的版本),也不是lftp
支持校驗和計算,僅讓下載的文件的自動驗證。
我什至不知道任何其他客戶端可以自動驗證下載的文件。
您可以在一些功能豐富的客戶端的幫助下明確編寫腳本。
我在OP指定他/她在Linux上之前已經寫了這個答案。我保留Windows解決方案以防別人幫助其他人。
在Windows上,您可以使用PowerShell using WinSCP .NET assembly來編寫腳本。
param (
$sessionUrl = "ftp://username:[email protected]/",
[Parameter(Mandatory)]
$localPath,
[Parameter(Mandatory)]
$remotePath,
[Switch]
$pause = $False
)
try
{
# 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
try
{
# Connect
$session.Open($sessionOptions)
Write-Host "Downloading $remotePath to $localPath..."
$session.GetFiles($remotePath, $localPath).Check();
# Calculate remote file checksum
$buf = $session.CalculateFileChecksum("sha-1", $remotePath)
$remoteChecksum = [BitConverter]::ToString($buf)
Write-Host "Remote file checksum: $remoteChecksum"
# Calculate local file checksum
$sha1 = [System.Security.Cryptography.SHA1]::Create()
$localStream = [System.IO.File]::OpenRead($localPath)
$localChecksum = [BitConverter]::ToString($sha1.ComputeHash($localStream))
Write-Host "Downloaded file checksum: $localChecksum"
# Compare cheksums
if ($localChecksum -eq $remoteChecksum)
{
Write-Host "Match, deleting remote file"
$session.RemoveFiles($remotePath).Check();
$result = 0
}
else
{
Write-Host "Does NOT match"
$result = 1
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
}
catch [Exception]
{
Write-Host "Error: $($_.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
您可以運行它像:
powershell -file checksum.ps1 -remotePath ./file.dat -localPath C:\path\file.dat
這是部分基於例子的WinSCP爲Verifying checksum of a remote file against a local file over SFTP/FTP protocol。
(我在WinSCP賦予的作者)
什麼平臺,你有嗎? –
我正在客戶端和服務器上運行Linux – user764186
哦,好吧,我已經發布了Windows解決方案。我會保留它以防別人幫忙。希望你能得到Linux解決方案的答案。 –