expired.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #! /usr/bin/env python
  2. """
  3. expired - check for expired domain names
  4. By default, take a domain name on the command line.
  5. Print expiration date.
  6. exit 0 none of the domains is expired
  7. exit 1 any of the domains is expired
  8. Usage:
  9. expired [--all] [--help] [options] domain
  10. """
  11. # >>> import whois
  12. import whois
  13. import optparse
  14. import sys
  15. from dateutil import parser
  16. from datetime import date
  17. from datetime import datetime
  18. __author__ = 'George Jones'
  19. __maintainer__ = 'George Jones'
  20. __email__ = 'gmj@pobox.com'
  21. __version__ = '0.0.1'
  22. #
  23. # whois
  24. #
  25. def p_error(msg=None):
  26. optp.print_help()
  27. if msg:
  28. optp.error(msg)
  29. sys.exit(1)
  30. def parse_args(argv):
  31. global optp
  32. usage = """
  33. %prog [--all] [options] name [name...]
  34. """
  35. # Parse arguments.
  36. optp = optparse.OptionParser(description=__doc__.strip(), version=__version__,
  37. usage=usage)
  38. # Parse arguments
  39. (opts, args) = optp.parse_args()
  40. return opts, args
  41. def main():
  42. global opts
  43. opts, args = parse_args(sys.argv)
  44. # Check for conflicting options here
  45. if len(args) == 0:
  46. p_error('Must supply at least one domain name')
  47. # today = parser.parse("2010/01/01")
  48. today = datetime.today()#.strftime('%Y-%m-%d')
  49. any_expired = False
  50. for arg in args:
  51. domain = whois.query(arg)
  52. dict = domain.__dict__
  53. for key in dict.keys():
  54. value = dict[key]
  55. t = type(value)
  56. #print(f"{key}: {value} : {t}" )
  57. if dict["expiration_date"] < today:
  58. print(dict["name"] + " expired")
  59. any_expired = True
  60. else:
  61. print(dict["name"] + " not expired")
  62. break
  63. if any_expired:
  64. sys.exit(1)
  65. else:
  66. sys.exit(0)
  67. if __name__ == '__main__':
  68. main()