help index error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dragrid
    New Member
    • Jan 2009
    • 29

    help index error

    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
    Code:
    import os
    import string
    import sys
    import linecache
    
    ping_results = []
    ip_array = ('192.168.1.43', '192.168.1.1', '127.0.0.1')
    for ip in ip_array:
        f = os.popen('ping %s' % (ip)).read()
        out_ping = [item.strip() for item in f.splitlines(True)
    		if item.strip()]
    #f.close()
    
    ping_results.append(out_ping)
    
    for result in ping_results:
    	chk1 = result[4]
    	chk2 = int(''.join([s for s in result[8].split() [-1]
    	if s.isdigit()]))
    	if chk1.startswith("Reply") and chk2 < 80:
                print chk1
                print result[8]
    Last edited by bvdet; Apr 19 '09, 09:15 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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:
    Code:
    >>> result
    ['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),']
    >>>
    Since there are only 7 items in the list, attempting to access the item at list index 8 will produce an IndexError.

    Comment

    • dragrid
      New Member
      • Jan 2009
      • 29

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

      Comment

      Working...