-
Notifications
You must be signed in to change notification settings - Fork 4
/
qsnmp.py
143 lines (113 loc) · 4.63 KB
/
qsnmp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import flask
import flask_login
from flask import session
from flask_restful import Api
from flask_restful import Resource
from pysnmp.hlapi import getCmd, SnmpEngine, CommunityData, UdpTransportTarget, ContextData, ObjectType, ObjectIdentity
from app import create_app, db
from model import User, HostName
application = create_app()
login_manager = flask_login.LoginManager()
login_manager.init_app(application)
login_manager.session_protection = "strong"
@login_manager.user_loader
def load_user(id):
return User.query.get(int(id))
@application.errorhandler(404)
def page_not_found(e):
return flask.render_template('404.html', error=e), 404
@application.route('/', methods=['GET'])
def main_page():
return (
flask.redirect(flask.url_for('.dashboard_panel'))
if session.get('logged_in')
else flask.redirect(flask.url_for('.login'))
)
@application.route('/register', methods=['GET', 'POST'])
def register():
if flask.request.method == 'GET':
return flask.render_template('register.html')
username = flask.request.form['username']
email = flask.request.form['email']
password = flask.request.form['password']
user = User(username, email, password)
db.session.add(user)
db.session.commit()
flask.flash('User successfully registered')
return flask.redirect(flask.url_for('.login'))
@application.route('/login', methods=['GET', 'POST'])
def login():
if flask.request.method == 'GET':
return flask.render_template('login.html')
username = flask.request.form['login_username']
password = flask.request.form['login_password']
registered_user = User.query.filter_by(username=username).first()
if registered_user is None:
flask.flash('Username is invalid', 'error')
return flask.redirect(flask.url_for('.login'))
if not registered_user.check_password(password):
flask.flash('Password is invalid', 'error')
return flask.redirect(flask.url_for('.login'))
flask_login.login_user(registered_user)
flask.flash('Logged off successfully')
return flask.redirect(flask.url_for('.dashboard_panel'))
@application.route('/Dashboard', methods=['GET'])
@flask_login.login_required
def dashboard_panel():
return flask.render_template('panel.html')
@application.route('/delete/<int:id>/', methods=['GET'])
@flask_login.login_required
def delete_post_item(id):
db.session.query(HostName).filter(HostName.id == id).delete()
db.session.commit()
return flask.redirect(flask.url_for('.ip_adr'))
@application.route('/Hostlist', methods=['GET', 'POST'])
@flask_login.login_required
def ip_adr():
if flask.request.method == 'GET':
fetchlist = HostName.query.all()
return flask.render_template('ip.html', fetchlist=fetchlist)
if flask.request.method == 'POST':
host = HostName(flask.request.form['hostname'])
db.session.add(host)
db.session.commit()
flask.flash('Host successfully Accepted')
return flask.redirect(flask.url_for('.ip_adr'))
########################################
@application.route('/logout', methods=['GET'])
@flask_login.login_required
def logout():
if session.get('logged_in') is not False:
session['logged_in'] = False
flask_login.logout_user()
return flask.redirect(flask.url_for('.main_page'))
@login_manager.unauthorized_handler
def unauthorized_handler():
return flask.render_template('blocked.html')
class MainAPI(Resource):
def get(self, id, oid):
try:
u = HostName.query.filter_by(id=id).first()
session = getCmd(
SnmpEngine(),
CommunityData('public'),
UdpTransportTarget((u.hostname, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid))
)
errorIndication, errorStatus, errorIndex, varBinds = next(session)
if errorIndication:
return flask.flash(f'Error: {errorIndication}')
elif errorStatus:
return flask.flash(
f"{errorStatus.prettyPrint()} at {errorIndex and varBinds[int(errorIndex) - 1] or '?'}"
)
else:
for varBind in varBinds: # SNMP response contents
print(' = '.join([x.prettyPrint() for x in varBind]))
except (TimeoutError, NameError, ReferenceError):
return flask.flash('timed out while connecting to remote host')
# API class for the main page
Api(application).add_resource(MainAPI, '/api/<int:id>/<oid>')
if __name__ == '__main__':
application.run(host='127.0.0.1', port=8080, debug=True) # run app in debug mode on port 8080