-
Notifications
You must be signed in to change notification settings - Fork 1
/
CSVFeed-IPCalc.py
121 lines (100 loc) · 2.86 KB
/
CSVFeed-IPCalc.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
'''
Script add integer version of an IP address to the service now csv feed.
Author Siggi Bjarnason Copyright 2018
Website http://www.ipcalc.us/ and http://www.icecomputing.com
Following packages need to be installed as administrator
pip install requests
pip install xmltodict
pip install pymysql
'''
# strFilein = "C:/temp/cmdb_ci_server.csv"
# strOutFile = "C:/temp/cmdb_ci_server-fixed.csv"
import sys
import os
import getpass
import time
try:
import tkinter as tk
from tkinter import filedialog
btKinterOK = True
except:
print ("Failed to load tkinter, CLI only mode.")
btKinterOK = False
def getInput(strPrompt):
if sys.version_info[0] > 2 :
return input(strPrompt)
else:
return raw_input(strPrompt)
# end getInput
strFilein = ""
sa = sys.argv
lsa = len(sys.argv)
if lsa > 1:
strFilein = sa[1]
if strFilein == "":
if btKinterOK:
print ("File name to be processed is missing. Opening up a file open dialog box, please select the file you wish to process.")
root = tk.Tk()
root.withdraw()
strFilein = filedialog.askopenfilename(title = "Select CSV file",filetypes = (("CSV files","*.csv"),("Text files","*.txt"),("all files","*.*")))
else:
strFilein = getInput("Please provide full path and filename for the CSV file to be processed: ")
if strFilein == "":
print ("No filename provided unable to continue")
sys.exit()
if os.path.isfile(strFilein):
print ("OK found {}".format(strFilein))
else:
print ("Can't find CSV file {}".format(strFilein))
sys.exit(4)
iLoc = strFilein.rfind(".")
strOutFile = strFilein[:iLoc] + "-fixed" + strFilein[iLoc:]
def DotDec2Int (strValue):
strHex = ""
if ValidateIP(strValue) == False:
# print ("{} is invalid IP".format(strValue))
return 0
# end if
Quads = strValue.split(".")
for Q in Quads:
QuadHex = hex(int(Q))
strwp = "00"+ QuadHex[2:]
strHex = strHex + strwp[-2:]
# next
return int(strHex,16)
# end function
# Function ValidateIP
# Takes in a string and validates that it follows standard IP address format
# Should be four parts with period deliniation
# Each nubmer should be a number from 0 to 255.
def ValidateIP(strToCheck):
Quads = strToCheck.split(".")
if len(Quads) != 4:
return False
# end if
for Q in Quads:
try:
iQuad = int(Q)
except ValueError:
return False
# end try
if iQuad > 255 or iQuad < 0:
return False
# end if
return True
# end function
objFileIn = open(strFilein,"r")
objFileOut = open(strOutFile,"w")
strLine = objFileIn.readline()
objFileOut.write (strLine[:-1] + ",iIPAddr\n")
while strLine:
strLine = objFileIn.readline()
strLineParts = strLine.split(",")
if len(strLineParts)>2:
strIPAddr = strLineParts[2]
strIPAddr = strIPAddr.replace('"' ,"")
iIPAddr = DotDec2Int(strIPAddr)
# iIPAddr = strLineParts[2]
objFileOut.write ("{},{}\n".format(strLine[:-1],iIPAddr))
#end while
print ("All Done, make sure you import {}".format(strOutFile))