Task

Accessing a China-based office NetCam (IPcam) around the clock from anywhere via web browser.

Environment

  • Office uses a broadband connection with a dynamic IP address.
  • Router and NetCam support services like DynDNS; however all those services are blocked by the  national firewall.
  • The NetCam provides image uploading to FTP and email account.
  • No office computer shall be running to "manually" update some dynamic IP address record somewhere.

Options

  • Use NetCam's FTP upload to
    a) publish an image on a web site (no live picture)
    b) create a FTP log entry on the web site's server for IP extraction.
  • Use NetCam's email upload to
    a) get an image
    b) get the IP address in the email header.

Evaluation

  • Publish FTP image ok but no live picture.
  • FTP log entry ok but requires script to extract dynamic IP address.
  • Email option creates a big inbox and requires manual clean-up.
  • Too complicated to find IP address in email header.

Given that the email option failed, the only outbound connection is the FTP upload.

Solution

  • Setup of FTP upload in 1min intervals, overwriting previous upload.
    This is not too fast for slow Internet connection and not too slow for image viewing.
    Static image viewing can be done through a simple web page.
  • Creating a script which
    a) extracts the dynamic IP address and displays a log file extract
    b) updates a HTML page containing a JAVA applet for live image.

These solutions result in 3 web pages:

  • HTML page with static image (page auto-reloads every minute).
  • HTML page with JAVA applet for live image.
  • Text page with log file data.

Whenever the dynamic IP address changes, the HTML page containing the JAVA applet will be updated. In fact, all 3 files are updated every 1 minute but the user won't notice any change. If the JAVA applet suddenly stops showing images, just reload the page and the updated source code with new IP address is read.

Script to extract only the IP address from the log file (lines below are in fact one command line in a script file):

tail --lines=1 /home/virtual/siteX/fst/var/log/xferlog | grep -oE "([0-9]{1,3}.?){4}" | grep -n .* | grep "^2" | grep -oE "([0-9]{1,3}.?){4}" >> /home/virtual/siteX/fst/home/ipcam/public_html/ip.txt

Explanation:

Each command is separated by the pipe "|" symbol which refers the output from the previous command to the next one. This allows a theoretically endless command line and no need for temporary files in many cases.

tail --lines=1 /home/virtual/siteX/fst/var/log/xferlog
Extract the last (latest) line of the log file (only 1 line).

grep -oE "([0-9]{1,3}.?){4}"
Extract from the previous 1 line a value consisting of 4 parts, each between 1 and 3 digits long, ranging from 0 to 9.

grep -n .*
Because now the output is actually 3 lines where the second line is the desired IP address (other lines include unwanted content), this command numbers each line starting with 1. Note that this actually rewrites the lines with the adding of the line number on first position. Therefore, the previous clean line 2 containing only the IP address x.x.x.x now looks like this:
2 x.x.x.x

grep "^2"
Extracts the second line.

grep -oE "([0-9]{1,3}.?){4}"
Repeat of step 2 to extract the IP address only (removes the line numbering).

>> /home/virtual/siteX/fst/home/ipcam/public_html/ip.txt
Adds the result to the file ip.txt.
 > = overwrite
>> = attach to end, in new line

Security

Protecting the folder containing the files above with a password is recommended. Furthermore, in public only the page with the static image should be loaded as it does not reveal the IP address (image is stored locally). All 3 pages do not link to each other to prevent unwanted access.

Usage

The user can now access a static image updated every minute, a live image (only requires JAVA support) and a log file to see the IP address for further use (like entering manually into a desktop software).

If the IP address is stored in a clean text file, one could configure other software and scripts to access it to always use the latest one no matter how often the ISP changes it. This is important for any direct-office connections. Remote access and control comes into mind.