Operating System - HP-UX
1751976 Members
4746 Online
108784 Solutions
New Discussion юеВ

Re: telnet port 25 script

 
Steven Sim Kok Leong
Honored Contributor

Re: telnet port 25 script

Hi,

Try using expect. This works for me but you have to customize my script based on your (eg. not SST) mail server's returning messages.

I have stripped my script down for ease of customization as follows:

#!/usr/bin/expect

set timeout 10
spawn telnet a.b.c.d 25
expect_background -re .+
expect "(SST)"
send "mail from: a@x.y.z\n"
expect "Sender ok"
send "rcpt to: z@a.b.c\n"
expect "Recipient ok"
send "data\n"
expect "by itself"
send "Your message here.\n"
send ".\n";
expect "Message accepted for delivery"
close
Hope this helps. Regards.

Steven Sim Kok Leong
Jeff Schussele
Honored Contributor

Re: telnet port 25 script

If you're HP-UX system has sendmail running on it & been set to relay to that host you're trying to telnet to - then there's another way to do it.

To see if the above conditions are true try this:

#echo test | mailx -s "This is a test" mike.skora@hawker.com

the -s is for subject

If this gets thru then you can pipe whatever you want into mailx commands.

HTH,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Ron Kinner
Honored Contributor

Re: telnet port 25 script

The Ctrl C is just kiling your telnet session. Then it goes on to do the rest of your program which is why outlook picks it up.

First if you manually telnet to 193.0.9.27 25 does it answer?

If it answers, can you type in what you have in your script and does that work?

If the above are true then try rewriting your script to look more like:

(sleep 5
echo "username\r"
sleep 5
echo "password\r"
sleep 15
echo "date"
sleep 5
echo exit) |telnet otherboxipaddress portno

This is similar to what I use to talk to a 3Com switch. If I talk to a Cisco router I do not need the \r (I think I have the \ direction right. Working from my poor memory here. Could be /r. Not sure.) You have to substitute the appropriate values in each echo statement. The sleep statements are important and may need adjusting depending on how slow your remote system is. If, when you talk to the remote box, you have to give it an enter to wake it up that's echo "\r" or just echo.

Ron


Frank Slootweg
Honored Contributor

Re: telnet port 25 script

As others have mentioned, you need something like the 'expect' program to solve the (non-)handshaking issue. telnet(1) is intended for *interactive* use and does not work in 'batch-mode'.

Having said that, *why* are you using telnet(1) to the SMTP port? Why don't you use sendmail(1M) to do the work for you? You can just pipe the header lines, a blank 'separation' line and the body lines to "sendmail -t". Or do you need to control the envelope (sp?) in addition to the headers and body?
Peter Kloetgen
Esteemed Contributor

Re: telnet port 25 script

Hi,

you use the "!"- character as EOF- marker. In my opinion this could be the problem. Try another EOF- marker, like "EOF".

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Michael Skora
Advisor

Re: telnet port 25 script

Thanks to all for your suggestions.
Harry - I looked at your perl references, but got lost pretty quickly.
Steven - played with 'expect' but got lost there too.
Ron - manually entering each line of the script works - apparently since telnet is intended to be interactive only. Tried the sleep'ing suggestions but still couldn't get it to exit and complete.
Peter - tried a few other EOF marks, but with no success.
Jeff - 'mailx' fails because I messed up my sendmail configuration a while ago. That's why I was trying something "simple" like telnet.
Frank - telnet seemed like a good idea and easier than fumbling my way through the sendmail configuration. Apparently that's where I need to go though. Anyone know a good sendmail class I could attend?
Thanks again - I give.
Jeff Schussele
Honored Contributor

Re: telnet port 25 script

Michael,

Get the O'Reilly sendmail book - it's the "bat" book & while it's certainly comprehensive, it shouldn't be too over your head as long as you don't dwell on the rulesets - they've been known to make a person blind ;~). You want to focus on the configuration fields - EX: Setting=Value

And it explains the sendmail.cf & sendmail.cw files very well in addition.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Jeff Schussele
Honored Contributor

Re: telnet port 25 script

Michael,

And in case you do want to go the class route - here's a link:

http://www.sendmail.org/classes.html

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Frank Slootweg
Honored Contributor

Re: telnet port 25 script

If you only need this, i.e. things like the example in your original posting, then you do not need a sendmail class. As I mentioned, you can just feed the stuff to "sendmail -t". Just a small (partial) example:

{
echo "To: $TO"
echo "From: $FROM"
echo "Subject: $SUBJECT"
echo "Date: $DATE"
if [ ! -z "$MIME_Version" ]
then
echo "$MIME_Version"
fi
if [ ! -z "$Content_Type" ]
then
echo "$Content_Type"
fi
if [ ! -z "$boundary" ]
then
echo "$boundary"
fi
echo # Blank line because the body starts here:
cat /tmp/body$$
} | sendmail -t

I hope this helps.
Michael Skora
Advisor

Re: telnet port 25 script

Thanks again to all for your help. Finally resolved it by doing the right thing - getting sendmail to work.
1) '/etc/hosts' has to have the correct domainname aliases associated with the Exchange server ip.
2) '/etc/mail/sendmail.cf' needs the correct domainname (eg: Dj$w.hawker.com in my case).
3) and '/etc/mail/sendmail.cw' needed 3 entries: hawk01us, hawk01us.hawker, and hawk01us.hawker.com to be happy.
After restarting the sendmail daemon with '# /sbin/init.d/sendmail stop' and then '# /sbin/init.d/sendmail start' I was able to execute "# mailx -s 'Happy at last' mike.skora@hawker.com < filename.txt" and see it in my Outlook inbox. Now I can go home.