Skip to content

Instantly share code, notes, and snippets.

@zduymz
Last active March 26, 2021 23:49
Show Gist options
  • Save zduymz/6ea6e2b41bb3264ec88070718f3e895e to your computer and use it in GitHub Desktop.
Save zduymz/6ea6e2b41bb3264ec88070718f3e895e to your computer and use it in GitHub Desktop.
snmp assignment
snmpd:
build: .
ports:
- "161:161/udp"
privileged: true
FROM polinux/snmpd:alpine
RUN echo 'createUser demo MD5 "12341234" DES' >> /var/lib/net-snmp/snmpd.conf && \
echo "rwuser demo" >> /etc/snmp/snmpd.conf
from easysnmp import Session
import os
import sys
# snmp session requests
class SNMPConn:
def __init__(self, host, user, passwd, privacy_password, privacy_protocol):
self.host = host
self.username = user
self.passwd = passwd
self.pripass = privacy_password
self.privacy_protocol = privacy_protocol
self.session = Session(hostname=self.host, community='public', version=2,
timeout=2, retries=3, use_sprint_value=True,
security_level="auth_with_privacy", security_username=self.username,
auth_protocol="MD5", auth_password=self.passwd,
privacy_protocol= self.privacy_protocol,privacy_password=self.pripass)
def Get(self, oid):
return self.session.get(oid) # get snmp OID
def GetNext(self, oid):
return self.session.get_next(oid) # get net SNMP
def GetBulk(self, oid):
return self.session.get_bulk(oid) # get bulk snmp
def SNMPWalk(self, oid):
return self.session.walk(oid) # get walk snmp
class InvalidChoice(Exception):
pass
# SNMP Connection
conn = None
# oid
oid = ".1.3.6.1.2.1.1.1.0"
PROTOCOLS = {
'1': 'DES',
'2': 'AES'
}
def login_menu():
global conn
os.system('clear')
hostname = input("Input hostname (localhost): ") or "localhost"
username = input("Input Username: " ) or "demo"
auth_password = input("Input auth password : ") or "12341234"
priv_password = input("input privacy password : ") or "12341234"
pp_id = 0
while pp_id < 1 or pp_id > 2:
try:
pp_id = int(input("Input Protocol (1: DES, 2: AES, default is DES) : ") or 1)
except ValueError:
continue
priv_protocol = PROTOCOLS[str(pp_id)]
# print(hostname, username, auth_password, priv_password, priv_protocol)
conn = SNMPConn(hostname, username, auth_password, priv_password, priv_protocol)
def exec_menu(choice):
global conn
os.system('clear')
if choice == 'quit':
sys.exit(0)
try:
ch = int(choice)
if ch == 1:
resp = conn.Get(oid)
print("GetRequest Response: ")
print('\t', resp.value)
elif ch == 2:
resp = conn.GetNext(oid)
print("GetNextRequest Response: ")
print('\t', resp.value)
elif ch == 3:
resp = conn.GetBulk(oid)
print("GetBulkRequest Response: ")
for v in resp:
print('\t', v.value)
elif ch == 4:
resp = conn.SNMPWalk(oid)
print("SNMPWalk Response:")
print('\t', resp.value)
else:
raise InvalidChoice
except ValueError:
print("Invalid Choice")
except InvalidChoice:
print("Invalid Choice")
finally:
input()
main_menu()
def main_menu():
os.system('clear')
print("1. GetRequest")
print("2. GetNextRequest")
print("3. GetBulkRequest")
print("4. SNMPWalk")
print("\n Other choice: quit")
choice = input("\n >> ")
exec_menu(choice)
return
if __name__ == "__main__":
try:
login_menu()
main_menu()
except KeyboardInterrupt:
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment