This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
/
tools.py
84 lines (68 loc) · 2.51 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import json
import os
from elasticsearch import Elasticsearch
import time
import matplotlib.pyplot as plt
import networkx as nx
import sys
from colors import bcolors
def parse_config():
conf_file = 'settings.json'
try:
with open(conf_file, 'r') as read_conf:
conf = json.load(read_conf)
except Exception as e:
print "Unable to parse config file: {0}".format(e)
sys.exit()
return conf
def test_connection():
config = parse_config()
try:
es = Elasticsearch(host=config['elastic']['host'], port=config['elastic']['port'])
print "Succesfully connected to ElasticSearch"
return es
except:
print 'Unable to connect to Elasticsearch. \nCheck your connection and settings.json file'
sys.exit()
def elast(index, doc_type, body):
config = parse_config()
es = Elasticsearch(host=config['elastic']['host'], port=config['elastic']['port'])
# es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
ids = []
print "[*] Saving output to Elasticsearch"
try:
resp = es.search(index=index)
for i in resp['hits']['hits']:
int_id = int(i['_id'])
ids.append(int_id)
last_id = max(ids)
es.index(index=index, doc_type=doc_type, id=last_id + 1, body=body)
except Exception as e:
try:
es.index(index=index, doc_type=doc_type, id=1, body=body)
except Exception as e:
pass
def json_output(name, filename, data):
directory = "output/" + name + "/"
if not os.path.exists(directory):
os.makedirs(directory)
with open(directory + filename + ".json", 'w') as outfile:
json.dump(data, outfile, indent=4, sort_keys=False)
def finding(finding):
print bcolors.OKGREEN + "---------------------------------------------------------" + bcolors.ENDC + finding + bcolors.OKGREEN + "---------------------------------------------------------" + bcolors.ENDC
def save_graph(G, name):
directory = "graph/"
if not os.path.exists(directory):
os.makedirs(directory)
# edges = G.edges
# colors = [G[u][v]['color'] for u, v in edges]
nx.draw(G, with_labels=True) # ,edge_colors=colors)
# plt.figure(figsize=(10,10))
timestr = time.strftime("%Y%m%d-%H%M%S")
print "[*] Saving graph to graph/" + timestr + '-' + name + ".png"
plt.savefig(directory + timestr + '-' + name + ".png")
plt.show()
raw_input("Press Enter to quit...")
sys.exit()
# nx.draw(G, with_labels=True)
# plt.show()