def ssh_check(hostname, username, password, timeout=2, initial_wait=0, interval=0, retries=2): client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy) sleep(initial_wait) for x in range(retries): try: client.connect(hostname=hostname, username=username, password=password, timeout=timeout) client.close() log(RUNTIMELOG, '[SSH to {}]: {}'.format(hostname, 'successfully'), output=True) return True except (BadHostKeyException, AuthenticationException, SSHException, socket.error) as e: log(RUNTIMELOG, '[SSH to {}]: {}'.format(hostname, e), output=True) sleep(interval) return False
def check(client, *args): USERNAME, SNMPv3_SECRET, SSH_SECRET, COMMUNITY = args # check PING status res_ping = ping_check(client) # check SSH status res_ssh = ssh_check(hostname=client, username=USERNAME, password=SSH_SECRET) # check SNMP status res_snmpv2c = not not snmpget( client, '.1.3.6.1.2.1.1.1.0', community=COMMUNITY) res_snmpv3 = not not snmpget(client, '.1.3.6.1.2.1.1.1.0', version='3', user=USERNAME, authkey=SNMPv3_SECRET, privkey=SNMPv3_SECRET) # logs checked hosts if (type(res_ping) is float) and res_ssh and (res_snmpv2c or res_snmpv3): log(CHECKED_HOSTS, client, logtime=False) return res_ping, res_ssh, res_snmpv2c, res_snmpv3
def ping_check(client): res_ping = ping(client, unit='ms') res_ping = res_ping if res_ping is not None else False log(RUNTIMELOG, '[ICMP to {}]: {}'.format( client, 'successfully' if (type(res_ping) is float) else 'timed out'), output=True) return res_ping
def get(self): log(RUNTIMELOG, '[{}]: {} {}'.format(request.remote_addr, request.method, request.url), output=True) ip = request.args['ip'] try: ip_address(ip) except: return {'error': 'not valid IP address'}, 400 # --- MOCK server # from time import sleep # if ip == '1.1.1.1': # sleep(1) # return { # 'ip': ip, # 'icmp': 1.12345, # 'ssh': True, # 'snmpv2c': True, # 'snmpv3': True # } # elif ip == '1.1.1.2': # sleep(1) # return { # 'ip': ip, # 'icmp': False, # 'ssh': False, # 'snmpv2c': False, # 'snmpv3': False # } # ------------------------ res_ping, res_ssh, res_snmpv2, res_snmpv3 = check( ip, USERNAME, SNMPv3_SECRET, SSH_SECRET, COMMUNITY) return { 'ip': ip, 'icmp': res_ping, 'ssh': res_ssh, 'snmpv2c': res_snmpv2, 'snmpv3': res_snmpv3 }
def snmpget(host, oid, community='public', version='2c', user='', authkey='', privkey='', authProtocol=usmHMACMD5AuthProtocol, privProtocol=usmAesCfb128Protocol, port=161): if version == '2c': iterator = getCmd(SnmpEngine(), CommunityData(community), UdpTransportTarget((host, port)), ContextData(), ObjectType(ObjectIdentity(oid))) elif version == '3': iterator = getCmd( SnmpEngine(), UsmUserData(user, authkey, privkey, authProtocol=authProtocol, privProtocol=privProtocol), UdpTransportTarget((host, port)), ContextData(), ObjectType(ObjectIdentity(oid))) errorIndication, errorStatus, errorIndex, varBinds = next(iterator) if errorIndication: # SNMP engine errors log(RUNTIMELOG, '[SNMP{} to {}]: {}'.format('v2c' if version == '2c' else 'v3', host, errorIndication), output=True) return False else: if errorStatus: # SNMP agent errors log(RUNTIMELOG, '[SNMP{} to {}]: {} at {}'.format( 'v2c' if version == '2c' else 'v3', host, errorStatus.prettyPrint(), varBinds[int(errorIndex) - 1] if errorIndex else '?'), output=True) return False else: log(RUNTIMELOG, '[SNMP{} to {}]: {}'.format('v2c' if version == '2c' else 'v3', host, 'successfully'), output=True) return True
def get(self): log(RUNTIMELOG, '[{}]: {} {}'.format(request.remote_addr, request.method, request.url), output=True) return {'version': defaults['VERSION']}