4 Replies - 336 Views - Last Post: 28 February 2014 - 02:42 PM Rate Topic: -----

#1 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Regex Causing "list index out of range" Error

Posted 28 February 2014 - 10:31 AM

Well, still being a total Python noob. I searched forums and stuff online to try to make some Regex to get ip addresses our of the ifconfig command. I know in Bash I would just use something like this:

ifconfig | awk -F":" '/inet/ && !/127.0.0.1/ && !/inet6/ {print $2 }' | awk '{ print $1 }'


However I'm wanting to learn how to do this with Python and came out with this:

#!/usr/bin/python

import re
import subprocess
from subprocess import Popen, PIPE

pattern = re.compile(r'inet.*\d+\.\d+\.\d+\.\d+\[^127.0.0.1]')

p1 = Popen(["/sbin/ifconfig"], stdout=subprocess.PIPE)

output = p1.communicate()[0]
result = re.findall(pattern, output)

print result[0]


Here is the error message I now receive:

  File "./dic2.py", line 14, in <module>
    print result[0]
IndexError: list index out of range


I'm not too familiar with the "list index out of range" error, but from what I gathered it means I'm putting more into result than it should hold. Ideally I want this to be able to hold multiple ips, but I only have one beyond localhost right now I'm guessing I'm getting too much data. Any suggestions? I've been trying this a lot of ways and banging my head for a while...

Is This A Good Question/Topic? 0
  • +

Replies To: Regex Causing "list index out of range" Error

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2114
  • View blog
  • Posts: 3,240
  • Joined: 21-June 11

Re: Regex Causing "list index out of range" Error

Posted 28 February 2014 - 10:44 AM

View PostIsrael, on 28 February 2014 - 06:31 PM, said:

I'm not too familiar with the "list index out of range" error, but from what I gathered it means I'm putting more into result than it should hold.


No, there is no limit to how much a list can hold (well other than available memory). Index out of range means that you're trying to get something out that isn't in there. If you're trying to get the 42nd element out of a list that only contains 41 elements, that's an index out of range error.

In this case you're trying to get the first element out of the list result and get that error. This means that result does not contain any elements and thus does not have a first element. In other words the regex never matched and here's why:

inet.*\d+\.\d+\.\d+\.\d+\[^127.0.0.1]


[^127.0.0.1] is a character class that matches any character other than 1, 2, 7, 0 or a dot. That is not what you want here. What you seem to be trying to do is to make sure that the previous part of the regex did not match the string "127.0.0.1", but you can't use character classes like that. In fact there is no regex feature that would allow you to attach an additional condition to a previous subexpression like that.

If you ask me, the easiest way to do what you want, is just to not try to filter out 127.0.0.1 in the regex and instead filter it out when iterating over the results.
Was This Post Helpful? 1
  • +
  • -

#3 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Regex Causing "list index out of range" Error

Posted 28 February 2014 - 11:14 AM

I see what you mean. I took out the [^127.0.0.1] like so:

#!/usr/bin/python

import re
import subprocess
from subprocess import Popen, PIPE

pattern = re.compile(r'inet.*\d+\.\d+\.\d+\.\d+')

p1 = Popen(["/sbin/ifconfig"], stdout=subprocess.PIPE)

output = p1.communicate()[0]
result = re.findall(pattern, output)

print result[0]


And a line with 127.0.0.1 is all I get:

# ./test.py 
inet addr:127.0.0.1  Mask:255.0.0.0


Would you happen to know why its finding 127.0.0.1, but missing 10.0.0.139 listed on my other interface?

wlan2     Link encap:Ethernet  HWaddr 00:1f:1f:7e:a3:de  
          inet addr:10.0.0.139  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::21f:1fff:fe7e:a3de/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:19712 errors:0 dropped:0 overruns:0 frame:0
          TX packets:17179 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:14323745 (13.6 MiB)  TX bytes:3351821 (3.1 MiB)


Thanks for getting me this far.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2114
  • View blog
  • Posts: 3,240
  • Joined: 21-June 11

Re: Regex Causing "list index out of range" Error

Posted 28 February 2014 - 11:35 AM

Because you're only printing the first result. You need to loop over the list to print all results. Then you can add a check inside the loop to throw out the entry for localhost.
Was This Post Helpful? 2
  • +
  • -

#5 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Regex Causing "list index out of range" Error

Posted 28 February 2014 - 02:42 PM

That dawned on me while I was away for my computer a couple hours.

Good call though. Derp!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1