Check IP
If your computer sits behind a firewall (which is a good thing), it can be difficult to determine what IP address it presents to the rest of the Internet when it makes requests.
You can point your browser at https://www.networksecuritytoolkit.org/nst/tools/ip.shtml to determine your system's IP address as it appears to the rest of the Internet.
The following lists some public URL addresses which should return your public IP address as a simple text string (this corresponds to the default list used by the getipaddr script):
The output provided by https://www.networksecuritytoolkit.org/nst/tools/ip.shtml is a very simple IP number in plain text. This is by design to facilitate automated scripting. For example, I cron the following script on my home LAN to determine how often my IP address is reassigned by my ISP's DHCP server:
#!/bin/sh LDIR="${PWD}" CUR_IP_FILE="${LDIR}/current_ip.txt" LAST_IP_FILE="${LDIR}/last_ip.txt" IP_HISTORY_FILE="${LDIR}/ip_history.tsv" # # Sets the environment variable TIME_STAMP to: # # yyyy-MM-dd HH:mm:ss # # Only does this one time (subsequent invocations are ignored) set_time_stamp() { if [ "$TIME_STAMP" = "" ]; then TIME_STAMP="$(date +'%Y-%m-%d %H:%M:%S')" fi } # # Get current IP of home LAN # # Returns 0 and updates ${CUR_IP_FILE} on success, returns 1 on failure get_ip() { if ( wget -O "${CUR_IP_FILE}" https://www.networksecuritytoolkit.org/nst/tools/ip.shtml > /dev/null 2>&1 ); then if ( grep '^[0-9]*.[0-9]*.[0-9]*.[0-9]*$' "${CUR_IP_FILE}" 2>&1 > /dev/null); then return 0 fi fi return 1 } # check_ip # # check for change in IP address (returns 0 if check was successful) check_ip() { set_time_stamp if ! ( get_ip ); then return 1; fi if [ -f "$LAST_IP_FILE" ]; then if ( diff "${LAST_IP_FILE}" "${CUR_IP_FILE}" > /dev/null 2>&1 ); then return 0; fi fi cp "${CUR_IP_FILE}" "${LAST_IP_FILE}" if [ ! -f "${IP_HISTORY_FILE}" ]; then printf "IP\tDATE\n" > "${IP_HISTORY_FILE}" fi REMOTE_ADDR="$(cat "${CUR_IP_FILE}")" printf "${REMOTE_ADDR}\t${TIME_STAMP}\n" >> "${IP_HISTORY_FILE}" } check_ip
The following script example adds on the ability to post changes in the IP address back to a public web server (you'll need to have your own web space on the Internet to make use of this script):
#!/bin/sh # check_ip.sh # # Script designed to be run as a cron job. Each time this script runs, it: # # - Checks to see if the IP address of the host (as seen by the # Internet) has changed. # # - Updates the ip_history.tsv table if a change is detected. # # - Posts a HTML version back to the Internet (if a ${LDIR}/.ncftpput # config file is found). # # To make this script work: # # - Create a ${LDIR}/.ncftpput config file (if you want results posted # back to a web server). Don't forget to set the permissions to # 600. Contents of file will have three lines in the form: # # host MYFTPSERVER # user MYID # pass MYPASS # # - Optionally update the post_ip() function to construct the HTML # page the way you want. # # - Update the following environment variables to your liking: # # LDIR - Where to create local files after each check # FTP_DIR - Directory to FTP results to # TAG - Name to identify this system by (no spaces) # POST_ON_CHANGE - Set to true if you only want to post updates, set to false # to always post results (allows you to see time changes) LDIR="${PWD}" FTP_DIR=/web/info/ip NAME=home POST_ON_CHANGE=false # # You should NOT need to adjust these # CUR_IP_FILE="${LDIR}/current_ip.txt" LAST_IP_FILE="${LDIR}/last_ip.txt" IP_HISTORY_FILE="${LDIR}/ip_history.tsv" HTML_FILE="${LDIR}/${NAME}.html" NEED_POST=false # # Sets the environment variable TIME_STAMP to: # # yyyy-MM-dd HH:mm:ss # # Only does this one time (subsequent invocations are ignored) set_time_stamp() { if [ "$TIME_STAMP" = "" ]; then TIME_STAMP="$(date +'%Y-%m-%d %H:%M:%S')" fi } # # Get current IP of home LAN # # Returns 0 and updates ${CUR_IP_FILE} on success, returns 1 on failure get_ip() { if ( wget -O "${CUR_IP_FILE}" http://nst.sourceforge.net/nst/tools/ip.php > /dev/null 2>&1 ); then if ( grep '^[0-9]*.[0-9]*.[0-9]*.[0-9]*$' "${CUR_IP_FILE}" 2>&1 > /dev/null); then return 0 fi fi return 1 } # check_ip # # check for change in IP address (returns 0 if check was successful) check_ip() { set_time_stamp if ! ( get_ip ); then return 1; fi if [ -f "$LAST_IP_FILE" ]; then if ( diff "${LAST_IP_FILE}" "${CUR_IP_FILE}" > /dev/null 2>&1 ); then return 0; fi fi NEED_POST=true; cp "${CUR_IP_FILE}" "${LAST_IP_FILE}" if [ ! -f "${IP_HISTORY_FILE}" ]; then printf "IP\tDATE\n" > "${IP_HISTORY_FILE}" fi REMOTE_ADDR="$(cat "${CUR_IP_FILE}")" printf "${REMOTE_ADDR}\t${TIME_STAMP}\n" >> "${IP_HISTORY_FILE}" } # # Post information on-line # post_ip() { set_time_stamp cat > ${HTML_FILE} <<EOF <html><head><title>Remote Access To: ${NAME}</title></head> <link href="http://www.networksecuritytoolkit.org/nst/css/site.css" rel="stylesheet" type="text/css"/> <body> <h1>Remote Access To: ${NAME}</h1> <p>As of <b>${TIME_STAMP}</b> remote access can be found at <b>${REMOTE_ADDR}</b>. Current services available include:</p> <table border="1"> <tr><th>IP</th><th>Port</th><th>Description</th></tr> <tr><td>${REMOTE_ADDR}</td><td>20022</td><td>Secure shell access to ${NAME}.</td></tr> </table> <h1>IP History</h1> <p>The following shows the history of IP address changes since we started running this script.</p> <table border="1" bgcolor="white"> $(awk '-F\t' -- '{ if ( FNR == 1 ) printf("<tr><th>%s</th><th>%s</th></tr>",$1,$2); else printf("<tr><td>%s</td><td>%s</td></tr>",$1,$2); }' < ${IP_HISTORY_FILE}) </table> </body></html> EOF if [ -f "${LDIR}/.ncftpput" ]; then ncftpput -f ${LDIR}/.ncftpput ${FTP_DIR} ${HTML_FILE} >/dev/null 2>&1 fi } check_ip if [ "${POST_ON_CHANGE}" != "true" -o "${NEED_POST}" = "true" ]; then REMOTE_ADDR="$(cat "${CUR_IP_FILE}")" post_ip fi
You can download the above example code by clicking here.
Our plan is to keep the https://www.networksecuritytoolkit.org/nst/tools/ip.shtml service available indefinitely. If you need/want to install the server side of this functionality, you can use the following PHP script on a Unix host running a Apache web server capable of processing PHP pages:
<?php // Echo back IP address as a plain text document header('Content-type: text/plain'); // Check to see if server has been forwarded the request if (isset($_SERVER['HTTP_X_REMOTE_ADDR'])) { printf("%s\n", $_SERVER['HTTP_X_REMOTE_ADDR']); } else { printf("%s\n", $_SERVER['REMOTE_ADDR']); } ?>