簡體   English   中英

如何使用批處理文件進行ftp?

[英]How to ftp with a batch file?

我想要一個批處理文件 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

用您的詳細信息替換servernameusernamepassword ,批處理文件將生成腳本為 temp.txt 使用腳本啟動 ftp 然后刪除腳本。

如果您總是得到相同的文件,您可以用文件名替換%1 如果不是,您只需啟動批處理文件並提供文件名作為參數。

這是一篇舊帖子,但是,另一種選擇是使用命令選項:

ftp -n -s:ftpcmd.txt

-n 將禁止初始登錄,然后文件內容將是:(用您的 FTP 站點 url 替換 127.0.0.1)

open 127.0.0.1
user myFTPuser myftppassword
other commands here...

這避免了用戶/密碼在單獨的行上

您需要在文本文件中編寫 ftp 命令,並將其作為 ftp 命令的參數提供,如下所示:

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 產品文檔中所述

您必須指定代替FileName的文件名必須包含要發送到服務器的 FTP 命令。 這些命令包括

  • 打開計算機 [端口]以連接到 FTP 服務器,
  • 用戶用戶名 [密碼] [帳戶]與 FTP 服務器進行身份驗證,
  • 獲取 RemoteFile [LocalFile]以檢索文件,
  • quit結束 FTP 會話並終止 ftp 程序。

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

您也可以使用 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"
}

這是我使用的。 在我的例子中,某些 ftp 服務器(一個是純 ftpd)即使使用 -i 參數也會始終提示輸入用戶名,並捕獲“用戶用戶名”命令作為交互式密碼。 我所做的就是輸入幾個 NOOP(無操作)命令,直到 ftp 服務器超時,然后登錄:

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

我寫了一個腳本作為 *.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

對我來說很好用

如果您需要將變量傳遞給 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM