How to pass commands to a telnet window with a batch script?



The telnet command does not offer many options for entering some commands...


I will leave here two possible options

  • To do it in bat/cmd with vbs

  • To do it with the software console called Telnet Script Tool

  • Option #1 using bat and vbs...

One option would be to use SendKey/VBS very useful for sending keys, commands etc., will send your entries/type send them to telnet interfaces instance/session...

Below an example of using SendKey/VBS to send login/input data by bat file and which generates in runtime the file VBS to perform this task.

@echo off 
setlocal enabledelayedexpansion 

echo/ && cls && color 9F
%__APPDIR__%mode.com 77,30

set "_user_=cisco_user"
set "_pwd_=my_secret_pwd"
set "_temp_vbs=%tmp%\_tmp_file_vbs_.vbs"

>"!_temp_vbs!"^
    (
     echo/ Set WshShell = WScript.CreateObject^("WScript.Shell"^)
     echo/ Set objShell = WScript.CreateObject^("WScript.Shell"^)
     echo/ StrPwd  = "!_pwd_!"
     echo/ StrUser = "!_user_!"
     echo/ for i=1 To Len^(StrUser^)
     echo/     x = Mid^(StrUser,i,1^)
     echo/     WshShell.SendKeys x
     echo/     Wscript.Sleep 250
     echo/ Next
     echo/ Wscript.Sleep 500
     echo/ WshShell.SendKeys "({ENTER})"
     echo/ for j=1 To Len^(StrPwd^)
     echo/     x = Mid^(StrPwd,j,1^)
     echo/     WshShell.SendKeys x
     echo/     Wscript.Sleep 200
     echo/ Next 
     echo/ Wscript.Sleep 200
     echo/ Wscript.Sleep 200
     echo/ WshShell.SendKeys "dir"
     echo/ Wscript.Sleep 200
     echo/ WshShell.SendKeys "({ENTER})"
     echo/ Wscript.Sleep 200
     echo/ WshShell.SendKeys "quit"
     echo/ Wscript.Sleep 200
     echo/ WshShell.SendKeys "({ENTER})"
     ) && %__APPDIR__%telnet.exe 192.168.0.254 
     
"%__APPDIR__%cScript.exe" //nologo "!_temp_vbs!"
2>nul >nul del /q /f "!_temp_vbs!" & endlocal & goto :EOF

You can consider seeing this question, as to why telnet.exe is not running on bat/cmd.

To correct this, apply this command on the command line (this requires administrator rights) and run this command only once

for /f %i in ('%__APPDIR__%where /r "C:\Windows\System32" "telnet.exe.mui" ^|%__APPDIR__%findstr.exe [a-z]\-[A-Z] ')do for %C in ("%windir%\system32\.","%windir%\SysWOW64\.")do if exist "%~C." copy /y "%~i" "%~C"

  • Option #2 using Telnet Script Tool...

Telnet Scripting Tool is a utility to automate telnet sessions (like calling dip on a Linux system, or doing router maintenance for example).

The Telnet Script Tool can also send entries to telnet...

Basically, this software reads the screen and looks for a prediction string that you will inform for waiting until the next command to be sent to telnet by the software...

Below is an example of using the Telnet Script Tool that send command inputs by using content of a text file: "%temp%\script_ts.scr"

@echo off 

setlocal enabledelayedexpansion 

%__APPDIR__%mode.com 77,30
echo/ && color 9F && echo/

set "_user_=my_user_name"
set "_pwd_=my_secret_pwd"
set "_ip_door_=10.0.50.1 23"

>"%temp%\script_ts.scr" ^
    (
     echo=!_ip_door_!
     echo=WAIT "User Name"
     echo=SEND "!_user_!\m"
     echo=WAIT "Passoword"
     echo=SEND "!_pwd_!\m"
    ) && "%temp%\TST10.exe" /r:"%temp%\script_ts.scr" /o:"%temp%\output_ts.txt"
endlocal & goto :EOF


  • Update v2 - Porting bat with sendkey to your command (string):
 {"id":0,"method":"set_power","params":["on","smooth",500]}

To use Send Key in your command with many special characters, you need to escape {:)}

@echo off && setlocal enabledelayedexpansion 

echo/ && cls && color 9F && %__APPDIR__%mode.com 77,30 && set "_temp_vbs=%tmp%\_tmp_file_vbs_.vbs" && >"!_temp_vbs!"^
  (    
   echo= On Error Resume Next
   echo= Set WshShell = WScript.CreateObject("WScript.Shell"^)
   echo= Set ObjShell = WScript.CreateObject("WScript.Shell"^)
   echo= Wsh.sleep 2000 'adjust this timeout for your needs
   echo= ObjShell.AppActivate "MS Telnet CMD" 
   echo= Wsh.sleep 333
   echo= WshShell.SendKeys "o 192.168.0.1 55443~"
   echo= Wsh.sleep 1500
   echo= WshShell.SendKeys "({{}{""}id{""}:0,{""}method{""}:{""}set_power{""},{""}params{""}{:}{[}{""}on{""},{""}smooth{""},500{]}{}}})"
   echo= Wsh.sleep 50
   echo= WshShell.SendKeys "~"
   echo= Wsh.sleep 50
   echo= WshShell.SendKeys "^]"
   echo= Wsh.sleep 50
   echo= WshShell.SendKeys "quit~"
  ) && pushd %windir%\system32\ & title <nul && title MS Telnet CMD

start "" /b "%__APPDIR__%cScript.exe" //nologo "!_temp_vbs!" && call telnet.exe 

:loop
tasklist.exe /nh | findstr.exe /i cscript.exe >nul && goto :loop
2>nul >nul del /q /f "!_temp_vbs!" & popd & endlocal & exit
  • Telnet Script Tool
  • Telnet Script Tool - Question/Answer
  • SendKey and the Escaping for Special Characters