Link to home
Start Free TrialLog in
Avatar of rortiz77
rortiz77

asked on

Need a batch script that will telnet into a server and run a command

Looking for a batch script that will establish a telnet connection and then run a command.  Is this even possible?
Avatar of activematx
activematx
Flag of United States of America image

Try combining:

echo password | telnet -l username IPaddress

Be aware that putting unencrypted passwords into scripts/batch files is a security breach.

Alternatively you could use other languages such as perl to do this task:

    use Net::Telnet ();
    $t = new Net::Telnet (Timeout => 10,
                          Prompt => '/bash\$ $/');
    $t->open("hostname");
    $t->login($username, $passwd);
    @lines = $t->cmd("who");
    print @lines;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of activematx
activematx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rortiz77
rortiz77

ASKER

@activematx, your right...don't want to free text my password in there. How does this other tool not store my password? Kinda hard to make sense of it so far...
How does this other tool not store my password?

This other tool will also store your password in a text file.  :(

Here is a write up of what the program does.  Link:  http://jerrymannel.com/blog/2008/11/11/telnet-scripting-tool-aka-tst10exe/
How about using PuTTY to fo this?
Hi SecureCRT is the BEST tool to do this automation..

Learn to use from the below link..
http://www.vandyke.com/products/securecrt/

I am using the same for telnet and run the commands which are preloaded.

Attached are the screenshots..just for reference..

Cheers!
secureCRT1.JPG
secureCRT1.JPG
@B_Denison @skraaz,

I'm all for telnet but this needs to be something that the SQL server can execute.  If so, then its a good option.
Ok, spoke with our admin and he has no concerns with using this Telnet script Tool.  His server is secured enough...so he's good to go.  

I've done some testing and I can't get passed the logon part.  What I've done so far is that I have a text file called, "logon.txt" and an "Output.txt" file which is blank.  

My logon.txt file contains:

128.#.#.# 23
WAIT "login"
SEND "username\m"
WAIT "password"
SEND "$1234\m"
WAIT ">"
SEND "ss1\m"
WAIT ">"
SEND "stop_procs\m"
WAIT ">"

I then go to the cmd and type:
TST10.exe /r:logon.txt /o:output.txt /m
but it fails to get passed the username/password.

It then shows this:
Welcome to Microsoft Telnet Service 
login: hciuser
ciuser
password: $eveN11
The handle is invalid.

Login Failed
login: 

Open in new window

ok, figured out the bug.  Since it always wanted me to re-enter the username and password...then the solution is to put that in twice in the script.  Seems to work so far.
Avatar of Bill Prew
For what it's worth, here's another approach using the built in windows telnet client, and VBScript to automate the usage of that "command line" program.  You may be all set, but thought I'd pass it along...

http://www.makeuseof.com/tag/automate-telnet-commands-vb-script/

~bp