Link to home
Start Free TrialLog in
Avatar of nummagumma2
nummagumma2Flag for United States of America

asked on

Automate FTP logon test

I want to create a simple batch file that can confirm an FTP server can be logged in to:

ie:

ftp -s:c:\temp\test.ftp

where test.ftp =

open ftp.domain.com
user
password
ls
quit

The results output 230 on success, and 530 on failure.  There's no errorlevel, however.

How can I run this and have it parse the results out?

ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nummagumma2

ASKER

Good logic - but the errorlevel string was backwards... =)

ftp -s:c:\temp\test.ftp > %temp%\ftp.log 2>&1
find /i "530" %temp%\ftp.log
If "%errorlevel%" == "0" (
      Echo Connect Failed.
      ) else (
      Echo Connect Succeeded.)


Out of curiosity, what does the 2>&1 do?  It worked fine with it omitted, too.
If errorlevel = 0 then nothing is wrong - no error.  I don't think my logic was backward.

1 = standard output
2 = standard error

> = default redirect standard output
1> = same as >

2> = redirect standard error
2>&1 = redirect standard error to the same file as standard output.

Thus:

... > file.txt 2>&1

redirects both standard output and standard error to the same file, file.txt.  Otherwise, you might miss some details.

Try this for an illustration of how it works:

at a command prompt, type "net /? > file.txt"  Instead of the help info being saved to file.txt, it is displayed.  So the redirection doesn't seem to work.  Then execute the same command like this:  "net /? 2> file.txt" and you'll find the info is now sent to the file.txt
The logic was only 'backwards' in that the "find" command returned a 0 when it found 530, therefore indicating that it was successful.

Thanks for the explanation of the rest.