5

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.

Hennes
  • 65,804
  • 7
  • 115
  • 169
an531
  • 95

2 Answers2

7


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

Io-oI
  • 9,237
0
import telnetlib
import time
import argparse

# Set up argument parser
parser = argparse.ArgumentParser(description="Automate Telnet commands")
parser.add_argument("host", help="The Telnet server IP address or hostname")
parser.add_argument("port", type=int, help="The Telnet server port")
parser.add_argument("user", help="The username for Telnet login")
parser.add_argument("password", help="The password for Telnet login")
parser.add_argument("command", help="The command to execute on the Telnet server")

# Parse the arguments
args = parser.parse_args()

HOST = args.host
PORT = args.port
USER = args.user
PASSWORD = args.password
COMMAND = args.command

# Connect to the Telnet server
tn = telnetlib.Telnet(HOST, PORT)

# Read the initial message (welcome message)
tn.read_until(b"login: ")

# Send the username
tn.write(USER.encode('ascii') + b"\n")

# Read until password prompt
tn.read_until(b"Password: ")

# Send the password
tn.write(PASSWORD.encode('ascii') + b"\n")

# Give some time to process login
time.sleep(2)

# Send the command
tn.write(COMMAND.encode('ascii') + b"\n")

# Give some time for the command to execute
time.sleep(2)

# Read the output
output = tn.read_very_eager().decode('ascii')
print(output)

# Close the connection
tn.write(b"exit\n")
tn.close()
Toto
  • 19,304