-
Notifications
You must be signed in to change notification settings - Fork 0
/
filezilla2netrc.py
executable file
·74 lines (62 loc) · 2.25 KB
/
filezilla2netrc.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
#!/usr/bin/env python
#
# Convert FileZilla sitemanager.xml to .netrc format for *nix FTP clients (lftp, mc, etc.)
#
# Usage:
# python ~/bin/filezilla2netrc.py <~/.config/filezilla/sitemanager.xml >~/.netrc
# chmod 600 ~/.netrc
from xml.parsers.expat import ExpatError
from xml.dom import minidom
import sys
def main():
DEFAULT_ENTRY = {
'username': None,
'password': None,
'local_dir': None,
'remote_dir': None,
}
try:
xmldoc = minidom.parse(sys.stdin)
except ExpatError:
print >> sys.stderr, 'Error parsing input file'
sys.exit(1)
entries = {}
for server in xmldoc.getElementsByTagName('Server'):
entry = DEFAULT_ENTRY.copy()
tmp = server.getElementsByTagName('Host')
if not tmp:
continue
hostname = tmp[0].firstChild.nodeValue
tmp = server.getElementsByTagName('User')
if tmp and tmp[0].firstChild:
entry['username'] = tmp[0].firstChild.nodeValue
tmp = server.getElementsByTagName('Pass')
if tmp and tmp[0].firstChild:
entry['password'] = tmp[0].firstChild.nodeValue
tmp = server.getElementsByTagName('LocalDir')
if tmp and tmp[0].firstChild:
entry['local_dir'] = tmp[0].firstChild.nodeValue
tmp = server.getElementsByTagName('RemoteDir')
if tmp and tmp[0].firstChild:
entry['remote_dir'] = tmp[0].firstChild.nodeValue.split()[-1]
if len( entry['remote_dir'] ) < 2:
entry['remote_dir'] = ''
entries[hostname] = entry
if entries:
for hostname in entries.keys():
entry = entries[hostname]
print ( 'machine', hostname )
if entry['username']:
print ( 'login', entry['username'] )
if entry['password']:
print ( 'password', entry['password'] )
if entry['local_dir'] or entry['remote_dir']:
print ( 'macdef init' )
if entry['local_dir']:
print ( 'lcd', entry['local_dir'] )
if entry['remote_dir']:
print ( 'cd', entry['remote_dir'] )
print ()
print ()
if __name__ == '__main__':
main()