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

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

Comments (19)
  1. try to change owner of data.hash. chown nobody: data.hash

  2. thanks but the data.hash file doesn’t exist, it is the script that is suppose to create it and i keep getting access denied. i even made the user nobody a root user in the sudoers file, still,., any idea what next?

  3. touch /etc/postfix/data.hash && chown nobody: /etc/postfix/data.hash

  4. postmap: warning: read TCP map reply from localhost:2527: malformed reply: Magic number checking on storable file failed at /usr/lib64/perl5/vendor_perl/Storable.pm line 381,
    postmap: fatal: table tcp:localhost:2527: query error: Operation now in progress

    I am using CentOS 7.4. Could u help anyone.

 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>

(required)

(required)

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.