home *** CD-ROM | disk | FTP | other *** search
/ 207.182.244.42 / 207.182.244.42.tar / 207.182.244.42 / pub / daemon-control < prev    next >
Text File  |  2007-07-21  |  4KB  |  157 lines

  1. #!/usr/bin/env python
  2. # denyhosts     Bring up/down the DenyHosts daemon
  3. #
  4. # chkconfig: 2345 98 02
  5. # description: Activates/Deactivates the
  6. #    DenyHosts daemon to block ssh attempts
  7. #
  8. ###############################################
  9.  
  10. ###############################################
  11. #### Edit these to suit your configuration ####
  12. ###############################################
  13.  
  14. DENYHOSTS_BIN   = "/usr/bin/denyhosts.py"
  15. DENYHOSTS_LOCK  = "/var/lock/subsys/denyhosts"
  16. DENYHOSTS_CFG   = "/usr/share/denyhosts/denyhosts.cfg"
  17.  
  18. PYTHON_BIN      = "/usr/bin/env python"
  19.  
  20. ###############################################
  21. ####         Do not edit below             ####
  22. ###############################################
  23.  
  24. DENYHOSTS_BIN = "%s %s" % (PYTHON_BIN, DENYHOSTS_BIN)
  25.  
  26. import os, sys, signal, time
  27.  
  28. # make sure 'ps' command is accessible (which should be
  29. # in either /usr/bin or /bin.  Modify the PATH so
  30. # popen can find it
  31. env = os.environ.get('PATH', "")
  32. os.environ['PATH'] = "/usr/bin:/bin:%s" % env
  33.  
  34. STATE_NOT_RUNNING = -1
  35. STATE_LOCK_EXISTS = -2
  36.  
  37. def usage():
  38.     print "Usage: %s {start [args...] | stop | restart [args...] | status | debug | condrestart [args...] }" % sys.argv[0]
  39.     print
  40.     print "For a list of valid 'args' refer to:"
  41.     print "$ denyhosts.py --help"
  42.     print
  43.     sys.exit(0) 
  44.  
  45.  
  46. def getpid():
  47.     try:
  48.         fp = open(DENYHOSTS_LOCK, "r")
  49.         pid = int(fp.readline().rstrip())
  50.         fp.close()
  51.     except Exception, e:
  52.         return STATE_NOT_RUNNING
  53.  
  54.     
  55.     if not sys.platform.startswith('freebsd') and os.access("/proc", os.F_OK):
  56.         # proc filesystem exists, look for pid
  57.         if os.access(os.path.join("/proc", str(pid)), os.F_OK):
  58.             return pid
  59.         else:
  60.             return STATE_LOCK_EXISTS
  61.     else:
  62.         # proc filesystem doesn't exist (or it doesn't contain PIDs), use 'ps'
  63.         p = os.popen("ps -p %d" % pid, "r")
  64.         p.readline() # get the header line
  65.         pid_running = p.readline() 
  66.         # pid_running will be '' if no process is found
  67.         if pid_running: 
  68.             return pid
  69.         else: 
  70.             return STATE_LOCK_EXISTS
  71.  
  72.  
  73. def start(*args):
  74.     cmd = "%s --daemon " % DENYHOSTS_BIN
  75.     if args: cmd += ' '.join(args)
  76.         
  77.     print "starting DenyHosts:   ", cmd
  78.  
  79.     os.system(cmd)
  80.  
  81.  
  82. def stop():
  83.     pid = getpid()
  84.     if pid >= 0:
  85.         os.kill(pid, signal.SIGTERM)
  86.         print "sent DenyHosts SIGTERM"
  87.     else:
  88.         print "DenyHosts is not running"
  89.  
  90. def debug():
  91.     pid = getpid()
  92.     if pid >= 0:
  93.         os.kill(pid, signal.SIGUSR1)
  94.         print "sent DenyHosts SIGUSR1"
  95.     else:
  96.         print "DenyHosts is not running"
  97.         
  98. def status():
  99.     pid = getpid()
  100.     if pid == STATE_LOCK_EXISTS:
  101.         print "%s exists but DenyHosts is not running" % DENYHOSTS_LOCK
  102.     elif pid == STATE_NOT_RUNNING:
  103.         print "Denyhosts is not running"
  104.     else:
  105.         print "DenyHosts is running with pid = %d" % pid
  106.  
  107.  
  108. def condrestart(*args):
  109.     pid = getpid()
  110.     if pid >= 0:
  111.         restart(*args)
  112.         
  113.  
  114. def restart(*args):
  115.     stop()
  116.     time.sleep(1)
  117.     start(*args)  
  118.  
  119.  
  120. if __name__ == '__main__':
  121.     cases = {'start':       start,
  122.              'stop':        stop,
  123.              'debug':       debug,
  124.              'status':      status,
  125.              'condrestart': condrestart,
  126.              'restart':     restart}
  127.     
  128.     try:
  129.         args = sys.argv[2:]
  130.     except:
  131.         args = []
  132.  
  133.     try:
  134.         # arg 1 should contain one of the cases above
  135.         option = sys.argv[1]
  136.     except:
  137.         # try to infer context (from an /etc/init.d/ script, perhaps)
  138.         procname = os.path.basename(sys.argv[0])
  139.         infer_dict = {'K': 'stop',
  140.                       'S': 'start'}
  141.         option = infer_dict.get(procname[0])
  142.         if not option:
  143.             usage()
  144.  
  145.     try:
  146.         if option in ('start', 'restart', 'condrestart'):
  147.             if '--config' not in args and '-c' not in args:
  148.                 args.append("--config=%s" % DENYHOSTS_CFG)
  149.  
  150.         cmd = cases[option]
  151.         apply(cmd, args)
  152.     except:
  153.         usage()
  154.     
  155.  
  156.  
  157.