Windows – How to pass commands to a telnet window with a batch script

batch filecommand linetelnetwindows

I'm trying to control a wifi device with a batch script using the telnet command in Windows 7, but I don't know how to pass the commands to the telnet window. This will be a part of a longer script tweaking other devices and PC settings.

I can do this manually by running telnet <IP address> 55443 in cmd.exe, which creates a new window. In that window, I can write the commands for the device and it works as intended. However, when I write the code in the script, like this:

telnet <IP address> 55443
<device commands>

The <device commands> are not passed on to the newly created telnet window.

Best Answer



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


    I will leave here two possible options

    • Option #1 using and ...

    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 /.

    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"
    

    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
    
  • Related Question