vbscript测试ftp连接

时间:2015-06-18 14:50:51

标签: testing vbscript ftp connection

我尝试测试我的ftp连接,

我需要恢复建立连接所需的时间并进入目录

我必须在用户或密码错误时出现错误信息

我试试这个,但是当没有建立连接或用户或密码错误时我不会收到错误。

Set WshShell = CreateObject("WScript.Shell" )       

        wshShell.run "ftp.exe",true  
        wscript.sleep 100
        date_before_timestamp=Timer

        WshShell.SendKeys "ftp.exe"&chr(13)
        WshShell.SendKeys "open 127.0.0.1 21"&chr(13)
        WshShell.SendKeys "galene"&chr(13)
        WshShell.SendKeys "galene"&chr(13)
        WshShell.SendKeys "cd test"&chr(13)
        WshShell.SendKeys "quit"&chr(13)
        WshShell.SendKeys chr(13)
        date_after_timestamp=Timer

        interval= date_after_timestamp - date_before_timestamp
        wscript.echo interval

我怎么做?

1 个答案:

答案 0 :(得分:1)

这是一种方式。将FTP命令添加到文本文件中并将其作为参数传递(使用-s开关)。然后,将FTP命令的输出重定向到可以解析的文件。

' Create the FTP command file...
With CreateObject("Scripting.FileSystemObject").CreateTextFile("c:\ftp.txt", True)
    .WriteLine "USER testuser"  ' Login
    .WriteLine "secret1"        ' Password
    .WriteLine "cd test"        ' Perform a command once logged in
    .WriteLine "quit"           ' Done
    .Close
End With

' Run the command and capture its output...
With CreateObject("WScript.Shell")
    tmrStart = Timer
    .Run "%comspec% /c ftp -n -v -s:c:\ftp.txt ftp.mysite.com >c:\output.txt", 0, True
    tmrEnd = Timer
End With

以下是您在登录失败时可能会在output.txt中看到的内容的示例:

ftp> USER testuser
User cannot log in.