Intégrer des variables ds un threading
Bonjour,
quelqu'un pourrais m'indiquer ou suggérer comment je pourrais incorporer c'est 2 scripts pour valider plusieurs IP (5) pour me donner le status de chacun d'eux. (les 2 scripts fonctionnent a merveille).
script no. 1 (valide un ip) :oops:
-------------------------------------------------
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| import re
ipAddress = raw_input('donnez une adresse ip: ')
def validateIP(ipAddress):
ipRegex = r"^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$"
re_ip = re.compile(ipRegex)
match = re_ip.match(ipAddress)
if not match:
return "erreur, %s n'est pas une adresse IP valide" %(ipAddress)
else:
return "oui, %s est une adresse IP valide" %(ipAddress)
print(validateIP(ipAddress)) |
--------------------------------------------------------------------
script no. 2(ping differents hosts).
-------------------------------------------------------------------
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| import os
import re
import time
import sys
from threading import Thread
class testit(Thread):
def __init__ (self,ip):
Thread.__init__(self)
self.ip = ip
self.status = -1
def run(self):
pingaling = os.popen("ping -q -c2 "+self.ip,"r")
# This code is what each parallel thread does
# the 'run' method is triggered when 'start' is called
while 1:
line = pingaling.readline()
if not line: break
igot = re.findall(testit.lifeline,line)
if igot:
self.status = int(igot[0])
testit.lifeline = re.compile(r"(\d) received")
report = ("No response","Partial Response","Alive")
print time.ctime()
pinglist = []
for host in range(60,70):
ip = "192.168.200."+str(host)
# create a thread, add it to a thread list
current = testit(ip)
pinglist.append(current)
# start the thread running
current.start()
for pingle in pinglist:
# await the completion of each thread in turn
pingle.join()
print "Status from ",pingle.ip,"is",report[pingle.status]
print time.ctime() |
--------------------------------------------------------------------
Edité par Guigui_: merci de penser aux balises [CODE]
Fabriquer une liste - Débutant
Bonjour à tous,
je crois que ma question n'est pas vraiment claire alors je la formule d'une autre façon...
la partie du script ci dessous me donne le résultat d'un ping pour les serveurs
192.168.200.60 jusqu' à 192.168.200.69 alors, je veux changer cette partie(for host in range(60,70): pour une ligne de commande qui me permet de pinger 5 IP que je tappe manuellement, que me conseillé vous comme pour remplacer ce (for host in range(60,70): ?
------------------------------------------------------------------
pinglist = []
for host in range(60,70): <------??????????
ip = "192.168.200."+str(host) <------??????????
# create a thread, add it to a thread list
current = testit(ip)
pinglist.append(current)
# start the thread running
current.start()
------------------------------------------------------------------
Merci/Charlie City