Postfix Changing Outgoing IP By Time Interval Using TCP_TABLE And Perl

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 1.2.3.4 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:

master.cf
127.0.0.1:2527 inet  n       n       n       -       0      spawn
          user=nobody argv=/etc/postfix/ip_by_time.pl

ip1  unix -       -       n       -       -       smtp
          -o syslog_name=postfix-ip1
          -o smtp_helo_name=smtp1.example.com
          -o smtp_bind_address=1.2.3.1

ip2  unix -       -       n       -       -       smtp
          -o syslog_name=postfix-ip2
          -o smtp_helo_name=smtp2.example.com
          -o smtp_bind_address=1.2.3.2

ip3  unix -       -       n       -       -       smtp
          -o syslog_name=postfix-ip3
          -o smtp_helo_name=smtp3.example.com
          -o smtp_bind_address=1.2.3.3

ip4  unix -       -       n       -       -       smtp
          -o syslog_name=postfix-ip4
          -o smtp_helo_name=smtp4.example.com
          -o smtp_bind_address=1.2.3.4
....
....

main.cf

transport_maps = tcp:[127.0.0.1]:2527
127.0.0.1:2527_time_limit = 3600s


Perl section:

#!/usr/bin/perl
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 = (
'ip1:',
'ip2:',
'ip3:',
'ip4:',
'ip5:',
'ip6:',
'ip7:',
'ip8:',
'ip9:',
'ip:'
);
# and more ips

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

while (<>) {
        chomp;
        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) {
                                $time_stamp->{'ip_index'}++;
                        } else {
                                $time_stamp->{'ip_index'} = 0;
                        }
                        print "200 $smtp_array[$time_stamp->{'ip_index'}]\n";
                        $time_stamp->{'time_diff'} = $now;
                        store $time_stamp, $hashfile;
                        next;
                } else {
                        print "200 $smtp_array[$time_stamp->{'ip_index'}]\n";
                        next;
                }
        }

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

Good luck.
PS: not tested in real environtment

14 Comments

    • try put header_checks in your main.cf to discard certain information

      header_checks = regexp:/etc/postfix/header_checks
      

      /etc/postfix/header_checks

      /^Received:.*s22\.vangxa1\.com.*/ IGNORE
      
      • Hieu Tran

        Thank you.
        But I want to use SMTP with IP 184.172.219.242 (s22.vangxa1.com) & remove the main IP information 173.193.98.197 (s27.vangxa1.com). How to make it show the information 184.172.219.242 (s22.vangxa1.com) only?

  1. Hieu Tran

    Thank you. But I want to use SMTP with IP 184.172.219.242 (s22.vangxa1.com) & remove the main IP information 173.193.98.197 (s27.vangxa1.com). How to make it show the information s22.vangxa1.com only?

  2. Thanh

    Hello

    I configured follow your instruction. Outgoing mail is ok. But incoming mail, I receive error: “mail for example.com loops back to myself”

    If I use “transport_maps = hash:/etc/postfix/transport” there is no error there.

    Can you fix it??

    • there’s a quick workaround on comment section regarding “mail for example.com loops back to myself” error. example.com has to be in exception.

      • Thanh

        example.com is just an example of mydomain.com.

        Our server use zpanel as control panel, I tried too many way but it still can’t receive mail.

        Please help me.

        • as i said, you need to exclude your local/virtual domain in order not to get affected by the script. says, your domain is example.com and your maildir is virtual:

          while (<>) {
                  chomp;
                  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) {
                     my $local_domain = $1;
                     $local_domain =~ s/.*\@//i;
                     if ($local_domain eq "example.com") {
                          print "200 virtual:\n";
                          next;
                     } else {
                          if ($now > $time_stamp->{'time_diff'} + $ip_interval) {
                                  if ($time_stamp->{'ip_index'} < scalar(@smtp_array) - 1) {
                                          $time_stamp->{'ip_index'}++;
                                  } else {
                                          $time_stamp->{'ip_index'} = 0;
                                  }
                                  print "200 $smtp_array[$time_stamp->{'ip_index'}]\n";
                                  $time_stamp->{'time_diff'} = $now;
                                  store $time_stamp, $hashfile;
                                  next;
                          } else {
                                  print "200 $smtp_array[$time_stamp->{'ip_index'}]\n";
                                  next;
                          }
                     }
                  }
          
                  ## default response 
                  print "200 smtp:\n";
          }
          

          it’s not tested, but i hope you can figure it out

          cheer

  3. hi bro, great tutorial but when i send mail, on header just apper per example: mx01.mydomain.com aways! but the ips are rotating.

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

    after received from mx03.mydomain.com and goes like this.

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

    Thanks!
    Ronan

    • 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.

      log
      Jun 10 13:43:57 smtp2 postfix-rotate14/smtp[5582]: Host offered STARTTLS: [gmail-smtp-in.l.google.com]
      Jun 10 13:43:59 smtp2 postfix-rotate14/smtp[5582]: 3gnhYy3QvCz1GRZM: to=, relay=gmail-smtp-in.l.google.com[173.194.79.27]: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)

      header
      Received: from smtp14.mydomain.net (smtp14.mydomain.net. [202.xxx.xxx.53])
      by mx.google.com with ESMTP id er8si1794384pad.81.2014.06.09.23.43.57
      for ;
      Mon, 09 Jun 2014 23:43:58 -0700 (PDT)

      # dig -x 202.xxx.xxx.53 +short
      smtp14.mydomain.net.
      # dig a smtp14.mydomain.net +short
      202.xxx.xxx.53

  4. 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!

    • 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.

Leave a Reply

Your email address will not be published. Required fields are marked *