The script you provided give the ping to primary IP and than the Secondary,
but what about, if I want to Ping the primary but ping the Secondary ONLY if packet lost of the Primary if greater or equal.
The script you provided give the ping to primary IP and than the Secondary,
but what about, if I want to Ping the primary but ping the Secondary ONLY if packet lost of the Primary if greater or equal.
The def fil() part that I put, Is filtering OK.
Brgds/Carl
I modified your fil() function somewhat. I am using re to get the ip address and percentage loss. This will keep trying IPs until the list runs out or pinging is successful.[code=Python]# imports, def validIP()
def fil(data):
patt = re.compile(r'\( (\d+)% loss\)')
patt1 = re.compile(r'(\ d+.\d+.\d+.\d)' )
for line in data.split('\n' ):
if line.startswith ("Ping statistics for"):
ip = patt1.search(li ne).group(1)
if patt.search(lin e):
loss_num = int(patt.search (line).group(1) )
if loss_num >= 2 :
s = open('myprimary logs.txt', 'a+')
s.write("%s '%s'\n" % (loss_num, ip))
s.close()
# if loss >= 2, return False - then secondary IPs are pinged
print 'loss_num is >= 2 ', loss_num
return False
else:
print 'loss_num is < 2,', loss_num
return True
def ping(*fnames):
g = open(fnames[0], 'w')
for fn in fnames[1:]:
f=open(fn, 'r')
ipList = [line.strip() for line in f.readlines()]
f.close()
resList = []
for ipAdd in ipList:
pingaling =os.popen("ping %s" %(ipAdd),"r")
data = pingaling.read( )
resList.append( data)
# if loss > 2, ping secondary IPs
proceed = fil(data)
if proceed:
break
g.write('/n'.join(resList ))
if proceed:
break
g.close()
def get_IPs(fnP, fnS):
while True:
ipAddP = raw_input("# Enter Primary IP: ")
if validIP(ipAddP) :
f = open(fnP, 'w')
f.write(ipAddP. strip() + "\n")
f.close()
break
else:
print "Invalid IP."
ipList = []
while True:
while True:
ipAddS = raw_input("# Enter Secondary IP: ")
if validIP(ipAddS) :
ipList.append(i pAddS.strip())
break
else:
print "Invalid IP"
s = raw_input("# Enter another IP? Y/N: " )
if s.upper() == "N":
f = open(fnS, 'w')
f.write('\n'.jo in(ipList))
f.close()
break
Comment