Results 1 to 2 of 2

Thread: AutoSend Your External IP to Your Google Documents

  1. #1
    Join Date
    Jan 2007
    Atlanta, GA
    Ubuntu 8.04 Hardy Heron

    AutoSend Your External IP to Your Google Documents

    Dynamic IPs are nice, except that you can never remember what your IP is. This is especially annoying when you are away from home somewhere and you want to SSH or FTP to your home computer. Google documents are a nice place to store information online without having to pay or setup web space. Using this script, you can save your external IP inside the documents section of your gmail account. Then, you will know your home computer's external IP from anywhere just by logging onto gmail and going to the spreadsheet where your IP is stored.

    Let's get started!

    1) Python will need the google documents api, so go here, download the latest version, unpack and run the install script:

    (if you're new to linux commands):
    tar xvf
    sudo python install
    2) Next, copy this code and paste it into your favorite text editor and save as
    #  1) Install the google documents python api
    #  2) Create a spreadsheet in google documents and save it under a name
    #     that matches SPREADSHEET_NAME
    #  3) Name the column headings of the google spreadsheet 'ip','date','time'
    #  4) Edit USERNAME and PASSWD to match your login info
    #  5) Make this file executable
    #  6) In a terminal window, type 'crontab -e' and add this script to your
    #  user's crontab. ex. 56 * * * * /usr/bin/python /path_to_your_script/
    import os
    ## Change These to Match Your Info!
    SPREADSHEET_NAME = 'MyHomeIP' # google documents spreadsheet name
    USERNAME = 'your_username_here' # google/gmail login id
    PASSWD = 'your_password_here' # google/gmail login password
    BASE_DIR = '/tmp/' # Base Directory to locally save your IP info
                       # trailing slash required!
    IP_WEBSITE = ''
    #IP_WEBSITE = '' # alternate website
    ## Function Definitions
    def StringToDictionary(row_data):
      result = {}
      for param in row_data.split():
        name, value = param.split('=')
        result[name] = value
      return result
    def load():
      import gdata.spreadsheet.service
      gd_client = gdata.spreadsheet.service.SpreadsheetsService() = USERNAME
      gd_client.password = PASSWD
      return gd_client
    def updateIP(ip):
      import time,string
      gd_client = load()
      docs= gd_client.GetSpreadsheetsFeed()
      spreads = []
      for i in docs.entry: spreads.append(i.title.text)
      spread_number = None
      for i,j in enumerate(spreads):
        if j == SPREADSHEET_NAME: spread_number = i
      if spread_number == None:
        return 0
      key = docs.entry[spread_number].id.text.rsplit('/', 1)[1]
      feed = gd_client.GetWorksheetsFeed(key)
      wksht_id = feed.entry[0].id.text.rsplit('/', 1)[1]
      feed = gd_client.GetListFeed(key,wksht_id)
      thetime = time.strftime('%I:%M%p')
      thedate = time.strftime('%m/%d/%y')
      entry = gd_client.InsertRow(StringToDictionary('date='+thedate+' time='+thetime+' ip='+ip),key,wksht_id)
      return 1
    ## Executed Code
    if os.path.exists(BASE_DIR+'CheckIP'): os.remove(BASE_DIR+'CheckIP')
    os.system('wget '+IP_WEBSITE+' -t 2 --output-document='+BASE_DIR+'CheckIP')
    fh2 = open(BASE_DIR+'CheckIP','r')
    ip2 =
    if os.path.exists(BASE_DIR+'CurrentIP'):
      fh1 = open(BASE_DIR+'CurrentIP','r')
      ip1 =
      ip1 = ''
    if ip1 == ip2: pass
      res = updateIP(ip2)
      if res == 0: raise 'Please Create a Spreadsheet named \''+SPREADSHEET_NAME+'\' in googleDocs.'
        fh = open(BASE_DIR+'CurrentIP','w')
    Edit the USERNAME and PASSWD to match your gmail login info.
    This file can be saved to the directory of your choosing, but for this example lets put it in ~/Documents/

    Make the file executable.
    cd ~/Documents/
    chmod +x
    3) Log onto your gmail account and create a new spreadsheet. If you've never done this before, click on 'Documents' at the top of your gmail account, click 'new > spreadsheet'. In the spreadsheet, the first row is where the names of the colums go. Name cells A1,B1 and C1 'ip','date','time'. They can be placed in any order but use all lower case.

    The top should look like this:
        A    B    C    D    E
    1| ip  date  time 
    Save the spreadsheet as 'MyHomeIP'. (again case matters, this can be changed easily to something different if you want to change SPREADSHEET_NAME in the python script).

    At this point you can test that the script works by running
    If you look in /tmp you should see the files CurrentIP and CheckIP. Also you can look at your google documents file and see if it sent the information.

    4) Now that you know the script is working, make a cron job to check if your IP has changed.

    crontab -e
    Add this line to the file
    # m h  dom mon dow   command
    59 * * * * /usr/bin/python /home/your_username/Documents/
    This will check at the 59th minute of every hour if your IP has changed. If you want to set it to check less frequently, setting the second * to a number 0-23 will cause it to update daily.

    I hope this works for you as it has been very useful to me.

  2. #2
    Join Date
    Oct 2011

    Re: AutoSend Your External IP to Your Google Documents

    Great little script I was trying to figure out how to use Google docs to do this and found this post! Unfortunately, the IP finding website listed here now uses a picture of the IP address but you can find only the plain text here:



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts