#!@PYTHON@ # Copyright (C) 2002 # # This file is part of the pxeconfig utils # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 2, or (at your option) any # later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA # ''' hexls.py - Lists directory contents, but converts the hexadecimal IP numbers to dotted decimal notation (so you can at least see what you're doing..!!) Author: Walter de Jong Date: January 2002 Changes: Bas van der Vlies Date: September 2002 SVN info $Id: hexls.in 100 2007-08-15 06:28:37Z bas $ ''' import os import string import re import socket def hexls(path): '''list directory, convert hexadecimal to dotted decimal''' regexp = re.compile('[' + string.hexdigits + ']+$' ) files = os.listdir(path) files.sort() for f in files: hostname = hostname_resolve = False fullpath = os.path.join(path, f) if os.path.islink(fullpath): symlink_dest = os.readlink(fullpath) else: symlink_dest = None if regexp.match(f) and symlink_dest: size = len(f) digit2 = digit3 = digit4 = 'X' if size >= 2: digit1 = str(string.atoi(f[:2], 16)) if size >= 4: digit2 = str(string.atoi(f[2:4], 16)) if size >= 6: digit3 = str(string.atoi(f[4:6], 16)) if size >= 8: hostname_resolve = True digit4 = str(string.atoi(f[6:], 16)) ipaddr = '%s.%s.%s.%s' % (digit1, digit2, digit3, digit4) if hostname_resolve: try: hostname = socket.gethostbyaddr(ipaddr) except socket.herror, detail: hostname = ( detail[1], 'dummy' ) if hostname: output = '%s => %s => %s' % (f, ipaddr, hostname[0]) else: output = '%s => %s' % (f, ipaddr) output = '%s -> %s' % (output, symlink_dest) print output else: if f[0] != '.': print f if __name__ == '__main__': import sys if len(sys.argv) > 1: for dir in sys.argv[1:]: hexls(dir) else: hexls('.') # EOB