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

1

1

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.

an531

Posted 2020-02-25T17:08:33.763

Reputation: 25

1Put the commands in a textfile first, then use telnet ip port < textfile.txt – LPChip – 2020-02-25T17:44:01.810

@LPChip Thanks for the suggestion. Unfortunately, it doesn't work. I double-checked manually that the commands are correct. It works manually (like I described above), but with your method the cmd window just closes and nothing happens. – an531 – 2020-02-25T18:01:25.570

I don't have a real answer, but I can tell you why this is happening. Once you run the telnet <IP address> 55443 command, anything that happens after that is ran in a separate instance. It would be like if you tried to run start newscript.bat from a batch script, then try to put commands under that line in the parent script and expect them to be passed on to the child script; it just doesn't work like that. When you try to run telnet <IP address> 55443 from the normal command line, it will automatically carry you over to the new instance. From a script, it won't. – Ethan Waldeck – 2020-02-25T19:16:24.710

@an531 LPChip is correct in their statement. You might want to show what your telnet file looks like because redirecting from a file into the input buffer is a Windows Command thing and not a single application thing – UnhandledExcepSean – 2020-02-25T19:55:17.450

@UnhandledExcepSean I tried to test @LPChip method simply by creating a .bat that contains only this: telnet 192.168.0.11 55443 < "C:\Users\user1\Desktop\New Text Document.txt". Then I created New Text Document.txt containing only the device commands: {"id":0,"method":"set_power","params":["on","smooth",500]}. As far as I can tell, the device commands are still not passed onto the telnet window, but rather stay in the initial cmd window. In fact, the telnet window doesn't seem to activate at all, i.e. the device is not being connected to via telnet. – an531 – 2020-02-25T20:13:03.747

So, if you type telnet 192.168.0.11 55443 and then {"id":0,"method":"set_power","params":["on","smooth",500]} into a command window, it works? – UnhandledExcepSean – 2020-02-25T20:21:34.140

Also, put a pause at the end of your batch file to see if an error is occurring. – UnhandledExcepSean – 2020-02-25T20:26:26.457

@UnhandledExcepSean Yes it does work, I checked it again just now. However, when I do it step by step, the telnet window's title is "Telnet 192.168.0.11". When I do it via the aforementioned .bat (+ pause), then the window title is just "Telnet" and there are a few blank lines followed by C:\Users\user1\Desktop>pause. So, contrary to what I said before, the Telnet window does seem to activate, I'm just not sure if the device is being connected to (given lack of IP in the title). Also, I don't know what the blank lines are all about... – an531 – 2020-02-25T20:39:57.290

1What about just launching telnet and then opening the ip and port from inside the script? – UnhandledExcepSean – 2020-02-25T21:34:07.933

Make sure you put an enter at the end of your text in the .txt document to send an enter too, otherwise you type in the text, but not actually pressing enter. – LPChip – 2020-02-25T21:35:04.910

Type telnet /?. It says you can specify a file of telnet commands. – Mark – 2020-02-25T22:23:46.610

I tried moving IP and port to .txt file as well as adding enter in .txt, but it didn't work. It always opens a weird window just titled Telnet. There are two telnet windows that seem to work, one titled Telnet <IP address> (if I type telnet <ip> <port> into cmd) and one titled c:\windows\system32\cmd.exe - telnet (if I type telnet into cmd), which requires an additional o parameter before <ip>. However, I was able to invoke the right window with start "c:\Windows\system32\cmd.exe" telnet 192.168.0.11 55443, but I still cannot add the commands (just shows a blinking underscore). – an531 – 2020-02-25T22:50:18.480

@Mark I'm not sure what you mean. The only telnet parameters that seem pertinent are host and port. I don't know if the parameter -e is relevant to the problem ("escape character to enter telnet clinet prompt"). Could you please elaborate? – an531 – 2020-02-25T23:23:31.607

Answers

2



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

It Wasn't Me

Posted 2020-02-25T17:08:33.763

Reputation: 851

1

@ItWasntMe Yes, you can send commands like "on", "off" or selecting a specific colour of the light. This product is called Yeelight 1S Colour.

– an531 – 2020-02-27T22:50:08.340