implement /d
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
# $Id: hbd,v 1.38 2013/07/14 02:25:05 andreas Exp $
|
# $Id: hbd,v 1.38 2013/07/14 02:25:05 andreas Exp $
|
||||||
# Wait for heartbeat messages and act on them (or their absence)
|
# Wait for heartbeat messages and act on them (or their absence)
|
||||||
#
|
#
|
||||||
VER = 1.36
|
VER = 1.38
|
||||||
|
|
||||||
import time, os, string, sys, socket, atexit, select, SocketServer, getopt, signal, cPickle, smtplib, traceback, urllib
|
import time, os, string, sys, socket, atexit, select, SocketServer, getopt, signal, cPickle, smtplib, traceback, urllib
|
||||||
|
|
||||||
@@ -239,7 +239,7 @@ def initwin():
|
|||||||
msgw.setscrreg(0, msgwHeight-1)
|
msgw.setscrreg(0, msgwHeight-1)
|
||||||
msgw.scrollok(1)
|
msgw.scrollok(1)
|
||||||
|
|
||||||
stdscr.addstr(0, 0, "hbd Version 1.0", curses.A_BOLD)
|
stdscr.addstr(0, 0, "hbd Version %s" % VER, curses.A_BOLD)
|
||||||
stdscr.refresh()
|
stdscr.refresh()
|
||||||
msgwB.refresh()
|
msgwB.refresh()
|
||||||
|
|
||||||
@@ -456,10 +456,11 @@ def readsock():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
rmsg="ACK"
|
rmsg="ACK"
|
||||||
if len(hosts[name].cmds) then
|
if len(hosts[name].cmds):
|
||||||
rmsg=hosts[name].cmds[0]
|
rmsg=hosts[name].cmds[0]
|
||||||
email("%s cmd exec" % name, "command '%s' initiated" % hosts[name].cmds[0])
|
email("%s cmd exec" % name, "command '%s' initiated" % hosts[name].cmds[0])
|
||||||
del hosts[name].cmds[0]
|
del hosts[name].cmds[0]
|
||||||
|
log("%s command initiated" % name)
|
||||||
try:
|
try:
|
||||||
sock.sendto(rmsg, addr)
|
sock.sendto(rmsg, addr)
|
||||||
except:
|
except:
|
||||||
@@ -489,71 +490,23 @@ def exitcurses():
|
|||||||
#
|
#
|
||||||
#
|
#
|
||||||
class HtmlHandler(SocketServer.BaseRequestHandler):
|
class HtmlHandler(SocketServer.BaseRequestHandler):
|
||||||
|
allow_reuse_address = True
|
||||||
def handle(self):
|
|
||||||
f = self.request.makefile()
|
|
||||||
|
|
||||||
uri = '/unknown'
|
|
||||||
while 1:
|
|
||||||
line = string.strip(f.readline())
|
|
||||||
if len(line) == 0:
|
|
||||||
break
|
|
||||||
r = line.split()
|
|
||||||
if r[0] == "GET":
|
|
||||||
uri = r[1]
|
|
||||||
html = r[2]
|
|
||||||
upar=string.split(uri,"?")
|
|
||||||
if len(upar) == 1:
|
|
||||||
uarg=[]
|
|
||||||
else:
|
|
||||||
uargs=string.split(upar[1],"&")
|
|
||||||
|
|
||||||
code = 404
|
|
||||||
cause = "Not Found"
|
|
||||||
if uri == "/":
|
|
||||||
code = 200
|
|
||||||
cause = "OK"
|
|
||||||
elif upar[0] == "/c": # /c?h=melschserver&c=sudo%20ls
|
|
||||||
uname=""
|
|
||||||
ucmd=""
|
|
||||||
if uarg[0][:2] == "h=":
|
|
||||||
uname=uarg[0][2:]
|
|
||||||
if uarg[1][:2] == "c=":
|
|
||||||
ucmd=uarg[1][2:]
|
|
||||||
if ucmd != "" and uname != "" and hosts.has_key(uname):
|
|
||||||
hosts[uname].cmds.append(urllib.unquote(ucmd))
|
|
||||||
code = 200
|
|
||||||
cause = "OK"
|
|
||||||
|
|
||||||
|
|
||||||
|
def buildhead(self, title="Heartbeat", refresh=None):
|
||||||
self.request.send("HTTP/1.0 %s %s\r\n" % (code, cause))
|
|
||||||
self.request.send("Date: %s\r\n" % time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)))
|
|
||||||
self.request.send("Server: hbd\r\n")
|
|
||||||
self.request.send("Last-Modified: %s\r\n" % time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)))
|
|
||||||
self.request.send("Accept-Ranges: bytes\r\n")
|
|
||||||
self.request.send("Connection: close\r\n")
|
|
||||||
self.request.send("Content-Type: text/html; charset = ISO-8859-1\r\n\r\n")
|
|
||||||
res=[]
|
res=[]
|
||||||
if code != 200:
|
|
||||||
res.append('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">')
|
|
||||||
res.append('<html><head>')
|
|
||||||
res.append('<meta http-equiv = Refresh content = 60>')
|
|
||||||
res.append('<title>%s %s</title>' % (code, cause))
|
|
||||||
res.append('</head><body>')
|
|
||||||
res.append('<h1>%s</h1>' % (cause))
|
|
||||||
res.append('<p>The requested URL %s was not found on this server.</p>' % uri)
|
|
||||||
res.append('<hr>')
|
|
||||||
res.append('<address>hbd (Unix) Server at %s Port %d</address>' % (hbd_host, hbd_port))
|
|
||||||
res.append('</body></html>')
|
|
||||||
|
|
||||||
else:
|
|
||||||
res.append('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">')
|
res.append('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">')
|
||||||
res.append("<html>")
|
res.append("<html>")
|
||||||
res.append("<head>")
|
res.append("<head>")
|
||||||
res.append("<meta http-equiv = Refresh content = %d>\n" % 60)
|
res.append('<title>%s</title>' % (title))
|
||||||
|
if refresh:
|
||||||
|
res.append("<meta http-equiv = Refresh content = %d>\n" % refresh)
|
||||||
res.append("</head>")
|
res.append("</head>")
|
||||||
res.append('<body BGCOLOR = "#FFFFFF" LINK = "#008000" VLINK = "#008000" BACKGROUND = "/~andreas/images/tile.marble.gif">')
|
res.append('<body BGCOLOR = "#FFFFFF" LINK = "#008000" VLINK = "#008000" BACKGROUND = "/~andreas/images/tile.marble.gif">')
|
||||||
|
return res
|
||||||
|
|
||||||
|
def buildpage(self):
|
||||||
|
res=self.buildhead(refresh=60)
|
||||||
res.append("<H2>Heartbeat status</h2><h4> %s (%s)</H4>" % (time.strftime("%H:%M:%S", time.localtime(now)), os.environ.get('TZ', 'CET-1CDT')))
|
res.append("<H2>Heartbeat status</h2><h4> %s (%s)</H4>" % (time.strftime("%H:%M:%S", time.localtime(now)), os.environ.get('TZ', 'CET-1CDT')))
|
||||||
res.append("<table>")
|
res.append("<table>")
|
||||||
res.append("<tr><th>Host</th><th>State</th><th>IP Addr</th><th>Last change</th></tr>\n")
|
res.append("<tr><th>Host</th><th>State</th><th>IP Addr</th><th>Last change</th></tr>\n")
|
||||||
@@ -566,6 +519,78 @@ class HtmlHandler(SocketServer.BaseRequestHandler):
|
|||||||
for m in msgs[len(msgs)-30:]:
|
for m in msgs[len(msgs)-30:]:
|
||||||
res.append("%s<BR>" % m)
|
res.append("%s<BR>" % m)
|
||||||
res.append("</body></html>")
|
res.append("</body></html>")
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def handle(self):
|
||||||
|
headers=[]
|
||||||
|
headers.append("Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)))
|
||||||
|
headers.append("Server: hbd")
|
||||||
|
headers.append("Last-Modified: %s" % time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)))
|
||||||
|
headers.append("Accept-Ranges: bytes")
|
||||||
|
headers.append("Connection: close")
|
||||||
|
headers.append("Content-Type: text/html; charset = ISO-8859-1")
|
||||||
|
|
||||||
|
uri = '/unknown'
|
||||||
|
f = self.request.makefile()
|
||||||
|
while 1:
|
||||||
|
line = string.strip(f.readline())
|
||||||
|
if len(line) == 0:
|
||||||
|
break
|
||||||
|
r = line.split()
|
||||||
|
if r[0] == "GET":
|
||||||
|
uri = r[1]
|
||||||
|
html = r[2]
|
||||||
|
upar=string.split(uri,"?")
|
||||||
|
if len(upar) == 1:
|
||||||
|
uarg=[]
|
||||||
|
else:
|
||||||
|
uarg=string.split(upar[1],"&")
|
||||||
|
|
||||||
|
code = 200
|
||||||
|
cause = "OK"
|
||||||
|
if uri == "/":
|
||||||
|
res=self.buildpage()
|
||||||
|
|
||||||
|
elif upar[0] == "/c": # command on host /c?h=melschserver&c=sudo%20ls
|
||||||
|
uname=""
|
||||||
|
ucmd=""
|
||||||
|
if uarg[0][:2] == "h=":
|
||||||
|
uname=uarg[0][2:]
|
||||||
|
if uarg[1][:2] == "c=":
|
||||||
|
ucmd=uarg[1][2:]
|
||||||
|
if ucmd != "" and uname != "" and hosts.has_key(uname):
|
||||||
|
hosts[uname].cmds.append(urllib.unquote(ucmd))
|
||||||
|
res=self.buildhead()
|
||||||
|
res.append("2Done")
|
||||||
|
|
||||||
|
elif upar[0] == "/d": # drop host /d?h=melschserver
|
||||||
|
if uarg[0][:2] == "h=":
|
||||||
|
uname=uarg[0][2:]
|
||||||
|
if uname != "" and hosts.has_key(uname):
|
||||||
|
del hosts[uname]
|
||||||
|
log("%s dropped" % uname)
|
||||||
|
res=self.buildhead()
|
||||||
|
res.append("Done")
|
||||||
|
|
||||||
|
else:
|
||||||
|
code = 404
|
||||||
|
cause = "Not Found"
|
||||||
|
res=[]
|
||||||
|
res.append('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">')
|
||||||
|
res.append('<html><head>')
|
||||||
|
res.append('<title>%s %s</title>' % (code, cause))
|
||||||
|
res.append('</head><body>')
|
||||||
|
res.append('<h1>%s</h1>' % (cause))
|
||||||
|
res.append('<p>The requested URL %s was not found on this server.</p>' % uri)
|
||||||
|
res.append('<hr>')
|
||||||
|
res.append('<address>hbd (Unix) Server at %s Port %s</address>' % (hbd_host, hbd_port))
|
||||||
|
res.append('</body></html>')
|
||||||
|
|
||||||
|
self.request.send("HTTP/1.0 %s %s\r\n" % (code, cause))
|
||||||
|
for h in headers:
|
||||||
|
self.request.send("%s\r\n" % h)
|
||||||
|
self.request.send("\r\n")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.request.send(string.join(res, "\n"))
|
self.request.send(string.join(res, "\n"))
|
||||||
@@ -576,7 +601,7 @@ class HtmlHandler(SocketServer.BaseRequestHandler):
|
|||||||
def saveandrestart():
|
def saveandrestart():
|
||||||
sock.close()
|
sock.close()
|
||||||
serv.shutdown()
|
serv.shutdown()
|
||||||
serv.socket.close()
|
serv.server_close()
|
||||||
log("restarting")
|
log("restarting")
|
||||||
os.execv(sys.argv[0], [sys.argv[0]]+cmdargs)
|
os.execv(sys.argv[0], [sys.argv[0]]+cmdargs)
|
||||||
|
|
||||||
@@ -739,9 +764,9 @@ atexit.register(on_exit)
|
|||||||
ilist = []
|
ilist = []
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
||||||
|
|
||||||
sock.bind(("", hb_port))
|
sock.bind(("", hb_port))
|
||||||
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
ilist.append(sock)
|
ilist.append(sock)
|
||||||
|
|
||||||
serv = SocketServer.TCPServer((hbd_host, hbd_port), HtmlHandler)
|
serv = SocketServer.TCPServer((hbd_host, hbd_port), HtmlHandler)
|
||||||
|
|||||||
Reference in New Issue
Block a user