#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

import socket,sys

nomfich=''
try:
	nomfich=sys.argv[1]
except IndexError, e:
	print "Passez le nom du fichier en argument"
	sys.exit(-1)

# pas de try sur les fichiers, ca me saoule
fpin = open (nomfich,'r')
fpout= open (nomfich+'.new','w')

separateur=' '
for ligne in fpin:
	# au cas oł gethostbyaddr leve une exception => host inexistant => on recrache tel quel
	newligne=ligne

	#recup de l'ip
	mots=ligne.split()
	ip = mots[0]

	# recup du hostname et reconstruction de la ligne de log apache
	try:
		h_tuple = socket.gethostbyaddr(ip)
		h = h_tuple[0]
		mots[0] = h
		print ip+' => '+h
		newligne = separateur.join(mots) +'\n'
	# peu importe l erreur
	except socket.error, e:
		print e

	# on ressort la nouvelle ligne, modifiee ou non
	fpout.write(newligne)

# pas de test la non plus
fpin.close()
fpout.close()

