Index Error: list index out of range in python -


i have project in internet security class. partner started project , wrote python code , have continue stopped. don't know python , planning learn running code , checking how works. when executing code error "indexerror: list index out of range".

import os # deauthenticate devices os.system("python2 ~/downloads/de_auth.py -s 00:22:b0:07:58:d4 -d & sleep 30; kill $!") # renew dhcp on linux "sudo dhclient -v -r & sudo dhclient -v"   # capture dhcp packet os.system("tcpdump -lenx -s 1500 port bootps or port bootpc -v > dhcp.txt & sleep 20; kill $!")  # read packet txt file dhcp_packet = open("dhcp.txt", "r")  # info txt file of saved packet line1 = dhcp_packet.readline() line1 = line1.split() sourcemac = line1[1] destmac = line1[3] ttl = line1[12] length = line1[8]  #parse packet line = dhcp_packet.readline() while "0x0100" not in line:     line = dhcp_packet.readline()  packet = line + dhcp_packet.read()  packet = packet.replace("0x0100:", "") packet = packet.replace("0x0110:", "") packet = packet.replace("0x0120:", "") packet = packet.replace("0x0130:", "") packet = packet.replace("0x0140:", "") packet = packet.replace("0x0150:", "") packet = packet.replace("\n", "") packet = packet.replace(" ", "") packet = packet.replace("   ", "") packet = packet.replace("000000000000000063825363", "")  # locate option (55) = 0x0037 option = "0" i=0 length = 0  while option != "37":     option = packet[i:i+2]     hex_length = packet[i+2:i+4]     length = int(packet[i+2:i+4], 16)     = i+ length*2 + 4  = - int(hex_length, 16)*2 print "option (55): " + packet[i:i+length*2 ] + "\nlength: " + str(length) + " bytes" print "source mac: " + sourcemac 

thank lot

the index error means have empty or undefined section (index) in lists. it's in loop condition @ bottom:

while option != "37":     option = packet[i:i+2]     hex_length = packet[i+2:i+4]     length = int(packet[i+2:i+4], 16)     = i+ length*2 + 4 

alternatively, earlier in reading text file:

# info txt file of saved packet line1 = dhcp_packet.readline() line1 = line1.split() sourcemac = line1[1] destmac = line1[3] ttl = line1[12] length = line1[8] 

try opening text file , make sure lines referred correctly.

if you're new coding , not used understanding error messages or using debugger yet, 1 way find problem area including print ('okay') between lines in code, moving down progressively until line no longer prints.

i'm pretty new python well, find easier learn writing own code , googling want achieve (especially when partner leaves code that...). website provides documentation on in-built commands (choose version @ top): https://docs.python.org/3.4/contents.html, , website contains more in-depth tutorials common functions: http://www.tutorialspoint.com/python/index.htm


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -