Relaying using a Dynamic IP

by Igloo on Jan 16th in Uncategorized

Sometimes you need to send email from a remote site, On a dynamic IP range that is blocked by mail servers for being a possible relay.

Here is what i’ve used to combat this problem using Postfix and some bash!

#! /bin/bash
IP=`<hosttobecheckedforitsipforrelaying> | sed 's/^[ ]*//g' | cut -d ' ' -f 4`
echo mynetworks = <yourserverip>/32, 127.0.0.0/8, $IP/32, > /etc/postfix/mynetworks
/etc/init.d/postfix reload

that writes the pattern file to /etc/postfix/mynetworks and then just does a simple reload on it :)

You could have as many hosts as you need and just ammend the line that outputs the pattern file accordingly.

I’ve setup a cron file to run this every 5 mins so that if remote site changes IP it’ll get updated *fairly* quickly!

*/5 * * * * /etc/postfix/getmynetworks.sh &> /dev/null

Then comment out the following in the main.cf file

# Not needed due to script getting my networks
# mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mynetworks = /etc/postfix/mynetworks

restart postfix

/etc/init.d/postfix restart

And you’re done :)

Be Sociable, Share!

5 Comments

  1. Rolf

    6th July 2010

    Hey, that’s exactly what I searched for! Thanks a lot!

  2. Dale

    14th September 2010

    I’ve been searching for this solution for hours now. In your example I assume the portion that determines the current IP address requires a command like ping perhaps?

    IP=`
    ping hostnametobechecked

    Is that correct?

  3. Dale

    14th September 2010

    Well I found my own answer by starting to use the following

    IP=`host -A hostnametobechecked.com | sed ‘s/^[ ]*//g’ | cut -d ‘ ‘ -f 4`
    echo mynetworks = /32, 127.0.0.0/8, $IP/32,

  4. Igloo

    20th September 2010

    Dale,

    You do not need to do any kind of ping request.

    Say for example your host is

    blog.leech0r.co.uk which has ip 89.238.172.245

    This is the script you would use:

    #! /bin/bash
    IP=`blog.leech0r.co.uk | sed ‘s/^[ ]*//g’ | cut -d ‘ ‘ -f 4`
    echo mynetworks = 123.456.789.0/32, 127.0.0.0/8, $IP/32, > /etc/postfix/mynetworks
    /etc/init.d/postfix reload

    which would output the following to /etc/postfix/mynetworks:

    mynetworks = 123.456.789.0/32, 127.0.0.0/8, 89.238.172.245/32

    Hope that clears it up :)

    (123.456.789.0 is the server IP of your mail server, which should be needed by the way!)

Leave a Reply

*

Powered By Wordpress Designed By Ridgey