Skip to content

Instantly share code, notes, and snippets.

@misterhay
Last active April 14, 2017 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misterhay/2d7b6d64d45db430fd28 to your computer and use it in GitHub Desktop.
Save misterhay/2d7b6d64d45db430fd28 to your computer and use it in GitHub Desktop.
finds the machine's hostname and external and internal IP address, and logs them to data.sparkfun.com
#!/usr/bin/env python
# by David Hay @misterhay
# This script finds the machine's hostname and external and internal IP address, and logs them to data.sparkfun.com
# borrowed some code from http://blog.turningdigital.com/2012/09/get-ip-address-of-raspberry-pi-operating-headlessly/
# edit the next two lines with your values from https://data.sparkfun.com/streams/make
public_key = ''
private_key = ''
import os
import re
import commands
import requests # https://github.com/kennethreitz/requests/
# get the hostname
hostname = os.uname()[1]
# on Windows:
'''
import socket
hostname = socket.gethostname()
ip = socket.gethostbyname(hostname)
'''
# get the internal IP address
addresses = []
found_these = re.findall( r'[0-9]+(?:\.[0-9]+){3}', commands.getoutput("/sbin/ifconfig"))
for ip in found_these:
if ip.startswith('255') or ip.startswith('127') or ip.endswith('255') or ip.startswith('0'):
continue # we don't want those sorts of IP addresses
addresses.append(ip)
internal_address = addresses[0] # we are assuming that we want the first IP address in the created list
# get the external IP address
check_address = requests.get('http://checkip.dyndns.org/').text
start = check_address.find(': ') + 2
end = check_address.find('</body>')
external_address = check_address[start:end]
# push what we've found to data.sparkfun.com
base_url = 'https://data.sparkfun.com/input/'+public_key
payload = {'private_key': private_key, 'hostname':hostname, 'internaladdress': internal_address, 'externaladdress': external_address}
push_url = requests.get(base_url, params=payload)
# print what we've found
print 'hostname:', hostname
print 'internal_address:', internal_address
print 'external_address:', external_address
print 'Logged to https://data.sparkfun.com/streams/'+public_key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment