Is it possible to use a batch file to establish a telnet session, send a command and have the output written to a file?

Ericj78 picture Ericj78 · Nov 2, 2012 · Viewed 136k times · Source

I run the following batch file to establish a telnet session to a device and create a file that will hold information pulled from the device.

CD\
COLOR 0E
CLS
@echo off
ECHO This will start the connection to the Heraeus QuicK-Lab DATACAST ENtouch. 
pause
telnet 172.17.0.16 4224 -f C:\LogFiles\Datacast.log

After the telnet session is established I type in a command to dump data to Datacast.log as specified in the last line of code. I am hoping to include the command ("M3,1,999" for example) in the batch file somehow but I can find no similar examples.

Is it possible to do this with a batch file?

Answer

Mukul Goel picture Mukul Goel · Nov 2, 2012

Maybe something like this ?

Create a batch to connect to telnet and run a script to issue commands ? source

Batch File (named Script.bat ):

     :: Open a Telnet window
   start telnet.exe 192.168.1.1
   :: Run the script 
    cscript SendKeys.vbs 

Command File (named SendKeys.vbs ):

set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 50 
OBJECT.SendKeys "mylogin{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys "mypassword{ENTER}"
WScript.sleep 50 
OBJECT.SendKeys " cd /var/tmp{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys " rm log_web_activity{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys " ln -s /dev/null log_web_activity{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys "exit{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys " "