Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a batch file to establish a telnet session, send a command and have the output written to a file?

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?

like image 357
Ericj78 Avatar asked Nov 02 '12 14:11

Ericj78


People also ask

What are the two important things needed to establish a telnet session What is the command?

The user connects to the server by using the Telnet protocol, which means entering Telnet into a command prompt by following this syntax: telnet hostname port. The user then executes commands on the server by using specific Telnet commands into the Telnet prompt.


3 Answers

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 " "
like image 56
Mukul Goel Avatar answered Oct 20 '22 07:10

Mukul Goel


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.

like image 39
foxidrive Avatar answered Oct 20 '22 07:10

foxidrive


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
like image 2
JoBaxter Avatar answered Oct 20 '22 06:10

JoBaxter