# Need Help with DOS batch file ..



## arul20

Hi I am writing a batch file to automatically run some commands by telnetting to a network device. Everything goes fine until the device asks for input from the user.

E.g "Press any key to continue .. "
Or
"Press enter to continue .. "

etc

The program then waits for the user input.

So I am forced to manually press the keyboard in order for the batch file to continue. 

What I want to know is:

How do I simulate the "Enter" key press in my batch program?


P.S: I did googling today on this subject but they all recommend using the "CON" command with fc command. I am running windows 2000 and my DOS does not support the "CON" command.


Thanks for all the help in advance.


----------



## arul20

I learnt from somewhere that I should store the return key or "carriage return"

in another file and pipe this ..

I have not much idea how to go about doing the above. Anybody can help?


Thanks a lot for helping guys.


----------



## asbo

Why would you put the PAUSE in in the first place if you are just going to simulate it occuring anyway?


----------



## arul20

HI ASBO,

I do not have a PAUSE in my code. I did not initiate the pause.

What happens is that the device I TELNET to asks me to press the enter key (in order to continue).

Example: "Press <enter> to continue ... "

The device is similar to a router.

When the device asks ME to press <enter>, my batch program stops there and waits FOR ME to press enter.

I WANT: My program to simulate the enter key being pressed.

Thanks for helping guys.


----------



## asbo

Ah, I understand now.

To the best of my knowledge, this is impossible with merely a batch file, as they were designed to execute, as oppose to interface with programs.


----------



## arul20

Thanks for the reply ..


So what other methods can I use to:

1. Telnet to a device
2. Enter username and password
3. Execute commands

I need this to be done automatically.

Thanks again for your help ..


----------



## MacFromOK

You can simulate an ENTER key press on many DOS apps by running the command preceeded by "*echo.|*" (no quotes) on the same line like this:

*echo.|<command>*

where <command> is the exe/com file that will require an enter keystroke. This is similiar to pressing the ENTER key before ya actually see a prompt to do so. 

Cheers, Mac


----------



## arul20

Thanks MacFromOK,

I have about 20 network devices to manage.

Whenever there is some common change in setting, I have to log on to each and every device and reconfigure.

What I am trying to do is (cos i'm lazy), I want to create a script to automatically log in to a device and execute commands and then log off and login to the next device .. etc .. all auto.

So using batch file, I am able to start telnet. BUT as I learnt over the past few days, DOS is not optimised for interfacing with devices (thanks ASBO). Thus I cannot execute telnet commands.

So I also tried Hyperterminal. You can prepare text files with the necessary commands before hand and after logging in to a device, let the file run. It is WAY faster than typing by hand all your usual commands.

However, I still have to create a new connection each time. I want to automate that too.

Currently I am researching telnet clients with scripting facility. CRT is popular in my office, however it has limited scripting ability. Then I found TN 3270 yesterday. It seems to be meeting my expectations, but I haven't wrote the script yet.

I'll welcome any suggestions. Thanks.


----------



## asbo

I'm not sure how much programming experience you have, or maybe someone in the office has, but maybe you could write a simple program that automates the whole thing -- VB would be simplest, but I'm positive there's more options out there, maybe even CGI/Perl/PHP. Just a thought.


----------



## arul20

Thanks Asbo,

I know VB and nothing of CGI/Perl/PHP.


----------



## asbo

Alright, in that case, I'm fairly certain that Visual Studio includes a Telnet component, and I've definately seen some stuff on the web relating to VB/VB.NET and telnet.

You could poke around on MSDN's Visual Basic section and Google for some more insight on that.


----------



## arul20

Ok ASBO,

I'll try it within a few days and post back.

Thanks.


----------



## yto_daniel

I built something in C/C++ that does exactly what you need arul20, it was a custom telnet client that logged into a device (Cable Access Router) automatically put in some commands, retrieved the results and stored those in a database for later retrieval. (Which it did by webinterface) So it can definately be done. Its just depends on how much time and effort you want to spend trying.

Daniel - YourTechOnline.com technician
[email protected]_spam_yourtechonline.com (remove no_spam_)


----------



## arul20

can I see a snipet of your code daniel? I sent u an email regarding it too ..


----------



## yto_daniel

which part of my code would you like to see? Its not setup that great, just a quick and dirty thing, but it works. I started off by finding a opensource telnet client somewhere, and simply modified it to send commands when it saw the prompts required.

Daniel - YourTechOnline.com technician


----------



## arul20

oh ok .. kinda like what I am doing .. I am using TN3270.

Which client did you use?

I just want to get the general idea from your code .. I have the idea already ..


I.E .. Login to device 1
(call command script and execute)
Logout/disconnect
:
:
Login to device 2
(call command script and execute)
Logout/disconnect 

and so on ..
Did you do the same thing or is there a more efficient way?

Thanks


----------



## arul20

Thanks for the reply Daniel.

Oh kinda like what I am doing now .. I am using TN3270 telnet client.
I wanted to get the general idea of your code. I have an idea already, but it might not be the same idea.
It involves two scripts:

Logon Script - With the logon details for all devices
Command script - With the commands

I.E 

Run logon script

logon to DEvice 1
call Command script and excute commands
Loggout/disconnect
:
:
logon to DEvice 2
call Command script and excute commands
Loggout/disconnect
:
:
logon to DEvice Y
call Command script and excute commands
Loggout/disconnect


This is my idea .. Haven't worked on it yet. Is your code based on the same idea or is there a more efficient method?

Thank You


----------



## arul20

As you have noticed, I posted a second msg above because I thought the first one was lost.


----------



## yto_daniel

My code is based on the same idea, Connect to the device, detect logon prompt, send the username/password, detect the next prompt, Send the correct command, get the results, disconnect. Repeat.

Daniel - YourTechOnline.com technician


----------



## arul20

Hi guys .. worked on it yesterday and today and it works.
I used three files. One Batch file, two script files for the telnet client 
Thanks for all the help ..


::How it works:
::
:: .Batch file is run
::	.Batch File runs TN3270 
:: .Opens Telnet connection to one address and calls a script (passwords.txt)
::
:: .Passwords.txt contains individual commands for each connection
:: Besides password, other specific commands such as banner can be specified here 
::	.TN3270 runs passwords.txt 
:: .looks up the list of passwords and logs in to the open connection
:: .calls the 'common commands script' -> cmds.txt	
::
:: .Cmds.txt contains the common commands to be executed for ALL the devices
::	.TN3270 executes the commands found in cmds.txt
:: .execution is passed back to passwords.txt which exits TN3270
:: .excution is passed back to batch file
::
:: .Batch file opens the second connection using TN3270
:: .The process is repeated
:: .When all devices have been contacted, the batch file closes DOS window.


----------



## bphilyaw

Hi - I'm trying to do the same thing - but with a Internet Power Switch - I can telnet into the device from a .bat file - but then all of my commands are not passed. Could you please provide the script syntax you used.

Thanks for your help in advance.

Ben


----------



## arul20

You try it out first please?

Download TN3270 plus .. try out the scripting feature .. any problem, post back.

There are two ways to solve the problem .. 

1. The way described in the above post. (This is if everything's automated.)

2. This is for system admins who have many devices and need all of them up and running in different sessions, preferablly in the same program window. When the script is run, it opens up all the devices specified and sends individual and common commands. All the devices are open in individual windows. The administrator can then continue to work on the devices or check the results.
--> I did not mention this in the posts above <--


Thanks.


----------



## classicsoftw

Try using the >null command in your batch files. This prevents screen echos.

You may also have to put a lf/cr into the file in the appropraite place.


----------

