FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home » Shell Scripting » Apache Environment Variables CGI Script

by 1 comment

Using Apache Environment Variables in .htaccess files with RewriteRule, SetEnvIf, Header, etc., is made easier by having a script that displays current variables and environment. Heres my slightly improved version of the common printenv and test-cgi scripts that come with Apache Installations.

Features of AskApache Environment Debugger

  • Only displays results for user-configured IP address
  • If IP is not allowed it will redirect to any page you specify.
  • Can also be run from the shell
  • Includes both built-in env and also uses perl

Example Output and Variables

printenv and test-cgi script

  • DOCUMENT_ROOT
  • GATEWAY_INTERFACE
  • HTTP_ACCEPT
  • HTTP_ACCEPT_CHARSET
  • HTTP_ACCEPT_ENCODING
  • HTTP_ACCEPT_LANGUAGE
  • HTTP_CACHE_CONTROL
  • HTTP_CONNECTION
  • HTTP_COOKIE
  • HTTP_HOST
  • HTTP_KEEP_ALIVE
  • HTTP_USER_AGENT
  • QUERY_STRING
  • REMOTE_ADDR
  • REMOTE_PORT
  • REQUEST_METHOD
  • REQUEST_URI
  • SCRIPT_FILENAME
  • SCRIPT_NAME
  • SCRIPT_URI
  • SCRIPT_URL
  • SERVER_ADDR
  • SERVER_ADMIN
  • SERVER_NAME
  • SERVER_PORT
  • SERVER_PROTOCOL
  • SERVER_SIGNATURE
  • SERVER_SOFTWARE

The Shell Script

Or Download

#!/bin/sh
###
### ASKAPACHE ENVIRONMENT DEBUGGER - Display Environment Variables using perl and builtin commands
### Version 1.0.2
###
### ---------------------------------------------------------------------------------------
### This work is licensed under the Creative Commons Attribution 3.0 United States License.
### To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/
### or send a letter to Creative Commons, 171 Second Street, Suite 300,
### San Francisco, California, 94105, USA.
### See http://www.askapache.com/about/disclaimer-and-license/ for info.
### ----------------------------------------------------------------------------------------
###
### Usage:
###
###   Place in public web directory request from your web browser.
###
###
### Install:
###
###   1. Place in public web directory like /cgi-bin/ and rename askapache-env.cgi
###   2. chmod 744 or chmod u+x
###   3. Add Options +ExecCGI to the top of /cgi-bin/.htaccess which should be chmod 644
###   4. Add your IP address to the MY_ALLOWED_IP variable in the config below (http://www.askapache.com/online-tools/whoami/)
###
###   Optionally you can add these lines to your .htaccess (http://www.askapache.com/htaccess/)
###   file to only allow from IP Address 208.113.183.103
###
###   Order Deny,Allow
###   Deny from All
###   Allow from 208.113.183.103

### CONFIGURATION
MY_ALLOWED_IP="208.113.183.103"
MY_REDIRECT="www.askapache.com"

# command line
if [ -e $GATEWAY_INTERFACE ]; then

echo "+ ARGS: ${0}"

echo "+-----------------------------------------------------+"
echo "| env                                                 |";
echo "+=====================================================+";
env | sort;echo;echo

echo "+-----------------------------------------------------+"
echo "| PERL PRINTENV                                       |";
echo "+=====================================================+";
perl -e'foreach $var (sort(keys(%ENV))) {$val = $ENV{$var};$val =~ s|n|\n|g;$val =~ s|"|\"|g;print "${var}="${val}"n"}';echo;echo

echo "+-----------------------------------------------------+"
echo "| declare                                             |";
echo "+=====================================================+";
declare | sort;echo;echo

else

if [ "$REMOTE_ADDR" == "$MY_ALLOWED_IP" ]; then

echo "Content-type: text/plain; charset=iso-8859-1";echo

echo "+ ARGS: ${0}"

echo "+-----------------------------------------------------+"
echo "| env                                                 |";
echo "+=====================================================+";
env | sort;echo;echo

echo "+-----------------------------------------------------+"
echo "| PERL PRINTENV                                       |";
echo "+=====================================================+";
perl -e'foreach $var (sort(keys(%ENV))) {$val = $ENV{$var};$val =~ s|n|\n|g;$val =~ s|"|\"|g;print "${var}="${val}"n"}';echo;echo

#echo "+-----------------------------------------------------+"
#echo "| declare                                             |";
#echo "+=====================================================+";
#declare | sort;echo;echo

else

echo "Content-type: text/html"
echo "Location: http://${MY_REDIRECT}";
echo "";

fi

fi

exit 0

Tags

Comments Welcome

Information is freedom. Freedom is non-negotiable. So please feel free to modify, copy, republish, sell, or use anything on this site in any way at any time ;)

My Online Tools

Popular Articles
Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two.
-- Richard M. Stallman


It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | htaccess.io | htaccess.guru

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service | @Htaccess

↑ TOPMain