-
Notifications
You must be signed in to change notification settings - Fork 3
/
BulkImportNetwork.py
executable file
·63 lines (55 loc) · 1.4 KB
/
BulkImportNetwork.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
import pymssql
import csv
import os
import sys
def GetConnectionString():
file = open("./connectionstring.txt", "r")
s = file.read().replace("\n","")
d = dict(item.split(":") for item in s.split(","))
return d
# Connect to the database
connectionString = GetConnectionString()
conn = pymssql.connect(host=connectionString["host"], user=connectionString["user"],
password=connectionString["password"], database=connectionString["database"])
cursor = conn.cursor()
fn = './networkData.txt'
if not os.path.exists(fn):
exit()
try:
sql = ''
with open(fn) as f:
for row in f:
data = (row.strip().split("|"))
if data[-1]== 'n/a':
continue
sql = "insert into network_data(" \
"hostname,timestamp,network_interface," \
"in_or_out,kbps_value) values(" \
"'{0}','{1}','{2}','{3}'," \
"{4})".format( \
data[0],data[1],data[2], \
data[3],data[4]);
sys.stdout.write('.')
sys.stdout.flush()
try:
cursor.execute(sql)
except:
print(sql)
pass
except:
print(sql)
pass
conn.commit()
cursor.close()
conn.close()
"""
for r in data:
print(r)
"""
"""
cursor.execute('select * from dbo.network_data')
row = cursor.fetchone()
while row:
print(str(row[0]) + " " + str(row[1]) + " " + str(row[2]))
row = cursor.fetchone()
"""