From 0b0d94fee3ab20e7f488072f624b4f33c527947a Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 29 Oct 2017 05:17:42 +0100 Subject: [PATCH] First commit --- .gitignore | 2 ++ notifier.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 .gitignore create mode 100644 notifier.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..99e904da --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.ropeproject +*.swp diff --git a/notifier.py b/notifier.py new file mode 100644 index 00000000..a079179b --- /dev/null +++ b/notifier.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python + +import os +import logging +import json +import subprocess + +from pushbullet import Listener +from pushbullet import Pushbullet + +__author__ = 'Fabio Manganiello ' + +def get_hostname(): + with open('/etc/hostname','r') as fp: + return fp.readline().strip() + +logging.basicConfig(level=logging.INFO) +hostname = get_hostname() + +API_KEY = 'o.EHMMnZneJdpNQv9FSFbyY2busin7floe' +HTTP_PROXY_HOST = None +HTTP_PROXY_PORT = None + + +def on_push(data): + global hostname + + if 'type' not in data \ + or 'push' not in data \ + or data['type'] != 'push': + return # Not a push notification + + push = data['push'] + logging.debug('Received push: {}'.format(push)) + + if 'body' not in push: + return + + body = None + try: + body = json.loads(push['body']) + except ValueError as e: + return + + if 'target' not in body or body['target'] != hostname: + return # Not for me + + logging.info('Received message: {}'.format(body)) + if 'cmd' in body: + logging.info('Executing command: {}'.format(body['cmd'])) + os.system(body['cmd']) + + +def main(): + pb = Pushbullet(API_KEY) + + s = Listener(account=pb, + on_push=on_push, + http_proxy_host=HTTP_PROXY_HOST, + http_proxy_port=HTTP_PROXY_PORT) + try: + s.run_forever() + except KeyboardInterrupt: + s.close() + + +if __name__ == '__main__': + main()