【问题标题】:How to ftp with a batch file?如何使用批处理文件进行ftp?
【发布时间】:2013-04-16 00:08:32
【问题描述】:

我想要一个批处理文件通过 ftp 传输到服务器,读出一个文本文件,然后断开连接。服务器需要用户名和密码。我试过了

@echo off
pause
@ftp example.com
username
password
pause

但它从未登录。我怎样才能让它工作?

【问题讨论】:

    标签: windows batch-file ftp


    【解决方案1】:

    0x90h 的回答帮了大忙……

    我将此文件保存为 u.ftp:

    open 10.155.8.215 
    user
    password
    lcd /D "G:\Subfolder\"
    cd  folder/
    binary
    mget file.csv
    disconnect
    quit
    

    然后我运行了这个命令:

    ftp -i -s:u.ftp
    

    成功了!!!

    非常感谢 :)

    【讨论】:

    • 如果您想查看上传或下载的进度,请在binary之后添加行hash
    • 如果他的回答对你有很大帮助,请将他的回答标记为已选!
    • 为了新手的利益,user 应该全部替换为your_username。即:这不包括单词用户是user your_username(这对于外行来说似乎是合乎逻辑的)。同样的密码。另外,如果您有服务器名称而不是 IP 地址,请不要将ftp:// 放在前面,因为这不起作用!
    【解决方案2】:

    使用 Windows FTP 客户端时,您需要使用-s:filename 选项来指定要运行的 FTP 客户端的脚本。该文档特别指出,您不应尝试使用 < 字符将输入通过管道传输到 FTP 客户端。

    脚本的执行将立即开始,因此它适用于用户名/密码。

    但是,此设置的安全性值得怀疑,因为您现在拥有 FTP 服务器的用户名和密码,任何决定查看您的批处理文件的人都可以看到。

    无论哪种方式,您都可以从批处理文件即时生成脚本文件,然后将其传递给 FTP 客户端,如下所示:

    @echo off
    
    REM Generate the script. Will overwrite any existing temp.txt
    echo open servername> temp.txt
    echo username>> temp.txt
    echo password>> temp.txt
    echo get %1>> temp.txt
    echo quit>> temp.txt
    
    REM Launch FTP and pass it the script
    ftp -s:temp.txt
    
    REM Clean up.
    del temp.txt
    

    servernameusernamepassword 替换为您的详细信息,批处理文件将生成脚本为 temp.txt 启动 ftp,并带有脚本,然后删除脚本。

    如果您总是得到相同的文件,您可以将%1 替换为文件名。如果不是,您只需启动批处理文件并提供要获取的文件名作为参数。

    【讨论】:

    • 我一直在阅读有关此-s 选项的信息,但我只收到“ftp: invalid option -- 's'”。这需要什么版本的ftp?我在 Windows 10 1903 上安装了 1.9.4。
    • 我刚刚意识到在我的机器上输入ftp 会运行cygwin 附带的ftp 程序,哈哈。很好,忘了我说了什么。
    【解决方案3】:

    这是一篇旧帖子,但另一种方法是使用命令选项:

    ftp -n -s:ftpcmd.txt
    

    -n 将禁止初始登录,然后文件内容将是:(将 127.0.0.1 替换为您的 FTP 站点 url)

    open 127.0.0.1
    user myFTPuser myftppassword
    other commands here...
    

    这避免了用户/密码在不同的行中

    【讨论】:

    • 这是有效的解决方案。我测试了它。遵循答案中提到的相同模式
    【解决方案4】:

    您需要将 ftp 命令写入文本文件,并将其作为 ftp 命令的参数,如下所示:

    ftp -s:filename
    

    更多信息在这里:http://www.nsftools.com/tips/MSFTP.htm

    我不确定它是否适用于用户名和密码提示。

    【讨论】:

      【解决方案5】:

      批处理文件的每一行都会被执行;但只有在上一行完成之后。在您的情况下,一旦它到达 ftp 行,ftp 程序就会启动并接管用户输入。当它关闭时,其余行将执行。这意味着用户名/密码永远不会发送到 FTP 程序,而是会在 ftp 程序关闭后自动提供给命令提示符。

      相反,您需要在 ftp 命令行上传递您需要的所有内容。比如:

      @echo off
      echo user MyUserName> ftpcmd.dat
      echo MyPassword>> ftpcmd.dat
      echo bin>> ftpcmd.dat
      echo put %1>> ftpcmd.dat
      echo quit>> ftpcmd.dat
      ftp -n -s:ftpcmd.dat SERVERNAME.COM
      del ftpcmd.dat
      

      【讨论】:

        【解决方案6】:

        使用

        ftp -s:FileName 
        

        Windows XP Professional Product Documentation中所述。

        您必须指定代替 FileName 的文件名必须包含您要发送到服务器的 FTP 命令。这些命令中有

        • 打开计算机 [端口] 以连接到 FTP 服务器,
        • 用户 UserName [Password] [Account] 向 FTP 服务器进行身份验证,
        • 获取 RemoteFile [LocalFile] 以检索文件,
        • quit 结束 FTP 会话并终止 ftp 程序。

        更多命令可以在Ftp subcommands下找到。

        【讨论】:

        • 提醒:如果您要使用user UserName [Password] [Account] 命令登录,请使用-n 参数运行ftp 以防止自动登录。所以,它变成了ftp -n -s:FileName
        【解决方案7】:

        这是我使用的。在我的情况下,某些 ftp 服务器(一个是纯 ftpd)将始终提示输入用户名,即使使用 -i 参数,并将“用户用户名”命令作为交互式密码。我的做法是输入几个NOOP(无操作)命令,直到ftp服务器超时,然后登录:

        open ftp.example.com 
        noop
        noop
        noop
        noop
        noop
        noop
        noop
        noop
        user username password
        ...
        quit
        

        【讨论】:

          【解决方案8】:

          您也可以使用 PowerShell;这就是我所做的。因为我需要根据模式下载文件,所以我动态创建了一个命令文件,然后让ftp 完成剩下的工作。

          我使用了基本的 PowerShell 命令。我不需要下载任何其他组件。我首先检查是否存在必要数量的文件。如果他们第二次使用 Mget 调用 FTP。 我从连接到 Windows XP 远程服务器的Windows Server 2008 运行它。

          function make_ftp_command_file($p_file_pattern,$mget_flag)
          {
              # This function dynamically prepares the FTP file.
              # The file needs to be prepared daily because the
              # pattern changes daily.
              # PowerShell default encoding is Unicode.
              # Unicode command files are not compatible with FTP so
              # we need to make sure we create an ASCII file.
          
              write-output "USER" | out-file -filepath C:\fc.txt -encoding ASCII
              write-output "ftpusername" | out-file -filepath C:\fc.txt -encoding ASCII -Append
              write-output "password" | out-file -filepath C:\fc.txt -encoding ASCII -Append
              write-output "ASCII" | out-file -filepath C:\fc.txt -encoding ASCII -Append
              If ($mget_flag -eq "Y")
              {
                  write-output "prompt" | out-file -filepath C:\fc.txt -encoding ASCII -Append
                  write-output "mget $p_file_pattern" | out-file -filepath C:\fc.txt -encoding ASCII -Append
              }
              else
              {
                  write-output "ls $p_file_pattern" | out-file -filepath C:\fc.txt -encoding ASCII -Append
              }
          
              write-output quit | out-file -filepath C:\fc.txt -encoding ASCII -Append
          }
          
          
          ###########################  Init Section ###############################
          $yesterday = (get-date).AddDays(-1)
          $yesterday_fmt = date $yesterday -format "yyyyMMdd"
          $file_pattern = "BRAE_GE_*" + $yesterday_fmt + "*.csv"
          $file_log = $yesterday_fmt + ".log"
          
          echo  $file_pattern
          echo  $file_log
          
          
          ############################## Main Section ############################
          # Change location to folder where the files need to be downloaded
          cd c:\remotefiles
          
          # Dynamically create the FTP Command to get a list of files from
          # the remote servers
          echo "Call function that creates a FTP Command "
          make_ftp_command_file $file_pattern N
          
          
          #echo "Connect to remote site via FTP"
          # Connect to Remote Server and get file listing
          ftp -n -v -s:C:\Clover\scripts\fc.txt 10.129.120.31 > C:\logs\$file_log
          
          $matches=select-string -pattern "BRAE_GE_[A-Z][A-Z]*" C:\logs\$file_log
          
          # Check if the required number of Files available for download
          if ($matches.count -eq 36)
          {
              # Create the FTP command file
          
              # This time the command file has an mget rather than an ls
              make_ftp_command_file $file_pattern Y
          
              # Change directory if not done so
              cd c:\remotefiles
          
              # Invoke ftp with newly created command file
              ftp -n -v -s:C:\Clover\scripts\fc.txt 10.129.120.31 > C:\logs\$file_log
          }
          else
          {
              echo "The full set of files is not available"
          }
          

          【讨论】:

            【解决方案9】:

            我已经编写了一个脚本作为 *.sh 文件

            #!/bin/sh
            set -x
            FTPHOST='host-address'
            FTPUSER='ftp-username'
            FTPPASSWD='yourPass'
            
            ftp -n -v $FTPHOST << EOT
            ascii
            user $FTPUSER $FTPPASSWD
            prompt
            ##Your commands
            bye
            EOT
            

            适合我

            【讨论】:

            • “ascii”这一行在做什么?转换为 ASCII 字符编码,还是寻找 ASCII 文件?
            • Nvm,得知“ASCII”行正在转换。
            【解决方案10】:

            如果您需要将变量传递给 txt 文件,您可以动态创建并在之后删除。

            此示例是以管理员身份运行的批处理脚本。它使用一些日期和时间变量创建一个 zip 文件。然后它会动态创建一个带有一些变量的 ftp 文本文件。然后它会删除 zip、文件夹和 ftp 文本文件。

            set YYYY=%DATE:~10,4%
            set MM=%DATE:~4,2%
            set DD=%DATE:~7,2%
            
            set HH=%TIME: =0%
            set HH=%HH:~0,2%
            set MI=%TIME:~3,2%
            set SS=%TIME:~6,2%
            set FF=%TIME:~9,2%
            
            set dirName=%YYYY%%MM%%DD%
            set fileName=%YYYY%%MM%%DD%_%HH%%MI%%SS%.zip
            
            echo %fileName%
            
            "C:\Program Files\7-Zip\7z.exe" a -tzip C:\%dirName%\%fileName% -r "C:\tozip\*.*" -mx5
            
            (
                echo open 198.123.456.789
                echo user@somedomain.com
                echo yourpassword
                echo lcd "C:/%dirName%"
                echo cd  theremotedir
                echo binary
                echo mput *.zip
                echo disconnect
                echo bye
            ) > C:\ftp.details.txt
            
            
            cd C:\
            FTP -v -i -s:"ftp.details.txt"
            
            del C:\ftp.details.txt /f
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-11-21
              • 1970-01-01
              • 2022-11-06
              相关资源
              最近更新 更多