20

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ericj78
  • 301
  • 1
  • 2
  • 4

5 Answers5

42

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 " "
kokbira
  • 608
  • 1
  • 10
  • 25
Mukul Goel
  • 8,387
  • 6
  • 37
  • 77
  • Thanks for this. I'll see if I can get something like this working. – Ericj78 Nov 05 '12 at 13:31
  • @JonathanRioux Please accept this answer. This will mark question as closed and will give you +2 to reputation as well. – Artemix Jul 10 '13 at 09:19
  • works like a charm on W7 64bit. Just be careful about the sleep time. 50milliseconds doesn't have to be enough. You might end up with telnet asking for username and .... waiting ... because vbs already passed all the keys while telnet was connecting... – Radek Oct 01 '15 at 02:10
  • 1
    Appreciate this Helped me out on my current project. – NetworkKingPin Feb 16 '16 at 07:12
  • If i send "x+1{ENTER}" i get x! as the string is sent. What is the solution for this? – ATT May 13 '19 at 07:47
5

The microsoft telnet.exe is not scriptable without using another script (which needs keyboard focus), as shown in another answer to this question, but there is a free

Telnet Scripting Tool v.1.0 by Albert Yale

that you can google for and which is both scriptable and loggable and can be launched from a batch file without needing keyboard focus.

The problem with telnet.exe and a second script when keyboard focus is being used is that if someone is using the computer at the time the script runs, then it is highly likely that the script will fail due to mouse clicks and keyboard use at that moment in time.

Kikiwa
  • 1,213
  • 2
  • 13
  • 20
foxidrive
  • 40,353
  • 10
  • 53
  • 68
2

I figured out a way to telnet to a server and change a file permission. Then FTP the file back to your computer and open it. Hopefully this will answer your questions and also help FTP.

The filepath variable is setup so you always login and cd to the same directory. You can change it to a prompt so the user can enter it manually.

:: This will telnet to the server, change the permissions, 
:: download the file, and then open it from your PC. 

:: Add your username, password, servername, and file path to the file.
:: I have not tested the server name with an IP address.

:: Note - telnetcmd.dat and ftpcmd.dat are temp files used to hold commands

@echo off
SET username=
SET password=
SET servername=
SET filepath=

set /p id="Enter the file name: " %=%

echo user %username%> telnetcmd.dat
echo %password%>> telnetcmd.dat
echo cd %filepath%>> telnetcmd.dat
echo SITE chmod 777 %id%>> telnetcmd.dat
echo exit>> telnetcmd.dat
telnet %servername% < telnetcmd.dat


echo user %username%> ftpcmd.dat
echo %password%>> ftpcmd.dat
echo cd %filepath%>> ftpcmd.dat
echo get %id%>> ftpcmd.dat
echo quit>> ftpcmd.dat

ftp -n -s:ftpcmd.dat %servername%
del ftpcmd.dat
del telnetcmd.dat
JoBaxter
  • 710
  • 2
  • 12
  • 23
  • 8
    Redirecting a telnetcmd.dat file to telnet does not work for me. It appears to be ignored. I am using Windows 7 64bit. – Martin J.H. Mar 23 '14 at 21:39
0

First of all, a caveat. Why do you want to use telnet? telnet is an old protocol, unsafe and impractical for remote access. It's been (almost)totally replaced by ssh.

To answer your questions, it depends. It depends on the telnet client you use. If you use microsoft telnet, you can't. Microsoft telnet does not have any mean to send commands from a batch file or a command line.

PA.
  • 28,486
  • 9
  • 71
  • 95
  • 6
    I don't **WANT** to use telnet but it is the only method available to connect to an archaic temperature monitoring device in our foundry. I am using the Microsoft telnet client, so it looks like a simple batch file is not an option. – Ericj78 Nov 05 '12 at 13:30
  • 6
    There are many telnet services that have not been replaced by SSH. Ideally, SSH should be used if available, but it isn't always :( – SSH This Apr 16 '13 at 16:08
0

This is old, but someone else may stumble on it as I did. When you connect to the DataCast, you are talking to a daemon that can access the database. It was intended that a customer would write some code to access the database and store the results somewhere. It just happens that telnet works to access data manually. netcat should also work. ssh obviously will not.