By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,035 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,035 IT Pros & Developers. It's quick & easy.

help index error

P: 29
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]
Apr 18 '09 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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.
Apr 19 '09 #2

P: 29
Bvdet ... Thanks for answering my question - will try to follow posting guideline better ... appreciate the link
Apr 21 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.