Incrémente ip

0/5 (1 avis)

Snippet vu 6 063 fois - Téléchargée 19 fois

Contenu du snippet

Voici un petit script qui incrémente une IP , pour divert programme ( exmple mapper reseaux etc ... )

Source / Exemple :


#!/usr/bin/python
# -*- coding: iso8859-1 -*-

from sys import exit,argv,stdout
from string import split

def BrokenIp(ip_start):
	ip = ""
	for buff in ip_start :
		ip += str(buff)+"."
	ip = ip[:len(ip)-1]
	return ip

def ConverTypeInt(ip):
	liste = []
	for buff in ip :
		liste.append(int(buff))
	return liste

if ( len(argv) != 3 ):
	stdout.write("Argument error , takes 2 arguments given\n")
	exit(0)

ip_start = split(argv[1],'.')
ip_end = split(argv[2],'.')

if ( ip_start[1] > ip_end[1] ) :
	stdout.write("Error rang of ip")
	exit(0)

ip = BrokenIp(ip_start)
ip_start = ConverTypeInt(ip_start)
ip_end = ConverTypeInt(ip_end)

while ( ip != str(argv[2]) ):
	if ( ip == argv[1] ):
		pass
	elif ( ip_start[3] == 255 ):
		ip_start[3] = 0
		ip_start[2] += 1
	elif ( ip_start[2] == 255 ):
		ip_start[2] = 0
		ip_start[1] += 1
	elif ( ip_start[1] == 255 ):
		ip_start[1] = 0
		ip_start[0] += 1
	elif ( ip_start[0] > 256 ):
		break
	ip_start[3] += 1
	ip = BrokenIp(ip_start)
	print ip

Conclusion :


Source inutile , souvent comme sa quand je c pas quoi faire :p

A voir également

Ajouter un commentaire Commentaire
xeolin Messages postés 336 Date d'inscription samedi 26 novembre 2005 Statut Membre Dernière intervention 8 novembre 2011 2
29 nov. 2007 à 23:09
deux petit truc :

pour ta fonction "break ip", il est preferable (pour plus de visibilitee) de la faire comme ca :
def BrokenIp(ip_start):
ip = ip_start[0]
for buff in ip_start[1:] :
ip += "."+str(buff)
return ip

epres pourquoi utiliser "stdout" ? Au lieu de print ?

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.