Skip to content

Instantly share code, notes, and snippets.

@fmoo
Last active December 20, 2015 21:39
Show Gist options
  • Save fmoo/6199057 to your computer and use it in GitHub Desktop.
Save fmoo/6199057 to your computer and use it in GitHub Desktop.
sparts service that uses twilio to send an SMS whenever a domain's WHOIS State changes
from sparts.vservice import VService
from sparts.vtask import TryLater
from sparts.tasks.poller import PollerTask
from sparts.sparts import option
import whois
import re
import twilio.rest
def parse_whois(data):
result = {}
for line in data.split('\r\n'):
m = re.match('(.+?):(.+?)$', line)
if m is None: continue
k, v = m.groups()
if not v: continue
if k not in result:
result[k] = v
else:
result[k] += ';' + v
return result
class WhoisStatusWatcher(PollerTask):
INTERVAL = 16.0
OPT_PREFIX = 'watch'
domain = option(help='domain to watch', required=True)
sms_from = option(metavar='+PHONE#', required=True)
sms_to = option(metavar='+PHONE#', required=True)
twilio_client_id = option(metavar='STR', required=True)
twilio_secret = option(metavar='STR', required=True)
def fetch(self):
n = whois.NICClient()
try:
result = n.whois_lookup(None, self.domain, 0)
except Exception as e:
self.logger.exception("Error in whois lookup")
raise TryLater(str(e))
return parse_whois(result).get('Status')
def onValueChanged(self, old_value, new_value):
self.logger.info("%s -> %s", old_value, new_value)
client = twilio.rest.TwilioRestClient(
self.twilio_client_id, self.twilio_secret)
message = client.sms.messages.create(
body="%s status changed from %s to %s" %
(self.domain, repr(old_value), repr(new_value)),
to=self.sms_to,
from_=self.sms_from,
)
self.logger.info(message.sid)
WhoisStatusWatcher.register()
VService.initFromCLI(name='WhoisWatch')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment