def nslookup_host(ip: str) -> str: ''' NSlookup host using ip address Return String or None Regex: egrep -o "([A-Za-z ])\w+\.\w+\.\w+\.\w+" ''' # FIXME: Check all returns from nslookup check regex # ** server can't find 93.35.100.10.in-addr.arpa: NXDOMAIN # name = rho.fiu.edu. try: if ip: proc = subprocess.Popen('nslookup ' + ip + ' | egrep -o "([A-Za-z ])\w+\.\w+\.\w+\.\w+"', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) # Clean the return string hostname = proc.communicate()[0].rstrip().strip() except Exception as err: FileIO.log("nslookup command error") try: # Check the response success if hostname: return hostname else: return None except Exception as err: FileIO.log("response error for nslookup_host function")
def get_assest_by_serial(serial: str) -> dict: ''' Get Assest ID by name ''' uri = '/api/v1/hardware?search=' request = server + uri + serial headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token } response = requests.get(request, headers=headers) #print(response.json()) assest_json = response.json() FileIO.save_to_file(assest_json, "caseidb.json") return assest_json
def get_id_by_mac(mac: str) -> dict: ''' Get Assest by Mac Address ''' uri = '/api/v1/hardware?search=' request = server + uri + mac headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token } response = requests.get(request, headers=headers) #print(response.json()) assest_json = response.json() FileIO.save_to_file(data, "caseidb.json") return assest_json
def dblisting(): if request.method == 'POST': #this block is only entered when the form is submitted tag = request.form.get('tag') if tag != None: #host_json = get_assest_by_tag(tag) host_json = FileIO.read_from_file("caseidb.json") FileIO.log(str(host_json)) return render_template('dblisting.html', host=host_json) else: host_json = "" flash('Nothing entered', 'danger') return render_template('dblisting.html', host=host_json) return render_template('dblisting.html')
def get_host(mac: str) -> dict: ''' Calls Network Security API Return JSON object ''' try: if mac == None: err_message="{'error': 'Missing mac address or malformed'}" FileIO.log(str(err_message)) return err_message else: host = json.loads(subprocess.Popen('php linux_app/apis/api.php ' + mac.lower(), shell=True, stdout=subprocess.PIPE, universal_newlines=True).communicate()[0]) return host except Exception as err: FileIO.log(str(err))
def ping_host(ip: str) -> bool: ''' Ping host using ip address or hostname Return True or False ''' try: if ip: response = os.system("ping -W 1 -c 1 " + ip) except Exception as err: FileIO.log("ping command error") try: # Check the response success if response == 0: print(ip, 'is up!') return True else: print(ip, 'is down!') return False except Exception as err: FileIO.log("response error for post_host function") return False
def entermac(): ''' ENTER MAC PAGE - Set up cache http://flask.pocoo.org/docs/1.0/patterns/caching/ ''' if request.method == 'POST': #this block is only entered when the form is submitted macaddr = request.form.get('macaddr') if macaddr != None: host_json = get_host(macaddr) FileIO.log(str(host_json)) # If host found check for ICMP, hostname, and add to database if host_json['status'] != 404: pingable = ping_host(host_json['device']['address']) hostname = nslookup_host(host_json['device']['address']) FileIO.log(host_json['device']['address'], str(pingable), str(hostname)) # Check if host is in the database, if not, commit to DB host_found = check_mac(host_json['device']['hardware']) if not host_found: # FIXME The SQL commits JSON objects, Check for different sizes and keys # new_host = Host(hardware=host_json['device']['hardware'],parent_name= name if host_json['device']['parent_name'] else None , parent_type= type if host_json['device']['parent_type'] else None, parent_port= port if host_json['device']['parent_port'] else None, fingerprint= finger if host_json['device']['fingerprint'] else None, address=host_json['device']['address']) try: new_host = Host( hardware=host_json['device']['hardware'], parent_name=host_json['device']['parent_name'], parent_type=host_json['device']['parent_type'], parent_port=host_json['device']['parent_port'], fingerprint=host_json['device']['fingerprint'], address=host_json['device']['address']) except: new_host = Host( hardware=host_json['device']['hardware'], parent_name=None, parent_type=None, parent_port=None, fingerprint=host_json['device']['fingerprint'], address=host_json['device']['address']) db.session.add(new_host) db.session.commit() # FIXME Make a way to update the database and flag changes to the switchs return render_template('entermac.html', host=host_json, pingable=pingable, hostname=hostname) return render_template('entermac.html', host=host_json) else: host_json = "" flash('Nothing entered', 'danger') return render_template('entermac.html', host=host_json) return render_template('entermac.html')