windows 如何使用批处理文件进行ftp?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16158138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:27:29  来源:igfitidea点击:

How to ftp with a batch file?

windowsbatch-fileftp

提问by user1935683

I want a batch file to ftp to a server, read out a text file, and disconnect. The server requires a user and password. I tried

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

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

but it never logged on. How can I get this to work?

但它从未登录。我怎样才能让它发挥作用?

回答by Outlier

The answer by 0x90hhelped a lot...

0x90h的回答很有帮助...

I saved this file as u.ftp:

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

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

I then ran this command:

然后我运行了这个命令:

ftp -i -s:u.ftp

And it worked!!!

它奏效了!!!

Thanks a lot man :)

非常感谢人:)

回答by 0x90h

Using the Windows FTP client you would want to use the -s:filenameoption to specify a script for the FTP client to run. The documentation specifically points out that you should not try to pipe input into the FTP client with a <character.

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

Execution of the script will start immediately, so it does work for username/password.

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

However, the security of this setup is questionable since you now have a username and password for the FTP server visible to anyone who decides to look at your batch file.

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

Either way, you can generate the script file on the fly from the batch file and then pass it to the FTP client like so:

无论哪种方式,您都可以从批处理文件动态生成脚本文件,然后将其传递给 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

Replace servername, username, and passwordwith your details and the batch file will generate the script as temp.txt launch ftp with the script and then delete the script.

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

If you are always getting the same file you can replace the %1with the file name. If not you just launch the batchfile and provide the name of the file to get as an argument.

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

回答by Mark Schultheiss

This is an old post however, one alternative is to use the command options:

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

ftp -n -s:ftpcmd.txt

the -n will suppress the initial login and then the file contents would be: (replace the 127.0.0.1 with your FTP site url)

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

open 127.0.0.1
user myFTPuser myftppassword
other commands here...

This avoids the user/password on separate lines

这避免了用户/密码在单独的行上

回答by Lasse

You need to write the ftp commands in a text file and give it as a parameter for the ftp command like this:

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

ftp -s:filename

More info here: http://www.nsftools.com/tips/MSFTP.htm

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

I am not sure though if it would work with username and password prompt.

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

回答by NotMe

Each line of a batch file will get executed; but only after the previous line has completed. In your case, as soon as it hits the ftp line the ftp program will start and take over user input. When it is closed then the remaining lines will execute. Meaning the username/password are never sent to the FTP program and instead will be fed to the command prompt itself once the ftp program is closed.

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

Instead you need to pass everything you need on the ftp command line. Something like:

相反,您需要在 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

回答by Oswald

Use

ftp -s:FileName 

as decribed in Windows XP Professional Product Documentation.

Windows XP Professional 产品文档中所述

The file name that you have to specify in place of FileNamemust contain FTP commands that you want to send to the server. Among theses commands are

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

  • open Computer [Port]to connect to an FTP server,
  • user UserName [Password] [Account]to authenticate with the FTP server,
  • get RemoteFile [LocalFile]to retrieve a file,
  • quitto end the FTP session and terminate the ftp program.
  • 打开计算机 [端口]以连接到 FTP 服务器,
  • 用户用户名 [密码] [帐户]与 FTP 服务器进行身份验证,
  • 获取 RemoteFile [LocalFile]以检索文件,
  • quit结束 FTP 会话并终止 ftp 程序。

More commands can be found under Ftp subcommands.

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

回答by kev

You can use PowerShell as well; this is what I did. As I needed to download a file based on a pattern I dynamically created a command file and then let ftpdo the rest.

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

I used basic PowerShell commands. I did not need to download any additional components. I first checked if the requisite number of files existed. If they I invoked the FTP the second time with an Mget. I run this from a Windows Server 2008connecting to a Windows XP remote server.

我使用了基本的 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"
}

回答by Justin Goldberg

Here's what I use. In my case, certain ftp servers (pure-ftpd for one) will always prompt for the username even with the -i parameter, and catch the "user username" command as the interactive password. What I do it enter a few NOOP (no operation) commands until the ftp server times out, and then login:

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

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

回答by Ghayel

I have written a script as *.sh file

我写了一个脚本作为 *.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

Works fine for me

对我来说很好用

回答by drooh

If you need to pass variables to the txt file you can create in on the fly and remove after.

如果您需要将变量传递给 txt 文件,您可以即时创建并删除。

This is example is a batch script running as administrator. It creates a zip file using some date & time variables. Then it creates a ftp text file on the fly with some variables. Then it deletes the zip, folder and ftp text file.

这是一个以管理员身份运行的批处理脚本示例。它使用一些日期和时间变量创建一个 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-Zipz.exe" a -tzip C:\%dirName%\%fileName% -r "C:\tozip\*.*" -mx5

(
    echo open 198.123.456.789
    echo [email protected]
    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