472,127 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

logging data truncated

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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. 10 %'10.10.1.1'
  3. 10 %'10.10.1.1'
  4. 20% '10.10.10.2'
  5.  
  6.  
Here is my code.

Expand|Select|Wrap|Line Numbers
  1.  
  2. import re
  3. import time 
  4. import thread 
  5. import os
  6. import sys
  7.  
  8. f=open('c:/tmp/primaryip.xls','w')
  9. f.close()
  10.  
  11. g=open('c:/tmp/workfile.txt','w')
  12. g.close()
  13.  
  14. h=open('c:/tmp/myprimarylogs.txt','w')
  15. h.close()
  16.  
  17. j=open('c:/tmp/secip.xls','w')
  18. j.close()
  19.  
  20. def validIP(ipAdd): 
  21.     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])$" 
  22.     re_ip = re.compile(ipRegex) 
  23.     return re_ip.match(ipAdd)
  24.  
  25. def validIP1(ipAdd1): 
  26.     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])$" 
  27.     re_ip = re.compile(ipRegex) 
  28.     return re_ip.match(ipAdd1)
  29.  
  30. def fil(data):
  31.     patt = re.compile(r'\((\d+)% loss\)')
  32.     patt1 = re.compile(r'(\d+.\d+.\d+.\d)')
  33.     line = str(loss_num) + tab + str(ip) + tab + str(out_time) + '\n' 
  34.     for line in data.split('\n'):
  35.  
  36.         if line.startswith("Ping statistics for"):
  37.             ip = patt1.search(line).group(1)
  38.  
  39.         if patt.search(line):
  40.             loss_num = int(patt.search(line).group(1))
  41.  
  42.             if loss_num >= 2 :
  43.                 s = open('c:/tmp/myprimarylogs.txt', 'a+')
  44.                 s.write("%s '%s'\n" % (loss_num, ip))
  45.                 #s.write("%s '%s'\n" % (loss_num, ip) + time.asctime())
  46.                 s.close()
  47.                 # if loss >= 2, return False - then secondary IPs are pinged
  48.                 print 'loss_num is >= 2 ', loss_num , ip , time.asctime()
  49.                 return False
  50.             else:
  51.                 print 'loss_num is < 2,', loss_num , ip , time.asctime()
  52.                 return True
  53.  
  54. def ping(*fnames):
  55.     g = open(fnames[0], 'w')
  56.     for fn in fnames[1:]:
  57.         f=open(fn, 'r')
  58.         ipList = [line.strip() for line in f.readlines()]
  59.         f.close()
  60.         resList = []
  61.         for ipAdd in ipList:
  62.             pingaling =os.popen("ping %s -n 10" %(ipAdd),"r")
  63.             data = pingaling.read()
  64.             resList.append(data)
  65.             # if loss > 2, ping secondary IPs
  66.             proceed = fil(data)
  67.             if proceed:
  68.                 break       
  69.         g.write('/n'.join(resList))
  70.         if proceed:
  71.             break
  72.     g.close()
  73.  
  74. def get_IPs(fnP, fnS):
  75.     while True:
  76.         ipAddP = raw_input("# Enter Primary IP: ")
  77.         if validIP(ipAddP):
  78.             f = open(fnP, 'w')
  79.             f.write(ipAddP.strip() + "\n")
  80.             f.close()
  81.             break
  82.         else:
  83.             print "Invalid IP."
  84.     ipList = []
  85.     while True:
  86.         while True:
  87.             ipAddS = raw_input("# Enter Secondary IP: ")
  88.             if validIP(ipAddS):
  89.                 ipList.append(ipAddS.strip())
  90.                 break
  91.             else:
  92.                 print "Invalid IP"
  93.         s = raw_input("# Enter another IP? Y/N: " )
  94.         if s.upper() == "N":
  95.             f = open(fnS, 'w')
  96.             f.write('\n'.join(ipList))
  97.             f.close()
  98.             break
  99.  
  100. fnP = 'c:/tmp/primaryip.xls'
  101. fnS = 'c:/tmp/secip.xls'
  102. fnW = 'c:/tmp/workfile.txt'
  103.  
  104. #get_IPs(fnP, fnS)
  105. #ping(fnW, fnP, fnS)
  106.  
  107. def main():
  108.     the_time = time.time()
  109.     start_time = the_time
  110.     end_time = the_time + (60*60)  # 1 hr
  111.     #end_time = the_time + (60*60*24)  # 24 hrs
  112.     get_IPs(fnP, fnS)
  113.     while the_time < end_time:
  114.         ping(fnW, fnP, fnS)
  115.         time.sleep(10)    # wait 60 seconds
  116.         the_time = time.time() # get the new time
  117.  
  118.  
  119. if __name__ == "__main__":
  120.     main()
  121.  
Oct 30 '07 #1
10 2249
bartonc
6,596 Expert 4TB
It's the regex that you are using in the logging routine:
Expand|Select|Wrap|Line Numbers
  1. >>> patt1 = re.compile(r'(\d+.\d+.\d+.\d)')
  2. >>> m = patt1.match('2.192.100.100')
  3. >>> m.group(1)
  4. '2.192.100.1'
  5. >>> 
Oct 31 '07 #2
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 ?


Expand|Select|Wrap|Line Numbers
  1.  
  2. def fil(data):
  3.     patt = re.compile(r'\((\d+)% loss\)')
  4.     patt1 = re.compile(r'(\d+.\d+.\d+.\d+)')
  5.     #line = str(loss_num) + tab + str(ip) + tab + str(out_time) + '\n' 
  6.     for line in data.split('\n'):
  7.  
  8.         if line.startswith("Ping statistics for"):
  9.             ip = patt1.search(line).group(1)
  10.  
  11.         if patt.search(line):
  12.             loss_num = int(patt.search(line).group(1))
  13.  
  14.             if loss_num >= 2 :
  15.                 s = open('c:/tmp/myprimarylogs.txt', 'a+')
  16.                 t = time.asctime()
  17.                 s.write("%s '%s'\n" % (loss_num, ip)), t
  18.                 #s.write("%s '%s'\n" % (loss_num, ip) + time.asctime())
  19.                 s.close()
  20.                 # if loss >= 2, return False - then secondary IPs are pinged
  21.                 print 'Packets loss are >= 2 ', loss_num , ip , time.asctime()
  22.                 return False
  23.             else:
  24.                 print 'Packets loss are < 2,', loss_num , ip , time.asctime()
  25.                 return True
  26.  
  27.  
Nov 2 '07 #3
Hi,

Is someone can help me ?

Brgds/Charlie
Nov 2 '07 #4
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 ?
Nov 2 '07 #5
bartonc
6,596 Expert 4TB
I think the answer to both the time question and the columns in Excel can be found here, in a tab separated values string:
Expand|Select|Wrap|Line Numbers
  1. s.write("%s\t'%s'\t%s\n" % (loss_num, ip, t)) # move the ,t inside the string format parens
Nov 2 '07 #6
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.

Expand|Select|Wrap|Line Numbers
  1. # Enter Primary IP: 11.11.11.11
  2. # Enter Secondary IP: 10.20.0.246
  3. # Enter another IP? Y/N: y
  4. # Enter Secondary IP: 1.2.3.4
  5. # Enter another IP? Y/N: y
  6. # Enter Secondary IP: 10.20.3.173
  7. # Enter another IP? Y/N: y
  8. # Enter Secondary IP: 2.3.6.8
  9. # Enter another IP? Y/N: n
  10. Packets loss are > 2: 100 11.11.11.11 Tue Nov 06 13:02:12 2007
  11. Packets loss are > 2: 12 10.20.0.246 Tue Nov 06 13:03:13 2007
  12. Packets loss are > 2: 100 1.2.3.4 Tue Nov 06 13:06:52 2007
  13. Packets loss are < 2 0 10.20.3.173 Tue Nov 06 13:07:32 2007
  14. Packets loss are > 2: 100 11.11.11.11 Tue Nov 06 13:11:21 2007
  15. Packets loss are > 2: 10 10.20.0.246 Tue Nov 06 13:12:18 2007
  16. Packets loss are > 2: 100 1.2.3.4 Tue Nov 06 13:15:57 2007
  17. Packets loss are < 2 0 10.20.3.173 Tue Nov 06 13:16:36 2007
  18. Packets loss are > 2: 100 11.11.11.11 Tue Nov 06 13:20:25 2007
  19.  
Nov 6 '07 #7
bartonc
6,596 Expert 4TB
Are this your current validators?
Expand|Select|Wrap|Line Numbers
  1. def validIP(ipAdd): 
  2.     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])$" 
  3.     re_ip = re.compile(ipRegex) 
  4.     return re_ip.match(ipAdd)
  5.  
  6. def validIP1(ipAdd1): 
  7.     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])$" 
  8.     re_ip = re.compile(ipRegex) 
  9.     return re_ip.match(ipAdd1)
Which one are you using?
Nov 7 '07 #8
Hi Bartonc,

I use only the first one:

Expand|Select|Wrap|Line Numbers
  1. def validIP(ipAdd):
  2.     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])$" 
  3.     re_ip = re.compile(ipRegex)
  4.     return re_ip.match(ipAdd)
  5.  
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 ?
Nov 7 '07 #9
Hi,

any suggestion ?

Brgds/Charlie
Nov 8 '07 #10
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
Nov 8 '07 #11

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
7 posts views Thread by Kenevel | last post: by
6 posts views Thread by Larry Bates | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.