lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello to all,
thank' s a lot to everybody. Now I've three of your's examples. I will try 
them and tell you  how it goes.
Thanks again
Bye
Claudio


>----Messaggio originale----
>Da: sean@conman.org
>Data: 25/05/2015 10.55
>A: "Claudio Mussoni"<cmussoni@libero.it>, "Lua mailing list"<lua-l@lists.lua.
org>
>Ogg: Re: Retrieve public IP
>
>It was thus said that the Great Claudio Mussoni once stated:
>> Hello to everyone,I need to get the public address from my router with 
LUA. 
>> This is a PHP example:  $my_public_ip = $_SERVER['REMOTE_ADDR'];
>> I searched around but I' ve not found examples.Someone can help me out?
Thank's a lot.ByeClaudio
>
>Here's how I do it:
>
>#!/usr/bin/env lua
>
>-- http://luasnmp.luaforge.net/snmp.html
>
>snmp = require "snmp"
>OID  = snmp.mib.oid
>
>print("  Dest           Mask           If")
>print("----------------------------------------")
>
>router = assert(snmp.open{
>                peer      = arg[1] or "192.168.1.1",
>                community = arg[2] or "public"
>                })
>
>interfaces = {}
>
>do
>  local max = router['ifNumber.0']
>  for i = 1 , max do
>    interfaces[i] = router['ifDescr.' .. tostring(i)]
>  end
>end
>
>setmetatable(interfaces,{ __index = function(t,k) return "(unknown)" end })
>values = 
>{
>  addr = OID "IP-MIB::ipAdEntAddr",
>  mask = OID "IP-MIB::ipAdEntNetMask",
>  intf = OID "IP-MIB::ipAdEntIfIndex"
>}
>
>result = {}
>
>for field,oid in pairs(values) do
>  local tmp    = router[oid]
>  local i      = 1;
>  result[field] = {}
>
>  for k,v in snmp.spairs(tmp) do
>    result[field][i] = v
>    i = i + 1
>  end
>  result.items = i - 1
>end
>
>for i=1,result.items do
>  print(string.format("%-16s",result.addr[i])
>        .. string.format("%-16s",result.mask[i])
>        .. interfaces[result.intf[i]]
>  )
>end
>
>And output:
>
>  Dest           Mask           If
>----------------------------------------
>127.0.0.1       255.0.0.0       lo
>192.168.1.1     255.255.255.0   br0
>74.---.---.---  255.255.255.255 ipsec0
>
>  -spc (But this supposes your router supports SNMP and it's configured)
>
>