I made a python script that periodically writes the global IP for ssh connection to Raspberry pi to Google Spreadsheet.
The following information is summarized in config.ini.
--Google account --Google app password (app-specific) --Spreadsheet ID of Google Sheets prepared for IP export
config.ini
[google]
username = ****
password = ****
spreadsheet_key = *****(Spreadsheet ID)
The spreadsheet ID corresponds to the ***** part.
https://docs.google.com/spreadsheets/d/*******/edit#gid=0
Use gdata.
sudo apt-get install python-gdata
I made a class that can be updated by giving IP and position (row, column). --Export IP --Line --Column
By default, the first sheet (od6) is selected as the sheet.
Reference: http://qiita.com/debiru/items/27a48dfb3b1c010cd478
mySpreadsheet.py
import gdata.spreadsheets.client
import gdata.spreadsheet.service
import ConfigParser
class mySpreadsheet:
user = ''
passwd = ''
key = ''
sheet_id= ''
def __init__(self,config):
inifile = ConfigParser.SafeConfigParser()
inifile.read(config)
self.user = unicode(inifile.get("google","username"))
self.passwd = unicode(inifile.get("google","password"))
self.key = unicode(inifile.get("google","spreadsheet_key"))
self.sheet_id = 'od6'
def updateCell(self,value,row,col):
client = gdata.spreadsheet.service.SpreadsheetsService(self.user,self.passwd)
client.ProgrammaticLogin()
cell = client.UpdateCell(inputValue = value,
row = row, col = col,
key = self.key,
wksht_id = self.sheet_id)
if __name__ == "__main__":
m = mySpreadsheet('config.ini')
m.updateCell('hoge',1,1)
If you read the above class and pass the IP, it's OK
updateip.py
import json
from urllib2 import urlopen
from mySpreadsheet import *
m = mySpreadsheet('config.ini')
ip = json.load(urlopen('http://httpbin.org/ip'))['origin']
m.updateCell(ip,1,2)
print ip
Recommended Posts