Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Display remote IP address in apache

I wanted a simple way to display a few environment variables in a web response. And in my shell ways, I used /bin/sh!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/sh
# File: ip-address.cgi
# Location: /mnt/public/www/cgi-bin/
# Author: bgstack15
# Startdate: 2020-08-18 22:49
# Title: Simple Diagnostic Page
# Purpose: To show IP address and other basic information, and to be a proof of concept
# History:
# Usage: visit http://www.example.com/cgi-bin/ip-address.cgi
# References:
#    https://httpd.apache.org/docs/2.4/howto/cgi.html
# Improve:
# Documentation:
printf "%s\n\n" "Content-type: text/html"
echo "<html><head><title>IP Addresses</title>"
echo "</head><body>"
echo "<pre>"
#env
test ${CONTENT_LENGTH} -gt 0 && { CONTENT="$( cat )" ; echo "CONTENT ${CONTENT}" ; }
printf '%s ' 'SERVER' ; dig +short myip.opendns.com @resolver1.opendns.com
echo "REMOTE_ADDR ${REMOTE_ADDR}"
test -n "${HTTP_X_FORWARDED_FOR}" && echo "HTTP_X_FORWARDED_FOR ${HTTP_X_FORWARDED_FOR}"
test -n "${QUERY_STRING}" && echo "QUERY_STRING ${QUERY_STRING}"
echo "</pre>"
printf '%s' "</body></html>"

Comments