check_imap_login.in 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/python
  2. # vi:si:et:sw=4:sts=4:ts=4
  3. # -*- coding: UTF-8 -*-
  4. # -*- Mode: Python -*-
  5. #
  6. # Copyright (C) 2005 Bertera Pietro <pietro@bertera.it>
  7. # This file may be distributed and/or modified under the terms of
  8. # the GNU General Public License version 2 as published by
  9. # the Free Software Foundation.
  10. # This file is distributed without any warranty; without even the implied
  11. # warranty of merchantability or fitness for a particular purpose.
  12. # See "LICENSE.GPL" in the source distribution for more information.
  13. import sys, os, imaplib, getopt
  14. def usage():
  15. print "-u <user>"
  16. print "-p <password>"
  17. print "-s use SSL"
  18. print "-H <host>"
  19. def main():
  20. try:
  21. opts, args = getopt.getopt(sys.argv[1:], "u:p:sH:")
  22. except getopt.GetoptError:
  23. usage()
  24. return 3
  25. user = host = password = use_ssl = None
  26. for o, a in opts:
  27. if o == "-u":
  28. user = a
  29. elif o == "-p":
  30. password = a
  31. elif o == "-s":
  32. use_ssl = True
  33. elif o == "-H":
  34. host = a
  35. if user == None or password == None or host == None:
  36. usage()
  37. return 1
  38. if use_ssl:
  39. M = imaplib.IMAP4_SSL(host=host)
  40. else:
  41. M = imaplib.IMAP4(host)
  42. try:
  43. M.login(user, password)
  44. except Exception, e:
  45. print "CRITICAL: IMAP Login not Successful: %s" % e
  46. sys.exit(2)
  47. M.logout()
  48. print "OK IMAP Login Successful"
  49. return 0
  50. if __name__ == "__main__":
  51. sys.exit(main())