Last active
February 10, 2016 08:46
-
-
Save awhitty/b7add0960b656f1fc09a to your computer and use it in GitHub Desktop.
Ideally sends the IP address of a raspberry pi to IFTTT Maker channel (untested), based on http://elinux.org/RPi_Email_IP_On_Boot_Debian
This file contains hidden or 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
import subprocess | |
import requests | |
import json | |
MAKER_EVENT_TITLE = "raspi_connected" | |
MAKER_CHANNEL_KEY = "<your key>" | |
def connect_type(word_list): | |
""" This function takes a list of words, then, depeding which key word, returns the corresponding | |
internet connection type as a string. ie) 'ethernet'. | |
""" | |
if 'wlan0' in word_list or 'wlan1' in word_list: | |
con_type = 'wifi' | |
elif 'eth0' in word_list: | |
con_type = 'ethernet' | |
else: | |
con_type = 'current' | |
return con_type | |
arg='ip route list' # Linux command to retrieve ip addresses. | |
# Runs 'arg' in a 'hidden terminal'. | |
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE) | |
data = p.communicate() # Get data from 'p terminal'. | |
# Split IP text block into three, and divide the two containing IPs into words. | |
ip_lines = data[0].splitlines() | |
split_line_a = ip_lines[1].split() | |
split_line_b = ip_lines[2].split() | |
# con_type variables for the message text. ex) 'ethernet', 'wifi', etc. | |
ip_type_a = connect_type(split_line_a) | |
ip_type_b = connect_type(split_line_b) | |
"""Because the text 'src' is always followed by an ip address, | |
we can use the 'index' function to find 'src' and add one to | |
get the index position of our ip. | |
""" | |
ipaddr_a = split_line_a[split_line_a.index('src')+1] | |
ipaddr_b = split_line_b[split_line_b.index('src')+1] | |
# Creates a sentence for each ip address. | |
my_ip_a = 'Your %s ip is %s' % (ip_type_a, ipaddr_a) | |
my_ip_b = 'Your %s ip is %s' % (ip_type_b, ipaddr_b) | |
# Creates the text, subject, 'from', and 'to' of the message. | |
payload = { | |
'value1': my_ip_a, | |
'value2': my_ip_b | |
} | |
requests.post("https://maker.ifttt.com/trigger/%s/with/key/%s" % (MAKER_EVENT_TITLE, MAKER_CHANNEL_KEY), data=json.dumps(payload)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment