Hi,
I have to below script,
and everything seems to work except
that if I entered the Primary IP and Secondaries IP, with such
number; 10.100.1.11 or 10.10.10.245 the last numbers of my IP are missing
in my logs file (c:/tmp/myprimarylogs.xls) Here is the result, you see, '10.100.1.1 should be 10.100.1.11
and 10.10.10.2 should be 10.10.10.224 how can I resolve this issue ?
and I need to know How to add the time : time.asctime() beside my IP on below logs ?
is this possible to put every result with a proper column in Excel file (myprimarylogs.xls) as these result are not properly separate all the purcentage will be on column A, all the IP in column B and time in column C. -
-
10 %'10.10.1.1'
-
10 %'10.10.1.1'
-
20% '10.10.10.2'
-
-
Here is my code. -
-
import re
-
import time
-
import thread
-
import os
-
import sys
-
-
f=open('c:/tmp/primaryip.xls','w')
-
f.close()
-
-
g=open('c:/tmp/workfile.txt','w')
-
g.close()
-
-
h=open('c:/tmp/myprimarylogs.txt','w')
-
h.close()
-
-
j=open('c:/tmp/secip.xls','w')
-
j.close()
-
-
def validIP(ipAdd):
-
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)
-
return re_ip.match(ipAdd)
-
-
def validIP1(ipAdd1):
-
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)
-
return re_ip.match(ipAdd1)
-
-
def fil(data):
-
patt = re.compile(r'\((\d+)% loss\)')
-
patt1 = re.compile(r'(\d+.\d+.\d+.\d)')
-
line = str(loss_num) + tab + str(ip) + tab + str(out_time) + '\n'
-
for line in data.split('\n'):
-
-
if line.startswith("Ping statistics for"):
-
ip = patt1.search(line).group(1)
-
-
if patt.search(line):
-
loss_num = int(patt.search(line).group(1))
-
-
if loss_num >= 2 :
-
s = open('c:/tmp/myprimarylogs.txt', 'a+')
-
s.write("%s '%s'\n" % (loss_num, ip))
-
#s.write("%s '%s'\n" % (loss_num, ip) + time.asctime())
-
s.close()
-
# if loss >= 2, return False - then secondary IPs are pinged
-
print 'loss_num is >= 2 ', loss_num , ip , time.asctime()
-
return False
-
else:
-
print 'loss_num is < 2,', loss_num , ip , time.asctime()
-
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 -n 10" %(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(ipAddS.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'.join(ipList))
-
f.close()
-
break
-
-
fnP = 'c:/tmp/primaryip.xls'
-
fnS = 'c:/tmp/secip.xls'
-
fnW = 'c:/tmp/workfile.txt'
-
-
#get_IPs(fnP, fnS)
-
#ping(fnW, fnP, fnS)
-
-
def main():
-
the_time = time.time()
-
start_time = the_time
-
end_time = the_time + (60*60) # 1 hr
-
#end_time = the_time + (60*60*24) # 24 hrs
-
get_IPs(fnP, fnS)
-
while the_time < end_time:
-
ping(fnW, fnP, fnS)
-
time.sleep(10) # wait 60 seconds
-
the_time = time.time() # get the new time
-
-
-
if __name__ == "__main__":
-
main()
-
10 2249
It's the regex that you are using in the logging routine: -
>>> patt1 = re.compile(r'(\d+.\d+.\d+.\d)')
-
>>> m = patt1.match('2.192.100.100')
-
>>> m.group(1)
-
'2.192.100.1'
-
>>>
Hi Bartonc,
thanks did changed the syntaxe now it is ok.
a)I would like to add the time to my logs( SEE line 15 and 16), I did tried
several combinaisons but does not seems to work
b)And how can I add the different type of data within a different column, in this case all my data goes into column A but I want it : A = purcentage, B=IPss, C = time within Excel spreadsheet ? -
-
def fil(data):
-
patt = re.compile(r'\((\d+)% loss\)')
-
patt1 = re.compile(r'(\d+.\d+.\d+.\d+)')
-
#line = str(loss_num) + tab + str(ip) + tab + str(out_time) + '\n'
-
for line in data.split('\n'):
-
-
if line.startswith("Ping statistics for"):
-
ip = patt1.search(line).group(1)
-
-
if patt.search(line):
-
loss_num = int(patt.search(line).group(1))
-
-
if loss_num >= 2 :
-
s = open('c:/tmp/myprimarylogs.txt', 'a+')
-
t = time.asctime()
-
s.write("%s '%s'\n" % (loss_num, ip)), t
-
#s.write("%s '%s'\n" % (loss_num, ip) + time.asctime())
-
s.close()
-
# if loss >= 2, return False - then secondary IPs are pinged
-
print 'Packets loss are >= 2 ', loss_num , ip , time.asctime()
-
return False
-
else:
-
print 'Packets loss are < 2,', loss_num , ip , time.asctime()
-
return True
-
-
Hi,
Is someone can help me ?
Brgds/Charlie
Hi Bartonc,
forget my last question regarding the time,
I found it too.
But need to know, how can I separte my data to different column in Excel
ei: column A = purcentage, B= IP address, C= Time ?
I think the answer to both the time question and the columns in Excel can be found here, in a tab separated values string: - s.write("%s\t'%s'\t%s\n" % (loss_num, ip, t)) # move the ,t inside the string format parens
Hi, I will try your suggestion later.
Bartonc,
According the my script provide to you on this page,
Do you have any idea on why the script does not provide
The ping from my 5th IP below.
the script only ping 4 IPs instead of 5, the
last ip entered: 2.3.6.8 is not pinged at all. -
# Enter Primary IP: 11.11.11.11
-
# Enter Secondary IP: 10.20.0.246
-
# Enter another IP? Y/N: y
-
# Enter Secondary IP: 1.2.3.4
-
# Enter another IP? Y/N: y
-
# Enter Secondary IP: 10.20.3.173
-
# Enter another IP? Y/N: y
-
# Enter Secondary IP: 2.3.6.8
-
# Enter another IP? Y/N: n
-
Packets loss are > 2: 100 11.11.11.11 Tue Nov 06 13:02:12 2007
-
Packets loss are > 2: 12 10.20.0.246 Tue Nov 06 13:03:13 2007
-
Packets loss are > 2: 100 1.2.3.4 Tue Nov 06 13:06:52 2007
-
Packets loss are < 2 0 10.20.3.173 Tue Nov 06 13:07:32 2007
-
Packets loss are > 2: 100 11.11.11.11 Tue Nov 06 13:11:21 2007
-
Packets loss are > 2: 10 10.20.0.246 Tue Nov 06 13:12:18 2007
-
Packets loss are > 2: 100 1.2.3.4 Tue Nov 06 13:15:57 2007
-
Packets loss are < 2 0 10.20.3.173 Tue Nov 06 13:16:36 2007
-
Packets loss are > 2: 100 11.11.11.11 Tue Nov 06 13:20:25 2007
-
Are this your current validators? -
def validIP(ipAdd):
-
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)
-
return re_ip.match(ipAdd)
-
-
def validIP1(ipAdd1):
-
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)
-
return re_ip.match(ipAdd1)
Which one are you using?
Hi Bartonc,
I use only the first one: -
def validIP(ipAdd):
-
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)
-
return re_ip.match(ipAdd)
-
but I think the problem is located more at the filter level:
what I want, is the primary ip to be ping and if the packet lost are more than 2%, I want ALL my secondaries IP to be ping even if they are under 2% packet lost. (but I want all the secondaries IP over 2% packet lost to go to my file: myprimarylogs.txt). Actually the present script stop pinging as soon as one of the secondaries IP are over 2 % packet lost and go back to ping my primary, it does not ping all the way down all the secondaries IPs. What do you suggest ?
Hi,
any suggestion ?
Brgds/Charlie
bvdet 2,851
Expert Mod 2GB
Hi,
any suggestion ?
Brgds/Charlie
Your problem can be corrected by modifying the code in function ping(). When fil() returns True, the 'break' statement breaks out of the for loop. You will need a way of continuing the for loop even though fil() returns True for the secondary IPs. HTH
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
2 posts
views
Thread by john.livermore |
last post: by
|
6 posts
views
Thread by Burkhard Schultheis |
last post: by
|
5 posts
views
Thread by uthuras |
last post: by
|
2 posts
views
Thread by bil.shah |
last post: by
|
reply
views
Thread by David A. Schramm |
last post: by
|
reply
views
Thread by Grip |
last post: by
| |
7 posts
views
Thread by Kenevel |
last post: by
|
6 posts
views
Thread by Larry Bates |
last post: by
| | | | | | | | | | |