What I basically want to write is a simple script, that sends me an email with the cgi-environment variables.
BUT I don't wanna get the email, when I am the one running the script (checking IP).
I got the script working on virtualave. switched now to tera-byte and added the ip checking.
what am i doing wrong?!
I also get an error without checking for the IP....
Here is the script:
#!/usr/bin/perl
# http://www.kersche.net/receipt.cgi?email=tst #
# Define Variables #
# $mailprog defines the location of your sendmail program on your unix #
# system. #
$mailprog = '/usr/sbin/sendmail';
# Done #
##############################################################################
# First, get the CGI variables into a list of strings
%cgivars= &getcgivars ;
# Retrieve Date
&get_date;
# Send E-Mail
&send_mail;
#----------------- start of &getcgivars() module ----------------------
# Read all CGI vars into an associative array.
# If multiple input fields have the same name, they are concatenated into
# one array element and delimited with the \0 character (which fails if
# the input has any \0 characters, very unlikely but conceivably possible).
# Currently only supports Content-Type of application/x-www-form-urlencoded.
sub getcgivars {
local($in, %in) ;
local($name, $value) ;
# First, read entire string of CGI vars into $in
if ( ($ENV{'REQUEST_METHOD'} eq 'GET') ||
($ENV{'REQUEST_METHOD'} eq 'HEAD') ) {
$in= $ENV{'QUERY_STRING'} ;
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
if ($ENV{'CONTENT_TYPE'}=~ m#^application/x-www-form-urlencoded$#i) {
$ENV{'CONTENT_LENGTH'} ;
read(STDIN, $in, $ENV{'CONTENT_LENGTH'}) ;
}
}
# Resolve and unencode name/value pairs into %in
foreach (split('&', $in)) {
s/\+/ /g ;
($name, $value)= split('=', $_, 2) ;
$name=~ s/%(..)/chr(hex($1))/ge ;
$value=~ s/%(..)/chr(hex($1))/ge ;
$in{$name}.= "\0" if defined($in{$name}) ; # concatenate multiple vars
$in{$name}.= $value ;
}
return %in ;
}
sub get_date {
# Define arrays for the day of the week and month of the year. #
@days = ('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
@months = ('January','February','March','April','May','June','July',
'August','September','October','November','December');
# Get the current time and format the hour, minutes and seconds. Add #
# 1900 to the year to get the full 4 digit year. #
($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
$time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
$year += 1900;
# Format the date. #
$date = "$days[$wday], $months[$mon] $mday, $year at $time";
}
sub send_mail {
# Localize variables used in this subroutine. #
#local($print_config,$key,$sort_order,$sorted_field,$env_report);
#if not MY IP then sent the mail!!
if ($ENV{'REMOTE_ADDR'} != '212.17.104.124') {
# Open The Mail Program
open(MAIL,"|$mailprog");
print MAIL "To: arno\@kersche.net (Arno Kersche)\n";
print MAIL "From: arno\@kersche.net (Arno Kersche)\n";
print MAIL "Subject: Mail was retrieved by recipient\n";
print MAIL "Below is the information of this return receipt:\n";
print MAIL "Email viewed on $date\n";
print MAIL "add 8 hours to get CET (correct time diffrence)\n"
print MAIL "-" x 75 . "\n\n";
# Print the CGI variables sent by the user.
# Note that the order of variables is unpredictable.
# Also note this simple example assumes all input fields had unique names,
# though the &getcgivars() routine correctly handles similarly named
# fields-- it delimits the multiple values with the \0 character, within
# $cgivars{$_}.
foreach (keys %cgivars) {
print MAIL "[$_] = [$cgivars{$_}]\n" ;
}
#print MAIL "Remote_Addr: $ENV{'REMOTE_ADDR'}\n" ;
print MAIL "\n\n" ;
foreach $key (sort keys(%ENV)) {
print MAIL "$key = $ENV{$key}\n" ;
}
print MAIL "-" x 75 . "\n\n";
close (MAIL);
} #end if
}
BUT I don't wanna get the email, when I am the one running the script (checking IP).
I got the script working on virtualave. switched now to tera-byte and added the ip checking.
what am i doing wrong?!
I also get an error without checking for the IP....
Here is the script:
#!/usr/bin/perl
# http://www.kersche.net/receipt.cgi?email=tst #
# Define Variables #
# $mailprog defines the location of your sendmail program on your unix #
# system. #
$mailprog = '/usr/sbin/sendmail';
# Done #
##############################################################################
# First, get the CGI variables into a list of strings
%cgivars= &getcgivars ;
# Retrieve Date
&get_date;
# Send E-Mail
&send_mail;
#----------------- start of &getcgivars() module ----------------------
# Read all CGI vars into an associative array.
# If multiple input fields have the same name, they are concatenated into
# one array element and delimited with the \0 character (which fails if
# the input has any \0 characters, very unlikely but conceivably possible).
# Currently only supports Content-Type of application/x-www-form-urlencoded.
sub getcgivars {
local($in, %in) ;
local($name, $value) ;
# First, read entire string of CGI vars into $in
if ( ($ENV{'REQUEST_METHOD'} eq 'GET') ||
($ENV{'REQUEST_METHOD'} eq 'HEAD') ) {
$in= $ENV{'QUERY_STRING'} ;
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
if ($ENV{'CONTENT_TYPE'}=~ m#^application/x-www-form-urlencoded$#i) {
$ENV{'CONTENT_LENGTH'} ;
read(STDIN, $in, $ENV{'CONTENT_LENGTH'}) ;
}
}
# Resolve and unencode name/value pairs into %in
foreach (split('&', $in)) {
s/\+/ /g ;
($name, $value)= split('=', $_, 2) ;
$name=~ s/%(..)/chr(hex($1))/ge ;
$value=~ s/%(..)/chr(hex($1))/ge ;
$in{$name}.= "\0" if defined($in{$name}) ; # concatenate multiple vars
$in{$name}.= $value ;
}
return %in ;
}
sub get_date {
# Define arrays for the day of the week and month of the year. #
@days = ('Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday');
@months = ('January','February','March','April','May','June','July',
'August','September','October','November','December');
# Get the current time and format the hour, minutes and seconds. Add #
# 1900 to the year to get the full 4 digit year. #
($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
$time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
$year += 1900;
# Format the date. #
$date = "$days[$wday], $months[$mon] $mday, $year at $time";
}
sub send_mail {
# Localize variables used in this subroutine. #
#local($print_config,$key,$sort_order,$sorted_field,$env_report);
#if not MY IP then sent the mail!!
if ($ENV{'REMOTE_ADDR'} != '212.17.104.124') {
# Open The Mail Program
open(MAIL,"|$mailprog");
print MAIL "To: arno\@kersche.net (Arno Kersche)\n";
print MAIL "From: arno\@kersche.net (Arno Kersche)\n";
print MAIL "Subject: Mail was retrieved by recipient\n";
print MAIL "Below is the information of this return receipt:\n";
print MAIL "Email viewed on $date\n";
print MAIL "add 8 hours to get CET (correct time diffrence)\n"
print MAIL "-" x 75 . "\n\n";
# Print the CGI variables sent by the user.
# Note that the order of variables is unpredictable.
# Also note this simple example assumes all input fields had unique names,
# though the &getcgivars() routine correctly handles similarly named
# fields-- it delimits the multiple values with the \0 character, within
# $cgivars{$_}.
foreach (keys %cgivars) {
print MAIL "[$_] = [$cgivars{$_}]\n" ;
}
#print MAIL "Remote_Addr: $ENV{'REMOTE_ADDR'}\n" ;
print MAIL "\n\n" ;
foreach $key (sort keys(%ENV)) {
print MAIL "$key = $ENV{$key}\n" ;
}
print MAIL "-" x 75 . "\n\n";
close (MAIL);
} #end if
}