OpenWrt CGI使用指南

CGI默认在/www/cgi-bin文件中,创建一个新的CGI程序helloCGI。代码如下

#!/usr/bin/lua

-- HTTP header
print("Content-Type: text/html")
print("")          -- An empty line

-- body
print("

Hello CGI

") print("

Current time: " .. os.date("%D %T") .. "

更改权限

chmod +x /www/cgi-bin/helloCGI

浏览http:///cgi-bin/helloCGI即可看到结果

OpenWrt CGI使用指南_第1张图片

通过html跳转到CGI

/etc/config/uhttpd中新增一个端口配置

OpenWrt CGI使用指南_第2张图片

config uhttpd 'ServerName' 

	option home '/www/tmp/' 

	list listen_http '0.0.0.0:8080' 

	option cgi_prefix '/cgi-bin'

	list interpreter  ".lua=/usr/bin/lua"

/www/tmp目录下新建index.html文件



<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="refresh" content="0; URL=/cgi-bin/scanwifi" />
head>
<body style="background-color: white">
<a style="color: black; font-family: arial, helvetica, sans-serif;" href="/cgi-bin/scanwifi">LuCI - Lua Configuration Interfacea>
body>
html>

注意配置url和href的跳转链接。

在/www/tmp目录下新建cgi-bin文件夹,并创建helloCGI文件

-- helloCGI
#!/usr/bin/lua

-- HTTP header
print("Content-Type: text/html\n\n")
print("")          -- An empty line

-- body
print("

Hello CGI

") print("

Current time: " .. os.date("%D %T") .. "

/etc/init.d/uhttpd restart重启uhttpd服务,访问openwrtIPAdress:8080即可。

总结:1、配置新端口 2、写html文件,指向cgi 3、写lua cgi程序(可用lua xxx命令来判断lua文件是否能运行)。

你可能感兴趣的:(OpenWRT,Linux)