Is it possible to pass a series of commands to a telnet session from a
text file? eg. using a command like "telnet <commands.txt", where
commands.txt would consist of a series of commands.
The purpose of this is to automate a telnet session on port 25 (the
SMTP port) of a mail server. My commands.txt file looks something like
this:
open mailserver.mydomain.com 25
helo myclient.mydomain.com
mail from:m...@mydomain.com
rcpt to:y...@yourdomain.com
data
.
quit
When I try this it gets as far as opening the connection, but then the
remote server just breaks it immediately. Doing the above sequence of
commands manually works just fine.
Any help or pointers appreciated - I'm trying to incorporate this into
a shell script (BASH) if that makes any difference.
Cheers,
Rich.
You want "expect". It's a scripting language designed to automate
interactive programs like telnet. There's a tutorial at the URL below:
http://www.raycosoft.com/rayco/support/expect_tutor.html
You can also do what you want by using Perl's Net::Telnet module, I'm
sure, but if you want straight bash, it's probably not going to happen
unless you monkey with "netcat" quite a bit. HTH, good luck....
--
Matt G|There is no Darkness in Eternity/But only Light too dim for us to see
Brainbench MVP for Linux Admin / Those who do not understand Unix are
http://www.brainbench.com / condemned to reinvent it, poorly.
-----------------------------/ --Henry Spencer
Use expect. Here's a sample expect script. This won't handle any
problems with the delivery but should give you a basic idea of how
to use it.
#!/usr/bin/expect -f
spawn telnet
expect "telnet>"
send "open mailserver.mydomain.com 25\n"
expect "Connected to "
send "helo myclient.mydomain.com\n"
expect "250"
send "mail from:m...@mydomain.com\n"
expect "Sender ok"
send "rcpt to:y...@yourdomain.com\n"
expect "Recipient ok"
send "data\n"
expect "itself"
send ".\n"
expect "Message accepted for delivery"
send "quit\n"
expect "foreign host."
Well, if you're wanting to send mail, use sendmail (provided you have it
installed and running). The -t option allows you to specify thru STDIN
all the headers and data and everything.
Something like:
---BEGIN BLOCK---
$> sendmail -t
To: y...@your-mail.net
From: m...@my-mail.net
Content-Type: text/plain
Subject: sending thru sendmail
This really does work, honest!
.
$>
---END BLOCK---
will work. Just end your message body with a single period. You can
include as many or as little message headers as you like ("To:" is
definitely required). This can be scripted from a shell script or perl
script or just about any language where you can send a stream of data to
a command (piping). I think there is an option to input into sendmail a
pre-typed, pre-formatted text file that contains all headers and message
body. I can't remember what it was off the top of my head though. You
could probably do
sendmail -t < message.txt
or even
cat message.txt | sendmail -t
where message.txt is like the block from above (sans command prompts and
message-ending period). [I haven't tried either of these two commands,
but they may get you in the right direction.]
And if all else fails,
man sendmail
Hope this helps! :o)
Stephen.
--
Stephen Hui, ARL:UT, Austin, Texas
Computer Terms: Programmer - A red-eyed, mumbling mammal
capable of conversing with inanimate objects.
Yes, use "expect"
See http://pcunix.com/Books/expect.html and, of course, "man
expect"
That's the general solution for controlling telnet, but for
your specific question
> The purpose of this is to automate a telnet session on port 25 (the
> SMTP port) of a mail server. My commands.txt file looks something like
> this:
you could use sendmail directly or even just call one of the
command-line mailers like mutt, fastmail, mailto etc. -
"apropos mail" will give you a list of what you have
immediately available and there's many more out there
including Perl modules and on and on and on..
--
Tony Lawrence (to...@aplawrence.com)
Linux articles, help, book reviews, tests,
job listings and more : http://www.pcunix.com/Linux
http://www.columbia.edu/kermit/ckermit.html
This has certain advantages over the Telnet/Expect combination which seems
to be the popular knee-jerk reaction to this question:
. If Expect is used to drive Telnet or Rlogin, it can't tell the
difference between local and remote text.
. Kermit knows whether each command succeeded or failed -- not just
INPUT ("expect"), but every command -- and can take any desired action
depending on the success or failure of each command.
. Expect has no access to the connection itself and so can't tell
if it's up or down, what state it's in, etc. For example, on a
serial connection, Kermit can check for specific modem signals.
. Expect can't transfer files.
. Kermit runs on a wider variety of platforms than expect.
Look here:
http://www.columbia.edu/kermit/ckscripts.html
for sample Kermit scripts. In particular see "autotelnet" in the Internet
scripts section.
- Frank