8

Is it possible to use the Windows Telnet client to run a series of commands in a script?

If not are there any alternatives that can be?

LachlanG
  • 183
  • 1
  • 1
  • 5
  • 1
    Either you are quite confused about what a telnet client is supposed to be, or you didn't explain your question properly. – Juliano Jul 17 '09 at 00:48
  • @Juliano: Some telnet clients let you send data to the remote machine using a scripting language to drive the process. Think like "expect" scripts, but wired up to telnet. – Evan Anderson Jul 17 '09 at 00:52
  • 1
    It might help if you give us an idea of what you're trying to do, as there may be better methods then using the telnet app. – John Gardeniers Jul 17 '09 at 02:22
  • Like Cygwin, SSH with a password-less Public Key Authentication. http://superuser.com/questions/6475/can-the-windows-telnet-client-be-scripted/6657#6657 – nik Jul 17 '09 at 06:40

6 Answers6

7

Yes. Check out this thread.

Adam Brand
  • 6,127
  • 2
  • 30
  • 40
  • 1
    Zow! Using SendKeys to send keystrokes to the TELNET client. It's a one-way communications channel... I guess it would depend on whether the poster needed to get back data from the TELNET client or not. – Evan Anderson Jul 17 '09 at 01:11
  • Where there's a will, there's a way :-). – Adam Brand Jul 17 '09 at 01:20
7

Ages ago I wrote a COM server to allow me to do telnet from VBScript. If you want a copy (including source) I'd be happy to put it on the Internet somewhere. I use it for remotely rebooting routers.

To give you some idea of what it does, here's an example script (with error checking removed)

const SVR = "www.microsoft.com"
dim telnet, s, i

set telnet = CreateObject("Rhs.Telnet")
telnet.Startup()

telnet.Connect SVR, 80
telnet.Writeline "GET / HTTP/1.0"
telnet.Writeline "Host: www.microsoft.com"
telnet.Writeline "User-Agent: RhsTelnet"
telnet.Writeline "Accept: */*"
telnet.Writeline ""

do while telnet.Readline(s, 1000)
  wscript.echo s
loop

telnet.Disconnect()
telnet.Cleanup()

JR

Link to COM Server: hi Lachlan try http://www.ratsauce.co.uk/RouterCheck.zip

This is the COM server and a script to reboot a Draytek router. The script RouterCheck.wsf checks if it can ping a couple of test hosts, so you'll probably want to ignore most of the code. Just use the RebootRouter function and ignore the rest. This is at the end of my ADSL line so the download may be a bit slow.

If anyone wants the source (Visual C++ 5.0 with ATL) then I can drop it on SourceForge.

John Rennie
  • 7,776
  • 1
  • 23
  • 35
3

You could install Perl and use Net::Telnet, which lets you both send and interpret the received data.

ActivePerl is probably the quickest way to get up and running with this:

http://www.activestate.com/activeperl/

And Net::Telnet is documented here:

http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm

Further questions about how to code using Net::Telnet probably belong on StackOverflow. You may also want to look at www.perlmonks.org for examples and tips.

James F
  • 6,689
  • 1
  • 26
  • 24
1

I know this is a terribly old thread, but I came here from a related question and noticed that a very obvious answer was missed. Ever heard of putty? There's a command line scriptable version called plink.

Plink Howto Plink Executable

JamesBarnett
  • 1,129
  • 8
  • 12
0

If you don't mind a commercial solution, there a business telnet client called TeSSH, that has a rich scripting language, and can be run from the command-line. It has support for writting scripts in vbs, perl, and lua. It's fairly inexpensive at $34.95. You can also visit the TeSSH Support Forums.

Hector Sosa Jr
  • 433
  • 1
  • 6
  • 10
0

I created this batch script based on a previous answer:

@ECHO OFF
SET ME=from@example.com
SET TO=to@example.com
SET TF=%TEMP%\%~n0.vbs
(
 ECHO Set sh = WScript.CreateObject^("WScript.Shell"^)
 ECHO sh.SendKeys "HELO %COMPUTERNAME%~"
 ECHO sh.SendKeys "MAIL FROM: %ME%~"
 ECHO sh.SendKeys "RCPT TO: %TO%~"
 ECHO sh.SendKeys "DATA~"
 ECHO sh.SendKeys "From: %ME%~"
 ECHO sh.SendKeys "To: %TO%~"
 ECHO sh.SendKeys "Subject: Example subject...~"
 ECHO sh.SendKeys "~"
 ECHO sh.SendKeys "Contents...~"
 ECHO sh.SendKeys ".~"
 ECHO sh.SendKeys "QUIT~"
 ECHO WScript.Sleep 1000
 ECHO sh.SendKeys "~"
) > "%TF%"
START "" TELNET.EXE SMTPSERVER 25
CSCRIPT //NoLogo "%TF%"
DEL "%TF%"
Michel de Ruiter
  • 167
  • 1
  • 10