-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHelper.py
128 lines (119 loc) · 4.17 KB
/
Helper.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
__author__ = "Vasilis Giotsas"
__email__ = "<[email protected]>"
# This software is Copyright (C) 2015 The Regents of the University of
# California. All Rights Reserved. Permission to copy, modify, and
# distribute this software and its documentation for educational, research
# and non-profit purposes, without fee, and without a written agreement is
# hereby granted, provided that the above copyright notice, this paragraph
# and the following three paragraphs appear in all copies. Permission to
# make commercial use of this software may be obtained by contacting:
#
# Office of Innovation and Commercialization
#
# 9500 Gilman Drive, Mail Code 0910
#
# University of California
#
# La Jolla, CA 92093-0910
#
# (858) 534-5815
#
#
# This software program and documentation are copyrighted by The Regents of
# the University of California. The software program and documentation are
# supplied “as is”, without any accompanying services from The Regents. The
# Regents does not warrant that the operation of the program will be
# uninterrupted or error-free. The end-user understands that the program
# was developed for research purposes and is advised not to rely
# exclusively on the program for any reason.
#
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
# INCLUDING LOST PR OFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
# DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY
# DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
# SOFTWARE PROVIDED HEREUNDER IS ON AN “AS IS” BASIS, AND THE UNIVERSITY OF
# CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
# ENHANCEMENTS, OR MODIFICATIONS.
from netaddr import *
import os
class Helper:
"""
Collection of helper classes used throughout the program
"""
@staticmethod
def getIPNetwork(ipnet):
"""
Used to check if a string represents an IP prefix or address.
Returns the ip version if a valid prefix/address, otherwise zero.
"""
if len(ipnet) < 7:
return 0
try:
ip = IPNetwork(str(ipnet))
return ip.version
except AddrFormatError:
return 0
@staticmethod
def validateIPNetwork(ipnet):
"""
Checks if a string is a valid IP address or not
"""
try:
IPNetwork(str(ipnet))
return True
except AddrFormatError:
return False
@staticmethod
def convertToAsn32(asn):
"""
Converts the ASN 32-bit dot notation to integer representation
"""
asnTokens = asn.split(".")
upper = int(asnTokens[0])
lower = int(asnTokens[1])
upperShifted = upper << 16
asn = upperShifted | lower
return asn
@staticmethod
def convertPrefixToAddress(address):
"""
Some looking glasses will only accept addresses
as parameters, not prefixes. This functions just
removes the prefix lenght.
"""
if '/' in address:
tokens = address.split("/")
ipTokens = tokens[0].split(".")
if ipTokens[len(ipTokens) - 1] == '0':
ipTokens[len(ipTokens) - 1] = '1'
return '.'.join(ipTokens)
@staticmethod
def saveToFile(filename, output, mode, asn):
"""
Saves the string <output> to the file <filename>
"""
directory = "lg-logs/" + asn + "/"
if not os.path.exists(directory):
os.makedirs(directory)
filepath = directory + filename
f = open(filepath, mode)
f.write(output)
f.flush()
f.close()
return filepath
@staticmethod
def isPositiveInt(s):
"""
Checks if a string represents a positive integer
"""
try:
if int(s) > 0:
return True
else:
return False
except ValueError:
return False