Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Secure POP and SMTP Email via SSH (final update)' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Secure POP and SMTP Email via SSH (final update)
Authored by: jeffiel on Jul 23, '03 12:32:40PM
I've been doing this for a while, and wrote a shell script to automate the starting and stopping of the tunnel. FYI, you don't want mutliple copies of the tunnel running simultaneously, so this script can also be used to start, stop, and restart the tunnel properly.
#!/bin/bash

MAILHOST='YOUR_MAIL_HOST'
USERNAME='YOUR_MAIL_ACCOUNT'

# to use public/private key file for authentication, provide the filename of your private key here
PRIVATE_KEY_FILE='PATH_TO_A_PRIVATE_KEY'

if [ -n "$PRIVATE_KEY_FILE" ]
then
       PRIVATE_KEY_FILE="-i $PRIVATE_KEY_FILE"
fi

if [ -n "$1" ]
then
        do=$1
else
        do=start
fi;

case "$do" in
        start)

                PID=`ps -aux | grep "ssh -l $USERNAME $PRIVATE_KEY_FILE -L 2025:127.0.0.1:25" | grep -v 'grep' |  awk '{print $2
}'`
                if [ "${PID}" != "" ]; then
                  kill $PID
                  echo "Stopped process" $PID
                fi

                ssh -l $USERNAME $PRIVATE_KEY_FILE -L 2025:127.0.0.1:25 -L 2143:127.0.0.1:143 -N -f $MAILHOST

                PID=`ps -aux | grep "ssh -l $USERNAME $PRIVATE_KEY_FILE -L 2025:127.0.0.1:25" | grep -v 'grep' |  awk '{print $2
}'`
                if [ "${PID}" != "" ]; then
                  echo "Started new process" $PID
                else
                        echo "Failed to restart"
                        exit 0
                fi


        ;;

        stop)
                PID=`ps -aux | grep "ssh -l $USERNAME $PRIVATE_KEY_FILE -L 2025:127.0.0.1:25" | grep -v 'grep' |  awk '{print $2
}'`
                if [ "${PID}" != "" ]; then
                  kill $PID
                  echo "Stopped process" $PID
                else
                  echo "Failed to find PID"
                fi
                exit 0
        ;;

        *)
                echo "Usage (start|stop)"

        ;;
esac


Usage notes:
  • Called with no args, it will start (or restart) the tunnel
  • Called with the argument stop will kill the tunnel
  • Make sure to chmod u+x the file after you save it, preferably to a bin directory in your path, such as /usr/bin/ /usr/local/bin etc..
  • You can add an optional keyfile to use public/private key encryption instead of a password. Setting up key encryption with SSH is a pain in the ass, and your ISP may or may not allow it. Seek other references on setting this up.


[ Reply to This | # ]
Secure POP and SMTP Email via SSH (final update)
Authored by: jeffiel on Jul 23, '03 12:34:12PM

Oh yes, and my scripts uses local ports 2025 and 2143. Make changes to your mail client appropriately.



[ Reply to This | # ]
Secure POP and SMTP Email via SSH (final update)
Authored by: kakilaki on Nov 18, '04 12:13:12AM

Hi,

I used your code to try and open a port and send mail from Mail on OS X 10.3 via my University email address. I used it to log onto my desktop in Uni as the main mail server does not listen for ports. However, whenever I try and send mail it comes up with the same error as if I wasn't using port forwarding. Is this something I have to set up in Mail or is the University blocking this option and how do I check?

Thanks very much



[ Reply to This | # ]