#!@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 81 2007-03-28 19:27:36Z bas $ ''' import os import string import re import socket def hexls(path): '''list directory, convert hexadecimal to dotted decimal''' regexp = re.compile('[' + string.hexdigits + ']{8}' ) for f in os.listdir(path): 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 len(f) == 8: digit1 = string.atoi(f[:2], 16) digit2 = string.atoi(f[2:4], 16) digit3 = string.atoi(f[4:6], 16) digit4 = string.atoi(f[6:], 16) ipaddr = '%d.%d.%d.%d' % (digit1, digit2, digit3, digit4) 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) if symlink_dest: 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