Show IP Address


Forum: DSL Ideas and Suggestions
Topic: Show IP Address
started by: friedgold

Posted by friedgold on July 01 2005,21:29
When DSL's network settings are obtained via DHCP the current IP Address is often unknown. Knowing the IP Address is necessary when connecting to the ssh server/ftp server/monkey webserver which come with DSL.

I put together this short lua-fltk script to display the current IP Address of the network device eth0. When run it pops up with a dialog box containing the current IP address and a 'ok' button to dismiss it. I then added it to the fluxbox menu (under System,Net Setup). I thought I'd post it here in the hope people find it useful.

Code Sample
#!/usr/bin/env flua
-- (c) 2005 Richard Osborne
-- Show IP current Address in a dialog box

function get_ip_address(interface)
     local command="ifconfig " .. interface .. " > /tmp/pipe.lua &"
     execute(command)
     local file,err = openfile("/tmp/pipe.lua", "r")
     if not file then
           print("Cannot open file. ".. err)
           exit(1)
     end
     local data = read(file,"*a")
     local label = "inet addr:"
     local i,j = strfind (data,label .. "([%d.]*)")
     if not i then
           return("Not Configured")
     else
           return (strsub (data, i+strlen(label),j))
     end
end

-- Main GUI loop

w = Window{200,80, "IP Address"}
ip = Input{40,10,120,25,"IP:"}
ip.value = get_ip_address("eth0")

ok = Button{65,45,70,25,"&OK"}
function ok:callback()
     flush()
     exit(0)
end

w:end_layout()
w:show()

Posted by ico2 on July 02 2005,16:20
cool
Posted by cbagger01 on July 02 2005,19:37
Nice job.

Looks like a good candidate for addition to a future release.

Posted by newOldUser on July 14 2005,23:39
Friedgold, thank you. This is a neat little piece of code.  

I was looking for something that would display my IP address at startup. I run this code from my .xinitrc and bingo, at start up it displays the IP address.

I have a problem on one of my older machines that causes this code to return a "Not Configured" error the first time that it's run. I don't think it's your code, I think it just takes the machine a little while to set the net.  Because of this I've added an "Again" button the code. The Again button simple tries to get the IP address another time.  On my trouble machine, the second time is always a charm.

Thanks again.

Code with Again button follows:
Code Sample

#!/usr/bin/env flua
-- (c) 2005 Richard Osborne
-- Show IP current Address in a dialog box

function get_ip_address(interface)
    local command="ifconfig " .. interface .. " > /tmp/pipe.lua &"
    execute(command)
    local file,err = openfile("/tmp/pipe.lua", "r")
    if not file then
          print("Cannot open file. ".. err)
          exit(1)
    end
    local data = read(file,"*a")
    local label = "inet addr:"
    local i,j = strfind (data,label .. "([%d.]*)")
    if not i then
          return("Not Configured")
    else
          return (strsub (data, i+strlen(label),j))
    end
end

-- Main GUI loop

w = Window{200,80, "IP Address"}
ip = Input{40,10,120,25,"IP:"}
ip.value = get_ip_address("eth0")
od = Button{100,45,70,25,"&Again"}
ok = Button{10,45,70,25,"&OK"}
function ok:callback()
    flush()
    exit(0)
end

function od:callback()
    ip.value = get_ip_address("eth0")
end
w:end_layout()
w:show()

Posted by Joat_Mone on Aug. 02 2005,02:15
What about those who arent using eth0?  My network device is ath0 (wireless PCMCIA card).  I see where I can change the script, but if you want it universal you might want to have a drop down list where you can select the appropriate device.  Just a suggestion.
Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.