script for telnet on port 25

  • Follow


Hi Gurus,
I am trying to write a shell script which keeps checking for smtp
server with "telnet smtp.test.com 25"
but it hangs and doesn't come out.
The purpose is to grep for code 220 with telnet smtp.test.com 25  (the
requirement is to grep for code
220 instead of cheking for port 25 or sendmail) and if its not 220 just
send out an error mail.

The output looks like below:
telnet smtp.test.com 25
Trying 10.1.0.1...
Connected to smtp.test.com.
Escape character is '^]'.
220 smtp.test.com ESMTP Sendmail Switch-; Thu, 11 Jan 2007 13:01:11
-0800
quit

Unless i type quit it hangs there and can't grep 220. I need to do this
for multiple mail servers with for loop in the shell script.
Please Help!!!
Regards

P.S. I tried to post this in comp.unix.shell group but somehow i can't.
So posting it here..

0
Reply bshah (26) 1/11/2007 9:14:36 PM

<bshah@citadon.com> wrote in message 
news:1168550076.575709.214100@k58g2000hse.googlegroups.com...
> Hi Gurus,
> I am trying to write a shell script which keeps checking for smtp
> server with "telnet smtp.test.com 25"
> but it hangs and doesn't come out.
> The purpose is to grep for code 220 with telnet smtp.test.com 25  (the
> requirement is to grep for code
> 220 instead of cheking for port 25 or sendmail) and if its not 220 just
> send out an error mail.
>
> The output looks like below:
> telnet smtp.test.com 25
> Trying 10.1.0.1...
> Connected to smtp.test.com.
> Escape character is '^]'.
> 220 smtp.test.com ESMTP Sendmail Switch-; Thu, 11 Jan 2007 13:01:11
> -0800
> quit
>
> Unless i type quit it hangs there and can't grep 220. I need to do this
> for multiple mail servers with for loop in the shell script.
> Please Help!!!
> Regards
>
> P.S. I tried to post this in comp.unix.shell group but somehow i can't.
> So posting it here..
>


echo quit | telnet server 25


0
Reply Jay 1/11/2007 10:01:32 PM


Hi Jay,
Thanks for the reply. I did tried that but still it doesn't spit the
required information the line with code 220 is still not printed.
"220 smtp.test.com ESMTP Sendmail Switch-; Thu, 11 Jan 2007 13:01:11"
is not printed in the output.

> 
> echo quit | telnet server 25

0
Reply bshah 1/12/2007 12:45:47 AM

bshah@citadon.com wrote:
> Hi Jay,
> Thanks for the reply. I did tried that but still it doesn't spit the
> required information the line with code 220 is still not printed.
> "220 smtp.test.com ESMTP Sendmail Switch-; Thu, 11 Jan 2007 13:01:11"
> is not printed in the output.
> 
>> echo quit | telnet server 25
> 

using shell for this is about as bad a choice as possible.
interactivity and error catching are really lame for shell, even good
shells like zsh and bash.  you should really use a language that has an
interactive scope like perl or ruby.  if you're really committed to
this, the following (called chatterbox.sh) will work:

#/bin/sh
echo "helo"
sleep 2
echo "quit"

then, do this:

chatterbox.sh | telnet mailhost.example.com 25

that's it.  the output from chatterbox.sh piped into telnet will make
the smtp server salute and the sleep will usually give the smtp server
enough time to emit the 220.  you can capture that output and react to
it however you want, including a grep for "220".

0
Reply ISO 1/12/2007 4:38:15 AM

b=2E..@citadon.com =DD=E3=F1=E1=F8=E5:
> Hi Gurus,
> I am trying to write a shell script which keeps checking for smtp
> server with "telnet smtp.test.com 25"
> but it hangs and doesn't come out.
> The purpose is to grep for code 220 with telnet smtp.test.com 25  (the
> requirement is to grep for code
> 220 instead of cheking for port 25 or sendmail) and if its not 220 just
> send out an error mail.
>
> The output looks like below:
> telnet smtp.test.com 25
> Trying 10.1.0.1...
> Connected to smtp.test.com.
> Escape character is '^]'.
> 220 smtp.test.com ESMTP Sendmail Switch-; Thu, 11 Jan 2007 13:01:11
> -0800
> quit
>
> Unless i type quit it hangs there and can't grep 220. I need to do this
> for multiple mail servers with for loop in the shell script.
> Please Help!!!
> Regards
>
> P.S. I tried to post this in comp.unix.shell group but somehow i can't.
> So posting it here..

You can do this in awk to (after all, it's awk's group).
Try with gawk 3.1.5 (unix or cygwin)

#!/bin/awk -f

BEGIN {
        Server =3D "smtp.test.com"
	MailService =3D "/inet/tcp/0/" Server "/smtp"

        # don't know smtp commands
        # you know what to send
	print "hello" |& MailServer

	while ((MailServer |& getline) > 0)
		if (!/^220/)
			print "error: got", $1
		else
			print $0

	print "quit" |& MailServer
	close(MailServer)
	exit
}

I didn't try it though, don't have the means.

0
Reply Vassilis 1/12/2007 4:59:14 PM

On 2007-01-12, Mister Sch�fer <mister@sonicpond.com> wrote:
> bshah@citadon.com wrote:
>> Hi Jay,
>> Thanks for the reply. I did tried that but still it doesn't spit the
>> required information the line with code 220 is still not printed.
>> "220 smtp.test.com ESMTP Sendmail Switch-; Thu, 11 Jan 2007 13:01:11"
>> is not printed in the output.
>
> using shell for this is about as bad a choice as possible.
> interactivity and error catching are really lame for shell, even good
> shells like zsh and bash.  you should really use a language that has an
> interactive scope like perl or ruby.  if you're really committed to
> this, the following (called chatterbox.sh) will work:

There is a telnet client specifically designed with scripting in
mind.  It's called xt and you can download it from
ftp.jpr.com/pub/xt-1.2.tgz.

I have a set of porting patches for NetBSD and probably other BSDs
that I can forward on request.

-- 
Andrew Smallshaw
andrews@sdf.lonestar.org
0
Reply Andrew 1/12/2007 5:16:42 PM

I agree, gawk may work for you.  I'm not exactly sure what you're
trying to do,
but, for example, this works:

bash-3.00$ gawk 'BEGIN { srv = "/inet/tcp/0/a.mx.mail.yahoo.com/smtp";
srv |& getline x; print x}'
220 mta483.mail.mud.yahoo.com ESMTP YSmtp service ready

Regards,
Andy

0
Reply Andrew 1/12/2007 7:50:06 PM

In article <1168621154.326490.105530@38g2000cwa.googlegroups.com>,
Vassilis <F.H.Novalis@gmail.com> wrote:
>
>b...@citadon.com ������:
>> I am trying to write a shell script which keeps checking for smtp
>> server with "telnet smtp.test.com 25"
>> but it hangs and doesn't come out.
>> The purpose is to grep for code 220 with telnet smtp.test.com 25  (the
>> requirement is to grep for code
>> 220 instead of cheking for port 25 or sendmail) and if its not 220 just
>> send out an error mail.
>>
>> The output looks like below:
>> telnet smtp.test.com 25
>> Trying 10.1.0.1...
>> Connected to smtp.test.com.
>> Escape character is '^]'.
>> 220 smtp.test.com ESMTP Sendmail Switch-; Thu, 11 Jan 2007 13:01:11
>> -0800
>> quit
>>
>> Unless i type quit it hangs there and can't grep 220. I need to do this
>> for multiple mail servers with for loop in the shell script.
>> Please Help!!!
>> Regards
>
>You can do this in awk to (after all, it's awk's group).

You can do it, but if any of your connections ever hangs, your script will
hang.  You really need timeouts to do anything like this right, and gawk
doesn't have them (yet).  To do it reliably, even in gawk, you're back to using
telnet.  A utility that does this (talking SMTP through telnet within gawk),
more than you need for this purpose but providing an example (search the code
for "telnet"), is at ftp://ftp.armory.com/pub/admin/awkmail 

	John
-- 
John DuBois  spcecdt@armory.com  KC6QKZ/AE  http://www.armory.com/~spcecdt/
0
Reply spcecdt 1/13/2007 6:45:36 PM

John DuBois wrote:
> You can do it, but if any of your connections ever hangs, your script will
> hang.  You really need timeouts to do anything like this right, and gawk
> doesn't have them (yet).

Good point.  It might be possible to add an alarm timer function to
xgawk
in the time extension, and that could solve the problem.  Currently,
the xgawk time library implements gettimeofday and sleep, so
it seems natural to add setitimer or ualarm or alarm.  Of course, at
the moment,
there would be no way to handle the signal, so it would simply kill
the xgawk process.  But that might be sufficient for some applications.
I'm not sure whether ignoring SIGALRM would give useful behavior...

Regards,
Andy

0
Reply Andrew 1/16/2007 2:10:50 PM

=CF/=C7 Andrew Schorr =DD=E3=F1=E1=F8=E5:
> John DuBois wrote:
> > You can do it, but if any of your connections ever hangs, your script w=
ill
> > hang.  You really need timeouts to do anything like this right, and gawk
> > doesn't have them (yet).
>
> Good point.  It might be possible to add an alarm timer function to
> xgawk
> in the time extension, and that could solve the problem.  Currently,
> the xgawk time library implements gettimeofday and sleep, so
> it seems natural to add setitimer or ualarm or alarm.  Of course, at
> the moment,
> there would be no way to handle the signal, so it would simply kill
> the xgawk process.  But that might be sufficient for some applications.
> I'm not sure whether ignoring SIGALRM would give useful behavior...
>
> Regards,
> Andy

You could also, more generally, implement signal or sigaction
(simplified really) as, let's say,

function signal(signum, sighandler) { ... }

where signum is the number associated with standard signals and
sighandler a "named" function, that is, the name of the function as
string.
I guess you have though of that too.

0
Reply Vassilis 1/16/2007 6:50:33 PM

Vassilis wrote:
> You could also, more generally, implement signal or sigaction
> (simplified really) as, let's say,
>
> function signal(signum, sighandler) { ... }
>
> where signum is the number associated with standard signals and
> sighandler a "named" function, that is, the name of the function as
> string.
> I guess you have though of that too.

Yes, there's been some discussion of adding signal handling to xgawk,
but this needs to be done carefully.  There is an existing xgawk
extension to do this here:

http://xgawk.radlinux.org/Articles/extension-signal/show

But it does not execute the signal handler in the proper context,
so I don't think it's safe to do much other than exit.

Regards,
Andy

0
Reply Andrew 1/17/2007 3:35:53 PM

10 Replies
842 Views

(page loaded in 1.402 seconds)

Similiar Articles:













7/20/2012 5:26:35 PM


Reply: