📄 arnoldhandler.py
字号:
## $Id$## Copyright 2003-2005 Norwegian University of Science and Technology## This file is part of Network Administration Visualized (NAV)## NAV is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## NAV is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with NAV; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA### Authors: John Magne Bredal <john.m.bredal@ntnu.no>#from mod_python import apacheimport reimport nav, nav.pathfrom nav import web, dbfrom nav.db import managefrom nav.web.templates.MainTemplate import MainTemplatefrom nav.web.templates.ArnoldTemplate import ArnoldTemplatefrom nav.web.URI import URIfrom urllib import unquote_plusfrom IPy import IPimport commands############################################################def handler(req): # Read config from configfile config = readConfig(nav.path.sysconfdir + "/arnold/arnold.cfg") # Connect to the database # getConnection('subsystem','database') conn = db.getConnection('arnold','arnold'); cur = conn.cursor() arnoldhome = nav.path.bindir # Reload to make sure any changes in ArnoldTemplate are included #reload(ArnoldTemplate) args = URI(req.unparsed_uri) page = ArnoldTemplate() # Page path is used for quick navigation page.path = [("Home","/"), ("Arnold", False)] section = "" s = re.search("arnold\/(\w+?)(?:\/$|\?|\&|$)",req.uri) if s: section = s.group(1) else: section = 'blockedports' # Make menu based on user setMenu(page) page.username = req.session['user'].login page.name = req.session['user'].name if section == 'blocktypes': sort = args.get('sort') or 'blocktitle' page.head = 'List of current blocktypes' printBlocks(cur, page, sort, section) page.path = [("Home","/"), ("Arnold", "/arnold"), ("Blocktypes", False)] elif section == 'history': sort = args.get('sort') or 'ip' days = args.get('days') or '7' # This is printhistory page.head = "History" printHistory(cur, page, sort, section, days) page.path = [("Home","/"), ("Arnold", "/arnold"), ("History", False)] elif section == 'blockedports': sort = args.get('sort') or 'ip' page.output = args.get('output') or "" page.head = "List of blocked ports" printBlocked(cur,page,sort, section) page.path = [("Home","/"), ("Arnold", "/arnold"), ("Blocked ports", False)] elif section == 'search': page.head = "Search" searchfield = args.get('searchfield') searchtext = args.get('searchtext') status = args.get('status') days = args.get('days') printSearch(cur, page, searchfield, searchtext, status, days) page.path = [("Home","/"), ("Arnold", "/arnold"), ("Search", False)] elif section == 'addreason': page.head = "Add blockreason" printBlockreasons(cur, page, section) page.path = [("Home","/"), ("Arnold", "/arnold"), ("Add blockreason", False)] elif section == 'manualblock': sort = args.get('sort') or 'ip' page.head = "Manual block" page.output = args.get('output') or "" printManualblock(cur, page, sort, section) page.path = [("Home","/"), ("Arnold", "/arnold"), ("Manual block", False)] elif section == 'showdetails': page.head = "Details" id = args.get('id') showDetails(cur, page, section, id) page.path = [("Home","/"), ("Arnold", "/arnold"), ("Details", False)] elif section == 'addBlocktype': page.head = "" page.path = [("Home","/"), ("Arnold", "/arnold"), ("AddBlocktype", False)] id = args.get('blockid') if not id: id = 0 printAddblocktype(cur, page, id) elif section == 'doenable': id = args.get('id') options = "-x enable -i" + id + " -u " + page.username outtext = commands.getoutput(arnoldhome + '/arnold.pl ' + options) redirect(req, 'blockedports?output=' + outtext) elif section == 'domanualblock': ip = args.get('ipadresse') comment = args.get('comment') autoenable = args.get('autoenable') reasonid = args.get('reason') options = "-x disable -a" + ip + " -r" + reasonid + " -u " + page.username if autoenable: options += " -t" + autoenable if comment: options += " -c \"" + comment + "\"" outtext = commands.getoutput(arnoldhome + '/arnold.pl ' + options) redirect(req, 'manualblock?output=' + outtext) elif section == 'doaddblockreason': text = args.get('blockreason') if text is not '': cur.execute("SELECT * FROM blocked_reason WHERE text='%s'" %text) if cur.rowcount < 1: cur.execute("INSERT INTO blocked_reason (text) VALUES ('" + text + "')") conn.commit() redirect(req, 'addreason') elif section == 'doaddblock': # blockid, blocktitle, description, reasonid, newreason, mailfile, inputfile, pursuit, eincrease, duration, active, user reasonid = args.get('reasonid') blockid = args.get('blockid') if not blockid: newreason = args.get('newreason') nr = re.search("^--", newreason) if not nr: cur.execute("SELECT * FROM blocked_reason WHERE text = '%s'" %newreason) if cur.rowcount < 1: cur.execute("INSERT INTO blocked_reason (text) VALUES ('" + newreason + "')") conn.commit() cur.execute("SELECT * FROM blocked_reason WHERE text = '%s'" %newreason) templist = cur.dictfetchone() reasonid = templist['blocked_reasonid'] blocktitle = args.get('blocktitle') blockdesc = args.get('description') mailfile = args.get('mailfile') inputfile = args.get('inputfile') determined = args.get('pursuit') incremental = args.get('eincrease') if incremental == 'on': incremental = 'y' else: incremental = 'n' blocktime = args.get('duration') active = args.get('active') if active == 'on': active = 'y' else: active = 'n' userid = args.get('user') if req.session.has_key('user') and req.session['user'].id > 0: lasteditedby = req.session['user'].name if blockid: cur.execute("UPDATE block SET blocktitle='%s', blockdesc='%s', reasonid=%s, mailfile='%s', inputfile='%s', determined='%s', incremental='%s', blocktime=%s, active='%s', userid='%s', lastedited=now(), lastedituser='%s' WHERE blockid=%s" %(blocktitle, blockdesc, reasonid, mailfile, inputfile, determined, incremental, blocktime, active, userid, lasteditedby, blockid)) else: cur.execute("INSERT INTO block (blocktitle, blockdesc, mailfile, reasonid, determined, incremental, blocktime, userid, active, lastedited, lastedituser, inputfile) VALUES ('%s','%s','%s',%s,'%s','%s',%s,'%s','%s',now(),'%s','%s')" %(blocktitle, blockdesc, mailfile, reasonid, determined, incremental, blocktime, userid, active, lasteditedby, inputfile)) conn.commit() if blockid: redirect(req,'addBlocktype?blockid=%s' %blockid) else: redirect(req,'blocktypes') else: page.head = section page.headersList = ['a'] page.headers = {'a':'b'} page.action = section # Set some page-variables req.content_type = "text/html" req.send_http_header() page.title = "Arnold" req.write(page.respond()) return apache.OK############################################################def setMenu(page): buttonnames = ['History',"Blocked ports","Search","Add blockreason","Manual block","Blocktypes"] buttons = {'History':'history' ,"Blocked ports":'blockedports', "Search":'search', "Add blockreason":'addreason', "Manual block":'manualblock', "Blocktypes":'blocktypes'} page.buttonnames = buttonnames page.buttons = buttons############################################################def printHistory(cur, page, sort, section, days):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -