Last active
December 10, 2015 02:58
-
-
Save soundkitchen/4371523 to your computer and use it in GitHub Desktop.
Raspberry Pi で動的 IP を Twitter な DM で送らせるメソッド
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
$ sudo apt-get python-pip | |
$ sudo pip install tweepy | |
$ mkdir ~/bin | |
$ pico ~/bin/send_addr.py | |
$ sudo pico /etc/dhcp/dhclient-exit-hooks.d/send_dm |
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
# vim: fileencoding=utf-8 : | |
import sys | |
from tweepy import API, OAuthHandler, TweepError | |
CONSUMER_KEY = u'consumer key here.' | |
CONSUMER_SECRET = u'consumer secret here.' | |
ACCESS_KEY = u'access token key here.' | |
ACCESS_SECRET = u'access token secret here.' | |
TARGET_SCREEN_NAME = u'screen_name here.' | |
def create_api(): | |
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) | |
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) | |
api = API(auth, api_root='/1.1') | |
return api | |
def main(): | |
params = sys.argv[1:] | |
try: | |
name, signal, addr = params[0:3] | |
except ValueError, e: | |
sys.stderr.write('name, signal and ip address required.\n') | |
sys.stderr.flush() | |
sys.exit(1) | |
api = create_api() | |
res = api.send_direct_message(screen_name=TARGET_SCREEN_NAME, text=u'%s caught %s: %s' % (name, signal, addr)) | |
if __name__ == '__main__': | |
main() | |
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
# | |
# tell ip address when changed. | |
# | |
case $reason in | |
BOUND) | |
python /path/to/send_addr.py "RaspberryPi" "${reason}" "${new_ip_address}" | |
;; | |
RENEW|REBIND) | |
if [ "$old_ip_address" != "$new_ip_address" ]; then | |
python /path/to/send_addr.py "RaspberryPi" "${reason}" "${new_ip_address}" | |
fi | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment