Last active
June 7, 2016 23:52
-
-
Save deadbok/db0c42b186bf2f0140f1edff0b0ce820 to your computer and use it in GitHub Desktop.
Script to ping a list of domains and IP's read from a file, and save status as csv
This file contains hidden or 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
''' | |
Created on 06/06/2016 | |
:copyright: (c) 2016 by Martin Grønholdt. | |
:license: GPLv3, see LICENSE for more details. | |
''' | |
import shlex | |
import locale | |
from subprocess import Popen, PIPE | |
import argparse | |
import re | |
import os.path | |
import difflib | |
import threading | |
from queue import Queue | |
__version__ = '0.0.3' | |
encoding = locale.getdefaultlocale()[1] | |
ip_regexp = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | |
host_regexp = r'from\s((\w+\.)+\w+)\s+' | |
results = list() | |
q = Queue() | |
def main(): | |
#Parse command line. | |
arg_parser = argparse.ArgumentParser() | |
arg_parser.add_argument("serverlist", type=argparse.FileType('r'), help="List of servers to ping.") | |
arg_parser.add_argument("csvoutput", type=argparse.FileType('w'), help="CSV output file.") | |
args = arg_parser.parse_args() | |
hosts = list() | |
# Read server list | |
if args.serverlist is None: | |
exit('Error reading input file.') | |
if args.csvoutput is None: | |
exit('Error writing output file.') | |
# Read text input. | |
hosts += args.serverlist.readlines() | |
# Create the queue and thread pool. | |
for i in range(10): | |
t = threading.Thread(target=worker) | |
t.daemon = True | |
t.start() | |
for host in hosts: | |
q.put(host) | |
q.join() | |
print("Sorting"); | |
sorted_results = sorted(results, key=lambda k: k['host']) | |
# Write data to a csv file. | |
for result in sorted_results: | |
args.csvoutput.write(result['host'] + ', ') | |
args.csvoutput.write(result['state'] + ', ') | |
args.csvoutput.write(result['ip'] + ', ') | |
args.csvoutput.write(result['re_host'] + '\n') | |
print("Host: " + result['host']) | |
print("State: " + result['state']) | |
print("IP: " + result['ip']) | |
print("Reply host: " + result['re_host']) | |
print("Diff: " + result['diff']) | |
def worker(): | |
while True: | |
host = q.get() | |
result = dict() | |
#Ping all hosts | |
status = ping_host(host) | |
result['host'] = status[0] | |
result['state'] = status[1] | |
result['ip'] = status[2] | |
result['re_host'] = status[3] | |
if (result['state'] == "Alive"): | |
#Run a diff on all /index.html files. | |
diff = diff_index_page(host) | |
result['diff'] = diff | |
else: | |
result['diff'] = "" | |
results.append(result) | |
q.task_done() | |
def get_simple_cmd_output(cmd): | |
""" | |
Execute an external command and get its output. | |
""" | |
#Split the command and arguments | |
args = shlex.split(cmd) | |
#Return everything from sdtout | |
ret = Popen(args, stdout=PIPE) | |
#Convert to string | |
ret_out = ret.communicate()[0].decode(encoding) | |
#Return code | |
ret = ret.returncode | |
return(ret, ret_out) | |
def ping_host(host): | |
""" | |
Ping a list of hosts | |
""" | |
print( "Pinging " + host.strip()) | |
#ping command | |
cmd = "ping -c 1 " + host.strip() | |
#Run | |
res = get_simple_cmd_output(cmd) | |
if res[0] == 0: | |
state = "Alive" | |
else: | |
state = "No answer." | |
host_re = re.compile(host_regexp) | |
re_host = host_re.search(res[1]) | |
if re_host is not None: | |
re_host = re_host.group(1) | |
else: | |
re_host = "Unknown" | |
ip_re = re.compile(ip_regexp) | |
ip = ip_re.search(res[1]) | |
if ip is not None: | |
ip = ip.group() | |
else: | |
ip = "Unknown" | |
return((host.strip(), state, ip, re_host)) | |
def diff_index_page(host): | |
""" | |
Diff /index.html of the host with last copy. | |
""" | |
host = host.strip() | |
print( "Getting index.html from: " + host) | |
#ping command | |
cmd = "curl -s -L " + host.strip() | |
#Run | |
res = get_simple_cmd_output(cmd) | |
index_file_name = host + "-index.html" | |
#Rename the old one if it is there | |
if os.path.isfile(index_file_name): | |
os.rename(index_file_name, index_file_name + ".old") | |
#Save new index.html | |
index_file = open(index_file_name, "w"); | |
if index_file is not None: | |
index_file.write(res[1]) | |
index_file.close() | |
else: | |
print("Could not write: " + index_file_name); | |
if os.path.isfile(index_file_name + ".old"): | |
print("Diffing: " + index_file_name) | |
#diff command | |
cmd = "diff -u1 " + index_file_name + ".old " + index_file_name | |
#Run | |
res = get_simple_cmd_output(cmd) | |
#old_index_file = open(index_file_name, "r"); | |
#old_index = old_index_file.read() | |
#index_file = open(index_file_name, "r"); | |
#index = index_file.read() | |
#Eeeeew | |
#diff = difflib.unified_diff(old_index, index, fromfile=index_file_name + ".old", tofile=index_file_name) | |
return(res[1]) | |
return("No old index"); | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment