Skip to content

Instantly share code, notes, and snippets.

@PogiNate
Last active April 10, 2017 20:18
Show Gist options
  • Save PogiNate/36b500e50260b0cfbd281cd35f2efc85 to your computer and use it in GitHub Desktop.
Save PogiNate/36b500e50260b0cfbd281cd35f2efc85 to your computer and use it in GitHub Desktop.
A script that lets a raspberry pi tell Pinboard where it is.
import socket
import requests
import sys
import os
name = socket.gethostname()
AUTH_TOKEN = 'YOUR_API_KEY_HERE'
PINBOARD_URL = 'https://api.pinboard.in/v1/posts/add'
FILE_NAME = './ipAddress'
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 0))
ip = s.getsockname()[0]
except:
ip = '127.0.0.1'
finally:
s.close()
return ip
def read_ip():
new_ip = get_ip()
if os.path.isfile(FILE_NAME):
file = open(FILE_NAME, 'r+')
ip = file.read()
else:
file = open(FILE_NAME, 'w')
ip = 'empty'
if ip != new_ip:
file.write(new_ip)
pin_it(new_ip)
def pin_it(ip):
params = {
'url': 'http://' + ip
, 'description': name
, 'tags': 'r_pi find_a_pi'
, 'replace': 'yes'
, 'shared': 'no'
, 'toread': 'yes'
, 'extended': 'This is the ip address for ' + name
, 'auth_token': AUTH_TOKEN}
r = requests.get(PINBOARD_URL, params=params)
if r.status_code == requests.codes.ok:
sys.exit()
else:
sys.exit(r.status_code)
read_ip()
@PogiNate
Copy link
Author

This is quick and dirty and could be much better, but it runs quickly and does what I need it to do. If you have a Pinboard account and some Raspberry Pis that you want to track this is a good way to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment