Last active
December 11, 2021 03:55
-
-
Save kism/658175774a1442a96a2c16b9244dcbe9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
import socket | |
import time | |
import sys | |
import sqlite3 | |
from ping3 import ping | |
from sqlite3 import Error | |
iprange = "10.42.0." | |
influxhost = "<influxdb host>" | |
influxport = "<influxdb port>" | |
influxdatabase = "<database name>" | |
hostlist = [] | |
def scanhosts(conn): | |
for i in range(1, 255): | |
print() | |
ip = iprange + str(i) | |
entry = None | |
try: | |
entry = socket.gethostbyaddr(ip) | |
except socket.herror: | |
time.sleep(0) | |
timeepoch = int(time.time()) | |
if entry != None: | |
print('Found: ' + entry[0]) | |
try: | |
add_entry(conn, (entry[0], timeepoch)) | |
except sqlite3.IntegrityError: | |
pass # If the unique check fails, just move on | |
else: | |
print('nothing at: ' + ip, end='\n') | |
def add_entry(conn, entry): | |
# Add entry to the elo table | |
sql = ''' INSERT INTO hosts(hostname,lastalive) | |
VALUES(?,?) ''' | |
cur = conn.cursor() | |
cur.execute(sql, entry) | |
print('adding: ', end='') | |
print(entry) | |
def gethosts(conn): | |
cur = conn.cursor() | |
cur.execute("SELECT hostname FROM hosts") | |
list = cur.fetchall() | |
# print(list) | |
hostnamelist = [] | |
for hostname in list: | |
hostnamelist.append(hostname[0]) | |
print(hostnamelist) | |
return(hostnamelist) | |
def checkhosts(conn): | |
hostlist = gethosts(conn) | |
timeepoch = int(time.time()) | |
timestamp = timeepoch * 1000000000 | |
for host in hostlist: | |
pingresult = ping(host) | |
# print('\n DEBUG ' + host + ' ' + str(pingresult) + '\n') | |
if pingresult == False: | |
result = False | |
else: | |
result = True | |
url = 'http://' + influxhost + ":" + influxport + "/write?db=" + influxdatabase | |
data = "ping" + ',' + 'host=kg.lan' + ',lad=' + host + \ | |
' ' + 'value=' + str(result) + ' ' + str(timestamp) | |
print(url + ' | ' + data) | |
try: | |
x = requests.post(url, data=data) | |
except requests.exceptions.ConnectionError: | |
pass | |
print(x) | |
print('\n') | |
time.sleep(0.1) | |
def create_connection(db_file): | |
# Init to connect to db | |
try: | |
conn = sqlite3.connect(db_file) | |
return conn | |
except Error as e: | |
print(e) | |
return None | |
def create_table(conn, create_table_sql): | |
# init to create table | |
try: | |
c = conn.cursor() | |
c.execute(create_table_sql) | |
except Error as e: | |
print(e) | |
def main(): | |
database = "hosts.db" | |
sql_create_elo_table = """ CREATE TABLE IF NOT EXISTS hosts ( | |
hostname text PRIMARY KEY, | |
lastalive int NOT NULL | |
); """ | |
# create a database connection | |
conn = create_connection(database) | |
if conn is None: | |
print("Error! cannot create the database connection.") | |
create_table(conn, sql_create_elo_table) # keyword, create table | |
if len(sys.argv) == 1: | |
print("Give parameter pls") | |
elif sys.argv[1] == 'scan': | |
scanhosts(conn) | |
elif sys.argv[1] == 'ping': | |
checkhosts(conn) | |
else: | |
print("What have you done?") | |
conn.commit() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment