Nov 152011

Someone asked me if i can make a perl scripts that can change the ip address based on time interval, say he want ip address used within one hour, if done next ip address will be used within next one hour..and so on. when it came to highest number of ip address in array, they will be reset back to the start. first i suggest him to look at the articles i wrote. But then i decide to write Perl script which was made for the purposes mention above.

here we are..

Postfix section: inet  n       n       n       -       0      spawn
          user=nobody argv=/etc/postfix/

ip1  unix -       -       n       -       -       smtp
          -o syslog_name=postfix-ip1
          -o smtp_bind_address=

ip2  unix -       -       n       -       -       smtp
          -o syslog_name=postfix-ip2
          -o smtp_bind_address=

ip3  unix -       -       n       -       -       smtp
          -o syslog_name=postfix-ip3
          -o smtp_bind_address=

ip4  unix -       -       n       -       -       smtp
          -o syslog_name=postfix-ip4
          -o smtp_bind_address=

transport_maps = tcp:[]:2527 = 3600s

Perl section:

use strict;
use warnings;
use Storable;

## interval between ip switchover, one hour for example
my $ip_interval=3600;

my $hashfile="data.hash";
store {}, $hashfile unless -r $hashfile;

my @smtp_array = (
# and more ips

# Autoflush standard output.
select STDOUT; $|++;

while (<>) {
        my $time_stamp = retrieve($hashfile);
        my $now = time();

        if (!defined $time_stamp->{'time_diff'}) {
                $time_stamp->{'time_diff'} = $now;
                $time_stamp->{'ip_index'} = 0;
                store $time_stamp, $hashfile;

        if (/^get\s(.+)$/i) {
                if ($now > $time_stamp->{'time_diff'} + $ip_interval) {
                        if ($time_stamp->{'ip_index'} < scalar(@smtp_array) - 1) {
                        } else {
                                $time_stamp->{'ip_index'} = 0;
                        print "200 $smtp_array[$time_stamp->{'ip_index'}]\n";
                        $time_stamp->{'time_diff'} = $now;
                        store $time_stamp, $hashfile;
                } else {
                        print "200 $smtp_array[$time_stamp->{'ip_index'}]\n";

        ## default response 
        print "200 smtp:\n";

Good luck.
PS: not tested in real environtment

  19 Responses to “Postfix Changing Outgoing IP By Time Interval Using TCP_TABLE And Perl”

Comments (19)
  1. hi bro, great tutorial but when i send mail, on header just apper per example: aways! but the ips are rotating.

    How can fix the postfix to show received from ip XX.XX.XX.XX

    after received from and goes like this.

    I try to find this out on google but without sucess can you helps me ?


  2. i’ve tested on my dev server, there’s nothing wrong. forward amd reverse hostname looks good.
    you might want take a look at your dns forward and reverse mapping.

    Jun 10 13:43:57 smtp2 postfix-rotate14/smtp[5582]: Host offered STARTTLS: []
    Jun 10 13:43:59 smtp2 postfix-rotate14/smtp[5582]: 3gnhYy3QvCz1GRZM: to=,[]:25, delay=153, delays=0/150/1.2/1.9, dsn=2.0.0, status=sent (250 2.0.0 OK 1402382639 er8si1794384pad.81 – gsmtp)

    Received: from ( [])
    by with ESMTP id er8si1794384pad.81.2014.
    for ;
    Mon, 09 Jun 2014 23:43:58 -0700 (PDT)

    # dig -x +short
    # dig a +short

  3. cool man! i buy a new vps today for re setting up everything!

    whats the best linux for it?

    i try to using zpanel with centos 6 but i cant recompile using tcp tables 🙁

    do you recomend some other linux distro for it?

    i prefer the zpanel for easily create new mail acounts, webmail etc…

    thanks in advance man!

  4. i’m using centos too, have you trying to commented out PreReq line in postfix.spec (/rpmbuild/SPECS/ or /usr/src/redhat/SPECS/ directpry), i know that would break dependencies requirement, just make sure you have chkconfig, initscripts, shadow-utils installed.

  5. Please cant run this script i keep getting this error when i try to test it;

    “postmap: warning: read TCP map reply from malformed reply: can’t create data.hash: Permission denied at /etc/postfix/ line 19
    postmap: fatal: table tcp: query error: Operation now in progress

    can help fix ??


 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>