Shell script to test SMTP mail by FTP

November 5, 2010 at 11:30:32
Specs: Red Hat Linux
PLEASE DISREGARD! Manual steps involved telnet and not FTP as title suggests. The only helpful thing here is the post by nails that shows how to use command substitution.


Hello everyone,

I'm new to *NIX and wanted to write a script that would test SMTP mail via FTP (since the powers that be have removed or disabled sendmail from the servers for whatever reason). I have several servers that I need to split up and test between a few newbie administrators (including myself)! All the servers have a common mount where I can place this script and have my admin's run it.

Here is what I've got so far . . .

#!/bin/sh
REMOTESVR='mail.domain.com 25'
hostname > LOCALSVR

ftp -v $REMOTESVR << END_SCRIPT
ehlo domain.com
mail from:me@domain.com
rcpt to:me@domain.com notify=success,failure
data
Subject: Test email from server: $LOCALSVR
This is a test email message from $LOCALSVR
.
quit
END_SCRIPT
exit 0

The firs thing that it wrong is the line . . .

hostname > LOCALSVR

I'm trying to get the output from the "hostname" command in to a variable "LOCALSVR" and right now it dumps it to a file.

The second part is probably a misunderstanding on my part. When I run the script above I get the following output.

$ ./email_test.sh
Connected to mail.domain.com.
220 mail.domain.com ESMTP Sendmail Switch-3.1.11/Switch-3.1.11; Fri, 5 Nov 2010 08:33:16 -0700 for info Postmaster@Domain.Com
504 5.3.3 AUTH mechanism GSSAPI not available
504 5.3.3 AUTH mechanism KERBEROS_V4 not available
KERBEROS_V4 rejected as an authentication type
Name (mail.domain.com:account): 500 5.5.1 Command unrecognized: "USER ehlo domain.com"
Login failed.
?Invalid command
?Invalid command
?Invalid command
?Invalid command
?Invalid command
221 2.0.0 mail.domain.com closing connection

So I can see where it's trying to use the first command as USR variable to log in to FTP, however, when I run the steps manually I receive no such log in prompt.

Do I just have the syntax incorrect and perhaps missing a flag to tell FTP not to authenticate or is it when I do it manually I'm passing through my credentials without my knowledge?

Any help is very much appreciated!


See More: Shell script to test SMTP mail by FTP

Report •

#1
November 5, 2010 at 11:41:33
To get the output of a unix command into a variable, use command substitution:

LOCALSVR=`hostname`

That is a back-tic - not a single quote


Report •

#2
November 5, 2010 at 12:04:42

Thank you nails! I've made that correction in the script.

New script looks like:

#!/bin/sh
REMOTESVR='mail.domain.com 25'
LOCALSVR=`hostname`

ftp -v $REMOTESVR << END_SCRIPT
ehlo domain.com
mail from:me@domain.com
rcpt to:me@domain.com notify=success,failure
data
Subject: Test email from server: $LOCALSVR
This is a test email message from $LOCALSVR
.
quit
END_SCRIPT
exit 0

Now I just need to solve the authentication problem and away I go!


Report •

#3
November 5, 2010 at 12:23:16
I have not used ftp a lot, but I think it is used mostly for transferring files. I am not aware that you can execute commands on a remote server.

I know you can execute commands on the local server within a ftp shell using !

See this link:

http://www.linux.org/docs/ldp/howto...

Of course, there might be an extended version of ftp that I am not aware of. Have you considered something like secure shell to do your tests?


Report •

Related Solutions

#4
November 5, 2010 at 13:08:50
You know when your walking to the store and you get there and forget why you are there?

Yes, one of those moments...

I'm pretty upset with myself right now. When running manually? Yep, I'm using telnet. *smacks head* I don't know why I got FTP in my head when writing the script.

Well, thank you for teaching me about command substitution. At least I did learn something!


Report •

#5
November 5, 2010 at 13:22:54
No problem. We're all here to learn. :)

So let's take command substitution one step further. The back tic method was developed for the Bourne shell:

LOCALSVR=`hostname`

Modern shells like ksh and bash, use this method:

LOCALSVR=$(hostname)

Of course, the back tic method also works with the modern shells.


Report •

#6
November 5, 2010 at 13:32:38
That is very helpful, thank you very much nails! I have a few more scripts that will be a bit more robust using this. Much appreciated!

I'm sad to say that it looks like you can't automate telnet very readily. Ah, well... They'll just have to run the manual steps! If I find a unique way to actually automate the telnet session I'll post it here!


Report •

#7
November 8, 2010 at 10:20:02
You are right about not being able to automate telnet readily. You might consider using Expect. Expect is great for automating interactive applications - such as when a command requires a password. Check out this thread:

http://www.computing.net/answers/un...

It contains an example using Expect. Since you using Red Hat Linux, you probably already have it installed.

Let me know if you have any other questions.


Report •

Ask Question