【问题标题】:ftp batch file scriptftp批处理文件脚本
【发布时间】:2017-03-28 12:23:51
【问题描述】:

希望有人可以指导/帮助我。

问题,我有 2 台服务器,其中一台运行 Ubuntu,它有一个网站供客户端登录和下载/查看报告。另一个是创建/存储报告的 windows server 2012 R2。我需要将文件从 Windows 移动到 Ubuntu 服务器,以便客户端可以查看。数据量很大,目前为 7gb,并且以每年 3gb 的速度增长。

我需要一个批处理文件来使用 ftp 进行连接,然后将该文件夹复制到本地文件夹。但是它只需要复制那些修改过的文件。

我只写过一个批处理文件,我似乎找不到任何只复制修改文件的 ftp 批处理脚本。

您是我的最后手段,因为我似乎找不到知道批处理脚本的编码器(它是一种死亡的艺术)。我从来没有使用过powershell,所以不知道从哪里开始。

任何帮助或建议请告诉我。

谢谢 约翰

【问题讨论】:

  • 我认为 rsync 可能是你的朋友
  • 您好,感谢您的想法。我看了看,都需要我做一些我不熟悉的事情,所以会告诉你我的进展情况。
  • 我遇到了winscp程序。这应该安全管理转移,但仍然不确定脚本部分?

标签: windows powershell batch-file ftp


【解决方案1】:

您可以通过winscp 使用 PowerShell 来完成此操作。例子:

try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "example.com"
    UserName = "user"
    Password = "mypassword"
    SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
}

$session = New-Object WinSCP.Session

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

    # Upload files
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary

    $transferResult = $session.PutFiles("d:\toupload\*", "/home/user/", $False, $transferOptions)

    # Throw on any error
    $transferResult.Check()

    # Print results
    foreach ($transfer in $transferResult.Transfers)
    {
        Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
    }
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}


    exit 0
}
catch [Exception]
{
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1
}

【讨论】:

    【解决方案2】:

    这将是在 PowerShell 中执行此操作的一种方法。这需要 31 天之前的文件并上传。

    function FTP-Upload {
        [CmdletBinding()]
        param(
        [Parameter(Mandatory=$true)]
        [string]$Source_File,
        [Parameter(Mandatory=$true)]
        [string]$Target_File,
        [Parameter(Mandatory=$true)]
        [string]$Target_Server,
        [Parameter(Mandatory=$true)]
        [string]$Target_Username,
        [Parameter(Mandatory=$true)]
        [string]$Target_Password
        )
    
        $FTP = [System.Net.FTPWebRequest]::Create("ftp://$Target_Server/$Target_File")
        $FTP = [System.Net.FTPWebRequest]$FTP
        $FTP.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
        $FTP.Credentials = New-Object System.Net.NetworkCredential($Target_Username,$Target_Password)
        $FTP.UseBinary = $true
        $FTP.UsePassive = $true
        # read in the file to upload as a byte array
        $content = [System.IO.File]::ReadAllBytes($Source_File)
        $FTP.ContentLength = $content.Length
        # get the request stream, and write the bytes into it
        $rs = $FTP.GetRequestStream()
        $rs.Write($content, 0, $content.Length)
        # be sure to clean up after ourselves
        $rs.Close()
        $rs.Dispose()
    }
    
    $Upload_Server = "server.network.tld"
    $Upload_Location = "/data/"
    $Upload_Username = "ftpuser"
    $Upload_Password = "ftppassword"
    
    $Files_To_Upload = Get-ChildItem E:\Path\To\Files -Recurse | Where-Object {($_.CreationTime -le (Get-Date).AddDays(-31)) -and (!$_.PSIsContainer)}
    
    Foreach ($File in $Files_To_Upload) {
        FTP-Upload -Source_File $File.FullName -Target_File ($Upload_Location + $File.Name) -Target_Server $Upload_Server -Target_Username $Upload_Username -Target_Password $Upload_Password
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多