1 | #! /usr/bin/env 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 61 2005-11-22 11:16:17Z bas $ |
---|
32 | ''' |
---|
33 | |
---|
34 | import os |
---|
35 | import string |
---|
36 | import re |
---|
37 | |
---|
38 | def hexls(path): |
---|
39 | '''list directory, convert hexadecimal to dotted decimal''' |
---|
40 | |
---|
41 | regexp = re.compile('[' + string.hexdigits + ']{8}' ) |
---|
42 | for f in os.listdir(path): |
---|
43 | if regexp.match(f) and len(f) == 8: |
---|
44 | digit1 = string.atoi(f[:2], 16) |
---|
45 | digit2 = string.atoi(f[2:4], 16) |
---|
46 | digit3 = string.atoi(f[4:6], 16) |
---|
47 | digit4 = string.atoi(f[6:], 16) |
---|
48 | |
---|
49 | ipaddr = '%d.%d.%d.%d' % (digit1, digit2, digit3, digit4) |
---|
50 | hostname = socket.gethostbyaddr(ipaddr) |
---|
51 | if hostname: |
---|
52 | print '%s => %s => %s' % (f, ipaddr, hostname) |
---|
53 | else: |
---|
54 | print '%s => %s' % (f, ipaddr) |
---|
55 | else: |
---|
56 | if f[0] != '.': |
---|
57 | print f |
---|
58 | |
---|
59 | |
---|
60 | if __name__ == '__main__': |
---|
61 | import sys |
---|
62 | |
---|
63 | if len(sys.argv) > 1: |
---|
64 | for dir in sys.argv[1:]: |
---|
65 | hexls(dir) |
---|
66 | else: |
---|
67 | hexls('.') |
---|
68 | |
---|
69 | # EOB |
---|
70 | |
---|