-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_config_connections.py
41 lines (35 loc) · 1.33 KB
/
parse_config_connections.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
#!/usr/bin/env python
import os,sys
import argparse
TESTDATA = """ <add key="SomeConnectionString" value="Data Source=somecomputer.com;Initial Catalog=somecatalog;Integrated Security=false;user id=someuser;password=somepassword;Application Name=someappname" />"""
def parse_section(section_name, line):
section_start_index = line.lower().rfind(section_name)+1+len(section_name)
try:
sec_line = line[section_start_index:len(line)].split(";")[0]
return sec_line
except:
return
def parse_line(line,file_name):
roi = ''
try:
roi = line[line.rfind('value=')+len('value=')+1:len(line)-1]
except:
print(f'err parsing line {line}')
return
user = parse_section("user id", roi)
password = parse_section("password", roi)
source = parse_section("data source", roi)
print(f"found in {file_name}:")
print(f"\t{user}:'{password}'@{source}")
parser = argparse.ArgumentParser()
parser.add_argument('i', help='input file')
args = parser.parse_args()
if os.path.exists(args.i) is False:
print('err, file {args.i} does not exist', file=sys.stderr)
exit(1)
with open(args.i,'r') as fh:
for line in fh.readlines():
line=line.strip()
if 'data source' not in line.lower() or 'password' not in line.lower():
continue
parse_line(line,args.i)