-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze_crumbs.py
173 lines (129 loc) · 4.68 KB
/
analyze_crumbs.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
"""
This is a Titan module
- AnalyzeCrumbs returns paths back
to a centralized server
To use:
sudo pip install --upgrade titantools
"""
import json
import logging
from socket import gethostbyaddr
from urllib2 import urlopen,URLError
from sys import argv, exit, path
# Titan includes
from titantools.orm import TiORM
from titantools.data_science import DataScience
from titantools.system import execute_command as shell_out
from time import time, gmtime, strftime
from os.path import dirname,basename,isfile,realpath
from os import chmod
#from titantools.decorators import run_every_5
# Set Logging Status
logging_enabled = False
# Set datastore directory
DATASTORE = argv[1]
#@run_every_5
class AnalyzeCrumbs(object):
""" AnalyzeCrumbs """
def __init__(self):
self.message = type(self).__name__
self.status = 0
self.datastore = []
# Create config file
config_file = '%s/config.json' % dirname(realpath(__file__))
# Check if config exists
if isfile(config_file):
with open(config_file) as config_file:
self.config = json.load(config_file)
def get_crumbs(self):
"""
Find network interesting information
"""
# First detect a connection
try:
response=urlopen('%s' % self.config['connectivity_target'],timeout=1)
# First get public IP
response = urlopen('%s' % self.config['externalip_target'])
public_ip = response.read().rstrip()
# Get PTR
reversed_dns = gethostbyaddr(public_ip)[0]
# Next get default route gateway
dgw_ip = shell_out("netstat -anr | grep default |awk '{print $2}'").strip().split('\n')
dgw_mac = []
# Grap ARPs to use as name attribute
arps = shell_out("arp -a").split('\n')
# Loop through gateways
for gwip in dgw_ip:
# Create ARP filter based on gw ip
arp_filter = "(%s)" % gwip
# Loop through ARP responses
for arp in arps:
if arp_filter in arp:
dgw_mac.append(arp.split()[3])
# Get traceroute
traceroute = shell_out('traceroute -n -w 3 -q 1 -m 16 %s' % self.config['traceroute_target'])
# Append to master
self.datastore.append({
"name": ', '.join(dgw_mac),
"gateway": ', '.join(dgw_ip),
"public_ip": public_ip,
"reverse_dns": reversed_dns,
"traceroute": traceroute,
"date": exec_date
})
# Set Message
self.message = "ip: %s, gwip: %s, gwmac: %s" % (public_ip, ', '.join(dgw_ip), ', '.join(dgw_mac))
# If no issues, return 0
self.status = 0
# Not internet connection available
except URLError as err:
# Set Message
self.message = "No internet connectivity"
# If no issues, return 0
self.status = 1
def analyze(self):
"""
This is the 'main' method that launches all of the other checks
"""
self.get_crumbs()
return json.JSONEncoder().encode({"status": self.status, "message": self.message})
# Store data in datastore
def store(self):
# Don't bother if there was an issue
if self.status is 0:
# the table definitions are stored in a library file. this is instantiating
# the ORM object and initializing the tables
module_schema_file = '%s/schema.json' % dirname(__file__)
# Is file
if isfile(module_schema_file):
with open(module_schema_file) as schema_file:
schema = json.load(schema_file)
# ORM
ORM = TiORM(DATASTORE)
for k, v in schema.iteritems():
ORM.initialize_table(k, v)
data_science = DataScience(ORM, self.datastore, 'crumbs')
data_science.get_new_entries()
if __name__ == "__main__":
start = time()
# the "exec_date" is used as the "date" field in the datastore
exec_date = strftime("%a, %d %b %Y %H:%M:%S-%Z", gmtime())
###########################################################################
# Gather data
###########################################################################
try:
a = AnalyzeCrumbs()
if a is not None:
output = a.analyze()
a.store()
print output
except Exception, error:
print error
end = time()
# to see how long this module took to execute, launch the module with
# "--log" as a command line argument
if "--log" in argv[1:]:
logging_enabled = True
logging.basicConfig(format='%(message)s', level=logging.INFO)
logging.info("Execution took %s seconds.", str(end - start))