/
wbpass
executable file
·88 lines (79 loc) · 2.48 KB
/
wbpass
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Lector d'arxius winbox
#
# Copyright (c) 2012, Ignacio Torres Masdeu <ignacio@torresmasdeu.name>
#
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
#
import sys
import re
from os.path import basename
progname = basename(sys.argv[0])
def print_help():
print """Forma d’ús: %s [-c] <FITXER>
Lector d'arxius winbox.cfg i wbx
Opcions:
-c Sortida CSV
""" % progname
def clean(item):
bound = '\7f'
keep = ''
for i in range(0,31):
bound += chr(i)
for i in range(32,127):
keep += chr(i)
for i in range(128,255):
keep += chr(i)
retval={}
# La miga del tema és aqui
# Bàsicament el que fem es cercar cadenes de text (ascii 32-127 i ascii
# extés 128-255) que estiguin envoltades per caràcters de control
values = re.findall('[%s][%s]+[%s]' % (bound, keep, bound), item)
for v in values:
for key in keys:
if v[1:len(key)+1] == key:
# Com que la regexp inclou un caràcter de control a
# cada banda hem de tenir-ho en compte per extraure
# l'informació
retval[key] = v[len(key)+1:-1]
return retval
if __name__=='__main__':
CSV=False
if ('-c' in sys.argv):
CSV=True
sys.argv.pop(sys.argv.index('-c'))
try:
f = open(sys.argv[1])
except IndexError:
print "%s: cal especificar un fitxer" % progname
sys.exit(1)
except:
print_help()
sys.exit(1)
keys = 'host login pwd note'.split()
a = f.read()
items = re.split('\x09\x00\x04', a)
if CSV:
print "# host, usuari, contrasenya, nota\n"
for (i, val) in enumerate(items):
items[i] = clean(val)
try:
if (CSV):
print "'%s','%s',''%s','%s'" % (items[i]['host'], items[i]['login'], items[i]['pwd'], items[i]['note'])
else:
print "%s (%s)\n\tUsuari: %s\n\tContrasenya: %s\n" % (items[i]['host'], items[i]['note'], items[i]['login'], items[i]['pwd'])
except KeyError, error:
continue