Connecting Tech Pros Worldwide Forums | Help | Site Map

help index error

Newbie
 
Join Date: Jan 2009
Posts: 14
#1: Apr 18 '09
when I run program below I am getting an index error on line 18
chk2 = .......
indexerror : list index out of range
Any help appreciated
Thx
Expand|Select|Wrap|Line Numbers
  1. import os
  2. import string
  3. import sys
  4. import linecache
  5.  
  6. ping_results = []
  7. ip_array = ('192.168.1.43', '192.168.1.1', '127.0.0.1')
  8. for ip in ip_array:
  9.     f = os.popen('ping %s' % (ip)).read()
  10.     out_ping = [item.strip() for item in f.splitlines(True)
  11.         if item.strip()]
  12. #f.close()
  13.  
  14. ping_results.append(out_ping)
  15.  
  16. for result in ping_results:
  17.     chk1 = result[4]
  18.     chk2 = int(''.join([s for s in result[8].split() [-1]
  19.     if s.isdigit()]))
  20.     if chk1.startswith("Reply") and chk2 < 80:
  21.             print chk1
  22.             print result[8]

bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,566
#2: Apr 19 '09

re: help index error


Please use code tags. See "How to ask a question" in Posting Guidelines. When a ping request makes no connection, the data returned looks like this:
Expand|Select|Wrap|Line Numbers
  1. >>> result
  2. ['Pinging 192.168.1.43 with 32 bytes of data:', 'Request timed out.', 'Request timed out.', 'Request timed out.', 'Request timed out.', 'Ping statistics for 192.168.1.43:', 'Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),']
  3. >>> 
Since there are only 7 items in the list, attempting to access the item at list index 8 will produce an IndexError.
Newbie
 
Join Date: Jan 2009
Posts: 14
#3: Apr 21 '09

re: help index error


Bvdet ... Thanks for answering my question - will try to follow posting guideline better ... appreciate the link
Reply