![]() |
ubuntu.com - launchpad.net - ubuntu help
|
|
|||||||
|
Tutorials & Tips The place to find Ubuntu related Tips & Tricks. |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
5 Cups of Ubuntu
![]() Join Date: Jan 2007
Location: Atlanta, GA
My beans are hidden!
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: http://code.google.com/p/gdata-pytho...downloads/list (if you're new to linux commands): Code:
wget http://gdata-python-client.googlecode.com/files/gdata.py-1.2.3.tar.gz tar xvf gdata.py-1.2.3.tar.gz cd gdata.py-1.2.3/ sudo python setup.py install Code:
#!/usr/bin/python
#
#
# 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/googleIP.py
#
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 = 'www.whatismyip.org'
#IP_WEBSITE = 'whatismyip.everdot.org/ip' # 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()
gd_client.email = USERNAME
gd_client.password = PASSWD
gd_client.ProgrammaticLogin()
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 = fh2.read()
fh2.close()
if os.path.exists(BASE_DIR+'CurrentIP'):
fh1 = open(BASE_DIR+'CurrentIP','r')
ip1 = fh1.read()
fh1.close()
else:
ip1 = ''
if ip1 == ip2: pass
else:
res = updateIP(ip2)
if res == 0: raise 'Please Create a Spreadsheet named \''+SPREADSHEET_NAME+'\' in googleDocs.'
else:
fh = open(BASE_DIR+'CurrentIP','w')
fh.write(ip2)
fh.close()
This file can be saved to the directory of your choosing, but for this example lets put it in ~/Documents/ Make the file executable. Code:
cd ~/Documents/ chmod +x googleIP.py The top should look like this: Code:
A B C D E 1| ip date time __________________________ 2| 3| 4| At this point you can test that the script works by running Code:
python googleIP.py 4) Now that you know the script is working, make a cron job to check if your IP has changed. Code:
crontab -e Code:
# m h dom mon dow command 59 * * * * /usr/bin/python /home/your_username/Documents/googleIP.py I hope this works for you as it has been very useful to me. |
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|