Revision: 10608
Updated Code
at January 5, 2009 18:57 by paramah
Updated Code
#!/usr/bin/perl
#
#
#
# CREATE TABLE `redirects` (
# `id` int(11) NOT NULL auto_increment,
# `ip` varchar(15) collate utf8_polish_ci NOT NULL,
# `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
# `choice` varchar(10) collate utf8_polish_ci NOT NULL,
# PRIMARY KEY (`id`),
# UNIQUE KEY `ip` (`ip`)
#) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci AUTO_INCREMENT=3395 ;
#
#
use Sys::Syslog qw(:DEFAULT setlogsock);
use DBI;
use Net::IP::Match::Regexp qw( create_iprange_regexp match_ip );
$dbase = DBI->connect("DBI:mysql:database=DBNAME;host=DBHOST","DBUSER","DBPASS", { RaiseError => 1 });
$utsfmt = "UNIX_TIMESTAMP()";
my $net1 = create_iprange_regexp( qw( 10.10.0.0/24 10.10.16.0/24 ) );
my $net2 = create_iprange_regexp( qw( 10.21.0.0/17 10.21.128.0/17 ) );
openlog("redirector");
setlogsock('unix');
syslog('info', 'Script started.');
$|=1;
while (<>)
{
@X = split;
my $url = $X[0];
my $oul = $url;
@uri = split(///,$url);
@ip = split(///,$X[1]);
my $sdbq = $dbase->prepare("SELECT * FROM redirects WHERE ip='".$ip[0]."' AND choice IN ('0','1','3')");
$sdbq->execute();
if ($sdbq->rows == 0)
{
if (match_ip($ip[0], $net1))
{
$url="http://redirect_host:port/net1/".$uri[3];
}
elsif(match_ip($ip[0], $net2))
{
$url="http://redirect_host:port/net2/".$uri[3];
}
else
{
$url="http://redirect_host:port/rest/".$uri[3];
}
$action ="New";
$all = "[".$ip[0]."] ".$url."[".$action."] - ".$oul." - ".$uri[3]."n";
syslog('info', $all);
print "$urln";
}
else
{
$action ="In database";
$all = "[".$ip[0]."] ".$url."[".$action."] - ".$oul." - ".$uri[3]."n";
syslog('info', $all);
print;
}
}
closelog();
Revision: 10607
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at January 5, 2009 18:50 by paramah
Initial Code
#!/usr/bin/perl
#
#
#
# CREATE TABLE `redirects` (
# `id` int(11) NOT NULL auto_increment,
# `ip` varchar(15) collate utf8_polish_ci NOT NULL,
# `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
# `choice` varchar(10) collate utf8_polish_ci NOT NULL,
# PRIMARY KEY (`id`),
# UNIQUE KEY `ip` (`ip`)
#) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci AUTO_INCREMENT=3395 ;
#
#
use Sys::Syslog qw(:DEFAULT setlogsock);
use DBI;
use Net::IP::Match::Regexp qw( create_iprange_regexp match_ip );
$dbase = DBI->connect("DBI:mysql:database=DBNAME;host=DBHOST","DBUSER","DBPASS", { RaiseError => 1 });
$utsfmt = "UNIX_TIMESTAMP()";
my $net1 = create_iprange_regexp( qw( 10.10.0.0/24 10.10.16.0/24 ) );
my $net2 = create_iprange_regexp( qw( 10.21.0.0/17 10.21.128.0/17 ) );
openlog("redirector");
setlogsock('unix');
syslog('info', 'Script started.');
$|=1;
while (<>)
{
@X = split;
my $url = $X[0];
my $oul = $url;
@uri = split(///,$url);
@ip = split(///,$X[1]);
my $sdbq = $dbase->prepare("SELECT * FROM redirects WHERE ip='".$ip[0]."' AND choice IN ('0','1','3')");
$sdbq->execute();
if ($sdbq->rows == 0)
{
if (match_ip($ip[0], $net1))
{
$url="http://redirect_host:port/net1/".$uri[3];
}
elsif(match_ip($ip[0], $net2))
{
$url="http://redirect_host:port/net2/".$uri[3];
}
else
{
$url="http://redirect_host:port/rest/".$uri[3];
}
$action ="New";
$all = "[".$ip[0]."] ".$url."[".$action."] - ".$oul." - ".$uri[3]."n";
syslog('info', $all);
print "$urln";
}
else
{
$action ="In database";
$all = "[".$ip[0]."] ".$url."[".$action."] - ".$oul." - ".$uri[3]."n";
syslog('info', $all);
print;
}
}
closelog();
Initial URL
Initial Description
For database inserts must be made some site on "http://redirect_host:port/.../" with nice getip function: http://snipplr.com/view/10946/
Initial Title
perl redirect for squid
Initial Tags
redirect, perl
Initial Language
Perl