[3] | 1 | #! /usr/bin/env python |
---|
[14] | 2 | # Copyright (C) 2002 |
---|
[6] | 3 | # |
---|
[14] | 4 | # This file is part of the pxeconfig utils |
---|
[6] | 5 | # |
---|
[14] | 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. |
---|
[6] | 10 | # |
---|
[14] | 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. |
---|
[6] | 15 | # |
---|
[14] | 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 | # |
---|
[3] | 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 | |
---|
[18] | 27 | Changes: Bas van der Vlies <basv@sara.nl> |
---|
| 28 | Date: September 2002 |
---|
| 29 | |
---|
[29] | 30 | SVN info |
---|
| 31 | $Id: hexls 64 2006-01-16 09:04:06Z walter $ |
---|
[3] | 32 | ''' |
---|
| 33 | |
---|
| 34 | import os |
---|
| 35 | import string |
---|
| 36 | import re |
---|
[62] | 37 | import socket |
---|
[3] | 38 | |
---|
| 39 | def hexls(path): |
---|
| 40 | '''list directory, convert hexadecimal to dotted decimal''' |
---|
| 41 | |
---|
[17] | 42 | regexp = re.compile('[' + string.hexdigits + ']{8}' ) |
---|
[3] | 43 | for f in os.listdir(path): |
---|
[64] | 44 | fullpath = os.path.join(path, f) |
---|
| 45 | if os.path.islink(fullpath): |
---|
| 46 | symlink_dest = os.readlink(fullpath) |
---|
| 47 | else: |
---|
| 48 | symlink_dest = None |
---|
| 49 | |
---|
[3] | 50 | if regexp.match(f) and len(f) == 8: |
---|
| 51 | digit1 = string.atoi(f[:2], 16) |
---|
| 52 | digit2 = string.atoi(f[2:4], 16) |
---|
| 53 | digit3 = string.atoi(f[4:6], 16) |
---|
| 54 | digit4 = string.atoi(f[6:], 16) |
---|
| 55 | |
---|
[61] | 56 | ipaddr = '%d.%d.%d.%d' % (digit1, digit2, digit3, digit4) |
---|
[63] | 57 | try: |
---|
| 58 | hostname = socket.gethostbyaddr(ipaddr) |
---|
| 59 | except socket.herror, detail: |
---|
| 60 | hostname = ( detail[1], 'dummy' ) |
---|
| 61 | |
---|
[61] | 62 | if hostname: |
---|
[64] | 63 | output = '%s => %s => %s' % (f, ipaddr, hostname[0]) |
---|
[61] | 64 | else: |
---|
[64] | 65 | output = '%s => %s' % (f, ipaddr) |
---|
| 66 | |
---|
| 67 | if symlink_dest: |
---|
| 68 | output = '%s -> %s' % (output, symlink_dest) |
---|
| 69 | |
---|
| 70 | print output |
---|
[3] | 71 | else: |
---|
| 72 | if f[0] != '.': |
---|
| 73 | print f |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | if __name__ == '__main__': |
---|
| 77 | import sys |
---|
| 78 | |
---|
| 79 | if len(sys.argv) > 1: |
---|
| 80 | for dir in sys.argv[1:]: |
---|
| 81 | hexls(dir) |
---|
| 82 | else: |
---|
| 83 | hexls('.') |
---|
| 84 | |
---|
| 85 | # EOB |
---|
| 86 | |
---|