[Sensors] screenlets.sensors.net_get_ip() doesn't necessarily return the correct value

Bug #241557 reported by Scott Severance
2
Affects Status Importance Assigned to Milestone
Individual Screenlets
Fix Released
Low
Unassigned

Bug Description

screenlets.sensors.net_get_ip() doesn't necessarily return the correct IP address. A modern Linux system likely has at least three IPs: 127.0.0.1 (which net_get_ip() ignores), the avahi IP, and the regular IP. The function sometimes grabs the IP for the device eth0:avahi on my machine instead of the correct device: wlan0. But if I was plugged in to a wired connection, the correct device would be eth0. Of course, people who use avahi might want to get the avahi IP instead of wlan0 or eth0 or whatever. So I think that net_get_ip() should instead return a dictionary of all IPs except 127.0.0.1 and let the individual screenlet (such as Sysmonitor) decide which interface(s) to use.

I'm not convinced that parsing ifconfig is the best way to get at the IP address, but I can't think of something better. Here's some sample code (in Ruby because I don't know Python very well. It shouldn't be too hard to translate for someone who knows Python better than I do.)

def net_get_ip()
    dict = {}
    iface = nil

    `ifconfig`.chomp.split("\n").each do |line|
        if line =~ /^([^\s]+)/ # If the line begins with a non-whitespace character, then it identifies an interface
            iface = $1 # This refers to the interface name matched in the regexp above
            iface = nil if iface == 'lo' # Skip the loopback interface
        elsif iface
            if line =~ /inet addr:[\s]?([0-9.]+)/
                dict[iface] = $1 # Again, $1 refers to the IP address matched by the regexp
            end
           iface = nil
        end
    end

    return dict
end

description: updated
Revision history for this message
R.Rublack (salatessen) wrote :

Hello Scott,

i think this is the best way,
and also it should be localized because in my german environment the is no result
because of die localized output of ifconfig.

My workaround is to call LC_ALL=C ifconfig

so I changed to
def net_get_ip(): # by Whise
        """Returns ip if it can"""
        ip = commands.getoutput("LC_ALL=C ifconfig")
        x = 0
        while True:
                ip = ip[ip.find("inet addr:"):]
                ip = ip[10:]
                ipc = ip[:ip.find(chr(32))]
                if ipc != '127.0.0.1' and ipc != None and ipc !='1':

                        return ipc

        return _('Cannot get ip')

Papp Bence (sclegnrbs)
affects: screenlets → indiv-screenlets
summary: - screenlets.sensors.net_get_ip() doesn't necessarily return the correct
- value
+ [Sensors] screenlets.sensors.net_get_ip() doesn't necessarily return the
+ correct value
Märt Põder (boamaod)
Changed in indiv-screenlets:
status: New → Fix Committed
Papp Bence (sclegnrbs)
Changed in indiv-screenlets:
importance: Undecided → Low
Märt Põder (boamaod)
Changed in indiv-screenlets:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.