Batch file for telnet output to .txt file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Abhi
Posts: 1
Joined: 07 Apr 2014 06:09

Batch file for telnet output to .txt file

#1 Post by Abhi » 07 Apr 2014 06:17

Hi,

I am trying to create a batch file , that show me the result on txt file for port available/ not available using telnet ...

@echo off
telnet 192.168.0.173 9091
if !%errorlevel%==! goto error1

ELSE
( goto error2)

:error1
echo TELNET_Success >> D:\host1_%date%.txt

:error2
echo TELNET_fail >> D:\host1_%date%.txt



but its not working ....in both case it is showing same result ... :(

Guys pls help

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch file for telnet output to .txt file

#2 Post by penpen » 07 Apr 2014 07:32

I'm not sure if telnet sets the errorlevel in that way.
You have to add some goto's to avoid processing both echoes to file.
In addition the syntax of the for else is not right.
Maybe this works (if telnet sets the errorlevel):

Code: Select all

@echo off
telnet 192.168.0.173 9091
if %errorlevel% == 0 goto :error1
goto :error2

:error1
echo TELNET_Success >> D:\host1_%date%.txt
goto :eof

:error2
echo TELNET_fail >> D:\host1_%date%.txt
goto :eof

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file for telnet output to .txt file

#3 Post by foxidrive » 07 Apr 2014 20:27

This uses the free Telnet Scripting Tool v.1.0 by Albert Yale

Code: Select all

@echo off
:: The format is: remote.domain.com port
set host=ftp.domain.com 21
(
echo %host%
echo WAIT "\m"
echo SEND ""
)>"%temp%\tst.script"

tst10.exe /r:"%temp%\tst.script" /o:"%temp%\tst.info" /m
for %%a in ("%temp%\tst.info") do if %%~za EQU 0 (echo %host% not responding) else (echo %host% is up)
pause

Post Reply