Last active
April 14, 2017 03:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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