Mailscript with netcat: Unterschied zwischen den Versionen

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen
 
(5 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
 
<pre>
 
<pre>
 
#!/bin/bash
 
#!/bin/bash
# script to send test mail with netcat.
+
test "$#" -eq 5 || { echo "$0 SERVER PORT FROM TO SUBJECT" ; exit ; }
# expects the following arguments:
+
SAVE=$IFS
# 1. recepient mail server
+
FROM=$3
# 2. port (typically 25 or 465)
+
TO=$4
# 3. mail from (e.g. from@example.com)
+
SERVER=$1
# 4. mail to (e.g. to@example.com)
+
PORT=$2
   
+
SUBJECT=$5
# for mail_input function
+
IFS=" "
from=$3
+
DATA=$(cat)
to=$4
+
function mail()
+
{
# error handling
+
cat<<HERE
function err_exit { echo -e 1>&2; exit 1; }
+
ehlo $(hostname -f)
+
MAIL FROM: <$FROM>
# check if proper arguments are supplied
+
RCPT TO: <$TO>
if [ $# -ne 4 ]; then
+
DATA
  echo -e "\n Usage error!"
+
From: <$FROM>
  echo " This script requires four arguments:"
+
To: <$TO>
  echo " 1. recepient mail server"
+
Subject: $SUBJECT
  echo " 2. port (typically 25 or 465)"
+
 
  echo " 3. mail from (e.g. from@example.com)"
+
$DATA
  echo " 4. mail to (e.g. to@example.com)"
+
.
  exit 1
+
quit
fi
+
HERE
 
# create message
 
function mail_input {
 
  echo "ehlo $(hostname -f)"
 
  echo "MAIL FROM: <$from>"
 
  echo "RCPT TO: <$to>"
 
  echo "DATA"
 
  echo "From: <$from>"
 
  echo "To: <$to>"
 
  echo "Subject: Testing one two three"
 
  echo "This is only a test. Please do not panic. If this works, then all is well, else all is not well."
 
  echo "In closing, Lorem ipsum dolor sit amet, consectetur adipiscing elit."
 
  echo "."
 
  echo "quit"
 
 
}
 
}
+
mail | nc $SERVER $PORT
# test
+
IFS=$SAVE
#mail_input
 
 
# send
 
mail_input | nc $1 $2 || err_exit
 
 
</pre>
 
</pre>

Aktuelle Version vom 12. September 2014, 09:54 Uhr

#!/bin/bash
test "$#" -eq 5 || { echo "$0 SERVER PORT FROM TO SUBJECT" ;  exit ; }
SAVE=$IFS
FROM=$3
TO=$4
SERVER=$1
PORT=$2
SUBJECT=$5
IFS=" "
DATA=$(cat)
function mail()
{
cat<<HERE
ehlo $(hostname -f)
MAIL FROM: <$FROM>
RCPT TO: <$TO>
DATA
From: <$FROM>
To: <$TO>
Subject: $SUBJECT

$DATA
.
quit
HERE
}
mail | nc $SERVER $PORT
IFS=$SAVE