我想要一个批处理文件去连接FTP服务器,读取一个文本文件,然后断开连接。该服务器需要用户名和密码。我尝试过
@echo off
pause
@ftp example.com
username
password
pause
但它从未登录。我该如何让它起作用?
我想要一个批处理文件去连接FTP服务器,读取一个文本文件,然后断开连接。该服务器需要用户名和密码。我尝试过
@echo off
pause
@ftp example.com
username
password
pause
但它从未登录。我该如何让它起作用?
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
太好了!!!
非常感谢你啊 :)
使用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
用您的详细信息替换servername,username和password,批处理文件将生成脚本作为temp.txt启动ftp,然后删除脚本。
如果您总是获取相同的文件,则可以使用文件名替换%1
。 如果不是,则只需启动批处理文件并提供要作为参数获取的文件名。
-s
选项的内容,但是我只得到了“ftp: invalid option -- 's'”这个错误。这需要什么版本的ftp?我在Windows 10 1903上安装了1.9.4版本。 - iko79ftp
会运行随cygwin一起安装的ftp程序,哈哈。没事了,忘了我说过什么。 - iko79这是一篇旧帖子,然而一个替代方案是使用命令选项:
ftp -n -s:ftpcmd.txt
-n选项将禁止初始登录,然后文件内容将为:(将127.0.0.1替换为您的FTP网站URL)
open 127.0.0.1
user myFTPuser myftppassword
other commands here...
这样可以避免将用户名/密码分开放置在不同的行中。
ftp -s:filename
更多信息在此: http://www.nsftools.com/tips/MSFTP.htm
但我不确定它是否适用于用户名和密码提示。
批处理文件中的每一行都会被执行;但只有在前一行完成后才会执行下一行。在您的情况下,一旦它到达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
使用
ftp -s:FileName
根据Windows XP Professional产品文档,您需要指定一个包含要发送到服务器的FTP命令的文件名来代替FileName。这些命令中包括:
更多命令可在FTP子命令中找到。
这是我使用的方法。在我的情况下,某些ftp服务器(例如pure-ftpd)即使使用-i参数也会提示输入用户名,并将“user username”命令作为交互式密码捕获。我的做法是输入几个NOOP(无操作)命令直到ftp服务器超时,然后再登录:
open ftp.example.com
noop
noop
noop
noop
noop
noop
noop
noop
user username password
...
quit
ftp
完成其余工作。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"
}
#!/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
对我来说运行良好
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