openwrt19.07中獲取光貓工做溫度的lua腳本

openwrt19.07中獲取光貓工做溫度的lua腳本

轉載註明來源: 本文連接 來自osnosn的博客,寫於 2020-10-25.html

本文的 lua 腳本都是運行在 openwrt 的 shell 中

  • openwrt 自身的 lan IP 改成非 192.168.1.x 網段。
    由於光貓都是 192.168.1.1 這個IP。

光貓 HG6201T

#!/usr/bin/lua

-- for "HG6201T" MODEM, 2020-10
-- --- command line argument, help ---
if(arg[1]==nil or string.len(arg[1])<1) then
  local cmd='   '..arg[0]..'  '
  print('Usage:')
  print(cmd..'origin')
  print(cmd..'string')
  print(cmd..'info')
  print(cmd..'temp')
  os.exit()
end
local show="none"
if(string.lower(string.sub(arg[1],1,1))=="t") then
  show="temp"
elseif(string.lower(string.sub(arg[1],1,1))=="o") then
  show="origin"
elseif(string.lower(string.sub(arg[1],1,1))=="s") then
  show="str"
elseif(string.lower(string.sub(arg[1],1,1))=="i") then
  show="info"
end

-- --- get MODEM Temperature ---
require("luci.sys")
string.split = function(s, p)
    local rt= {}
    string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end )
    return rt
end

-- If someone logs in, prevent being logged out.
-- Because one login, the whole network can access the MODEM.
-- First get the PON information directly.
local ponurl="http://192.168.1.1:8080/cgi-bin/pon_link_info.cgi?_=1234560123456"
local pon=luci.sys.httpget(ponurl)
if(string.find(pon,'window.location.href =')~=nil)
then
  print("login and get.\n")
  -- login
  url="http://192.168.1.1:8080/cgi-bin/login.htm.cgi?username=telecomadmin&password=nE7jA%5m"
  getcmd="/bin/wget --no-check-certificate -qO- '%s'" %url
  a=luci.sys.exec(getcmd)

  getcmd="/bin/wget --no-check-certificate -qO- '%s'" % ponurl
  pon=luci.sys.exec(getcmd)

  -- logout
  url="http://192.168.1.1:8080/cgi-bin/exit.htm.cgi"
  getcmd="/bin/wget --no-check-certificate -qO- '%s'" %url
  a=luci.sys.exec(getcmd)
end

if(show=="origin") then
  print(pon)
  os.exit()
end

require("luci.jsonc")
ponTable=luci.jsonc.parse(pon)

if(show=="str") then
  print(ponTable["PONINFO"]["poninfo_COUNstr"])
  os.exit()
end

coun=string.split(ponTable["PONINFO"]["poninfo_COUNstr"],"&")

if(show=="temp") then
  print('Temperature:',coun[4])
  os.exit()
end

if(show=="info") then
  print('Send(dBm):',coun[1])
  print('Receive(dBm):',coun[2])
  print('Voltage(V):',coun[3])
  print('Current(mA):',coun[5])
  print('Temperature:',coun[4])
  os.exit()
end

光貓 TG1400ct

#!/usr/bin/lua

-- for "TG1400ct" MODEM, 2020-10
-- --- command line argument, help ---
if(arg[1]==nil or string.len(arg[1])<1) then
  local cmd='   '..arg[0]..'  '
  print('Usage:')
  print(cmd..'origin')
  print(cmd..'string')
  print(cmd..'info')
  print(cmd..'temp')
  os.exit()
end
local show="none"
if(string.lower(string.sub(arg[1],1,1))=="t") then
  show="temp"
elseif(string.lower(string.sub(arg[1],1,1))=="o") then
  show="origin"
elseif(string.lower(string.sub(arg[1],1,1))=="s") then
  show="str"
elseif(string.lower(string.sub(arg[1],1,1))=="i") then
  show="info"
end

-- --- get MODEM Temperature ---
require("luci.sys")
string.split = function(s, p)
    local rt= {}
    string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end )
    return rt
end

-- If someone logs in, prevent being logged out.
-- Because one login, the whole network can access the MODEM.
-- First get the PON information directly.
local ponurl="http://192.168.1.1/status_gpon.asp"
local pon=luci.sys.httpget(ponurl)
if(string.find(pon,'type="password"')~=nil)
then
  print("login and get.\n")
  -- login
  -- post data need "&" at begin and end of the string
  postdata='&psd=nE7jA%255m&username=telecomadmin'
  url="http://192.168.1.1/boaform/admin/formLogin"
  getcmd="/bin/wget --no-check-certificate --post-data='%s' -qO- '%s'" % {postdata, url}
  a=luci.sys.exec(getcmd)

  getcmd="/bin/wget --no-check-certificate -qO- '%s'" % ponurl
  pon=luci.sys.exec(getcmd)

  -- logout
  url="http://192.168.1.1/boaform/admin/formLogout"
  getcmd="/bin/wget --no-check-certificate -qO- '%s'" %url
  a=luci.sys.exec(getcmd)
end

nn1=string.find(pon,'<table class="flat" border="1" cellpadding="1" cellspacing="1">')
nn2=string.find(pon,'<table class="flat" border="1" cellpadding="1" cellspacing="1">',nn1+2)
nn3=string.find(pon,'</table>',nn2+2)
pon_sector=string.sub(pon,nn2,nn3+7)
pon_sector=(string.gsub(pon_sector,'\t*',''))

if(show=="origin") then
  print(pon_sector)
  os.exit()
end

pon_sector=(string.gsub(pon_sector,'</td>','##'))
pon_sector=(string.gsub(pon_sector,'<.->',''))
pon_sector=(string.gsub(pon_sector,'\n',''))

if(show=="str") then
  print(pon_sector)
  os.exit()
end

coun=string.split(pon_sector,"##")

if(show=="temp") then
  print('Temperature:',coun[6])
  os.exit()
end

if(show=="info") then
  print('Send(dBm):',coun[2])
  print('Receive(dBm):',coun[4])
  print('Voltage(V):',coun[8])
  print('Current(mA):',coun[10])
  print('Temperature:',coun[6])
  os.exit()
end

光貓 TEWA-708G

  • 這款光貓,用 openwrt 缺省的 wget 實在是沒法登陸。
    只好裝個 curl , 在路由中執行opkg install curl,才搞定登陸。
#!/usr/bin/lua

-- for "TEWA-708G" MODEM, 2020-10
-- --- command line argument, help ---
if(arg[1]==nil or string.len(arg[1])<1) then
  local cmd='   '..arg[0]..'  '
  print('Usage:')
  print(cmd..'origin')
  print(cmd..'string')
  print(cmd..'info')
  print(cmd..'temp')
  os.exit()
end
local show="none"
if(string.lower(string.sub(arg[1],1,1))=="t") then
  show="temp"
elseif(string.lower(string.sub(arg[1],1,1))=="o") then
  show="origin"
elseif(string.lower(string.sub(arg[1],1,1))=="s") then
  show="str"
elseif(string.lower(string.sub(arg[1],1,1))=="i") then
  show="info"
end

-- --- get MODEM Temperature ---
require("luci.sys")
string.split = function(s, p)
    local rt= {}
    string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end )
    return rt
end

-- If someone logs in, prevent being logged out.
-- Because one login, the whole network can access the MODEM.
-- First get the PON information directly.
local ponurl="http://192.168.1.1:8080/SW_Pon_Linkinfo.html"
local pon=luci.sys.httpget(ponurl)
if(string.find(pon,':8080/login.html')~=nil)
then
  -- login
  url="http://192.168.1.1:8080/login.cgi"
  local post='&username=telecomadmin&password=nE7jA%255m&'
  -- wget post always FAIL to login
  --getcmd="wget --no-check-certificate --post-data='%s' -qO- '%s'" % { post, url }
  getcmd="curl -s -d '%s' -X POST 'http://192.168.1.1:8080/login.cgi'" % {post,url}
  a=luci.sys.exec(getcmd)
 
  getcmd="wget --no-check-certificate -qO- '%s'" % ponurl
  pon=luci.sys.exec(getcmd)

  -- logout
  url="http://192.168.1.1:8080/logout.cgi?sessionKey=1234567890"
  getcmd="wget --no-check-certificate -qO- '%s'" %url
  a=luci.sys.exec(getcmd)
end

pon=string.match(pon,"obj3Items = '[^']+';")

if (pon == nil or #pon <10) then
  print("login failed.")
  os.exit()
end

if(show=="origin") then
  print(pon)
  os.exit()
end

pon=string.sub(pon,14,-4)

if(show=="str") then
  print(pon)
  os.exit()
end

coun0=string.split(pon,"|")
coun2=string.split(coun0[1],"/")
coun1=string.split(coun0[2],"/")

if(show=="temp") then
  print('Temperature:',coun2[1])
  os.exit()
end

if(show=="info") then
  print('Send(dBm):',coun2[4])
  print('Receive(dBm):',coun2[5])
  print('Voltage(V):',coun2[2])
  print('Current(mA):',coun2[3])
  print('Temperature:',coun2[1])
  os.exit()
end

轉載註明來源: 本文連接 來自osnosn的博客.shell

相關文章
相關標籤/搜索