1 | #!@PYTHON@ |
---|
2 | # Copyright (C) 2002 |
---|
3 | # |
---|
4 | # This file is part of the pxeconfig utils |
---|
5 | # |
---|
6 | # This program is free software; you can redistribute it and/or modify it |
---|
7 | # under the terms of the GNU General Public License as published by the |
---|
8 | # Free Software Foundation; either version 2, or (at your option) any |
---|
9 | # later version. |
---|
10 | # |
---|
11 | # This program is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program; if not, write to the Free Software |
---|
18 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
---|
19 | # |
---|
20 | ''' |
---|
21 | hexls.py - Lists directory contents, but converts the hexadecimal IP numbers |
---|
22 | to dotted decimal notation (so you can at least see what you're doing..!!) |
---|
23 | |
---|
24 | Author: Walter de Jong <walter@sara.nl> |
---|
25 | Date: January 2002 |
---|
26 | |
---|
27 | Changes: Bas van der Vlies <basv@sara.nl> |
---|
28 | Date: September 2002 |
---|
29 | |
---|
30 | SVN info |
---|
31 | $Id: hexls.in 100 2007-08-15 06:28:37Z bas $ |
---|
32 | ''' |
---|
33 | |
---|
34 | import os |
---|
35 | import string |
---|
36 | import re |
---|
37 | import socket |
---|
38 | |
---|
39 | def hexls(path): |
---|
40 | '''list directory, convert hexadecimal to dotted decimal''' |
---|
41 | |
---|
42 | regexp = re.compile('[' + string.hexdigits + ']+$' ) |
---|
43 | files = os.listdir(path) |
---|
44 | files.sort() |
---|
45 | for f in files: |
---|
46 | |
---|
47 | hostname = hostname_resolve = False |
---|
48 | |
---|
49 | fullpath = os.path.join(path, f) |
---|
50 | if os.path.islink(fullpath): |
---|
51 | symlink_dest = os.readlink(fullpath) |
---|
52 | else: |
---|
53 | symlink_dest = None |
---|
54 | |
---|
55 | if regexp.match(f) and symlink_dest: |
---|
56 | size = len(f) |
---|
57 | digit2 = digit3 = digit4 = 'X' |
---|
58 | |
---|
59 | if size >= 2: |
---|
60 | digit1 = str(string.atoi(f[:2], 16)) |
---|
61 | if size >= 4: |
---|
62 | digit2 = str(string.atoi(f[2:4], 16)) |
---|
63 | if size >= 6: |
---|
64 | digit3 = str(string.atoi(f[4:6], 16)) |
---|
65 | if size >= 8: |
---|
66 | hostname_resolve = True |
---|
67 | digit4 = str(string.atoi(f[6:], 16)) |
---|
68 | |
---|
69 | ipaddr = '%s.%s.%s.%s' % (digit1, digit2, digit3, digit4) |
---|
70 | |
---|
71 | if hostname_resolve: |
---|
72 | try: |
---|
73 | hostname = socket.gethostbyaddr(ipaddr) |
---|
74 | except socket.herror, detail: |
---|
75 | hostname = ( detail[1], 'dummy' ) |
---|
76 | |
---|
77 | if hostname: |
---|
78 | output = '%s => %s => %s' % (f, ipaddr, hostname[0]) |
---|
79 | else: |
---|
80 | output = '%s => %s' % (f, ipaddr) |
---|
81 | |
---|
82 | output = '%s -> %s' % (output, symlink_dest) |
---|
83 | |
---|
84 | print output |
---|
85 | else: |
---|
86 | if f[0] != '.': |
---|
87 | print f |
---|
88 | |
---|
89 | |
---|
90 | if __name__ == '__main__': |
---|
91 | import sys |
---|
92 | |
---|
93 | if len(sys.argv) > 1: |
---|
94 | for dir in sys.argv[1:]: |
---|
95 | hexls(dir) |
---|
96 | else: |
---|
97 | hexls('.') |
---|
98 | |
---|
99 | # EOB |
---|
100 | |
---|