├── README.md ├── lua ├── README.md ├── crontab.lua ├── lib │ ├── email.lua │ └── gpio.lua ├── test.gpio.lua └── test.mail.lua ├── python ├── README.md ├── email.py └── sys.py └── shell ├── README.md ├── aps.sh └── getip.sh /README.md: -------------------------------------------------------------------------------- 1 | # About 2 | 3 | WRTnode开发中写的几个脚本,主要实现了openwrt下面GPIO控制、系统信息获取、wifi扫描器、定时发送邮件系统报警等功能。 4 | 5 | 6 | --------------------------------------------- 7 | 8 | 9 | # How to use 10 | 11 | ### 0x01.GPIO.lua 12 | 13 | * GPIO.mode(id, “out/in”) 14 | 15 | * GPIO.write(value) 16 | 17 | * GPIO.read() 18 | 19 | 20 | 先设置设置模式,GPIO.mode(id, “out/in”)两种模式之一 21 | 22 | 如果为’out’即可调用GPIO.write(value)函数,写入当然id端口,如果为’in’模式,只能调用GPIO.read()读取数值。 23 | 24 | 这里数值只能是0或1,非0即为1. 25 | 26 | 调用方式如下,这个存在一个可忽略的问题,一旦调用mode,数值将被置为默认数值,即0: 27 | 28 | 29 | ``` 30 | #!/usr/bin/lua 31 | x=require("gpio") 32 | print("Please input io id =>") 33 | id = io.read("*num") 34 | x.mode(id, "out")-- 设置io的模式为输入还是输出 [in/out] 35 | function readGPIO(id) 36 | value = x.read() 37 | print("read data from => `"..id.."` =>"..value) 38 | end 39 | function writeGPIO(id, data) 40 | x.write(data) 41 | print("write data to => `"..id.."` =>"..data) 42 | end 43 | 44 | count=1 45 | repeat 46 | count=count+1 47 | print("Please input value =>") 48 | data = io.read("*num") 49 | writeGPIO(id, data) 50 | readGPIO(id) 51 | until count>3 52 | 53 | ``` 54 | 55 | ### 0x02.Email.lua 56 | 57 | 填写账号信息,即可发送: 58 | 59 | ``` 60 | #!/usr/bin/lua 61 | local mail = require("email") 62 | local data = {} 63 | data.user = {["from"]="sender@gmail.com", ["to"]="receiver@gmail.com", ["password"]="password"} 64 | data.mail = {["subject"]="测试邮件模块", ["body"]="这是主体内容..."} 65 | data.sys = {["server"]="smtp.gmail.com", ["port"]=587} 66 | 67 | mail.set(data) 68 | mail.send() 69 | 70 | ``` 71 | 72 | --------------------------------------------- 73 | 74 | 75 | # Version 76 | 77 | 15.04.30 78 | 79 | * first version 80 | 81 | 82 | --------------------------------------------- 83 | 84 | 85 | # License 86 | 87 | GPL 88 | 89 | --------------------------------------------- 90 | 91 | 详细请看下面介绍,近期会更新: 92 | 93 | [http://homeway.me/2015/04/29/openwrt-develop-base-util/](http://homeway.me/2015/04/29/openwrt-develop-base-util/) 94 | 95 | -------------------------------------------------------------------------------- /lua/README.md: -------------------------------------------------------------------------------- 1 | # About 2 | 3 | WRTnode开发中几个脚本包,目前主要包含定时器、GPIO、发邮件等。 4 | 5 | # include: 6 | 7 | * Email 8 | * GPIO 9 | 10 | # How To Use 11 | 12 | See test file 13 | -------------------------------------------------------------------------------- /lua/crontab.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | 3 | --[[ 4 | Copyright 2015 http://homeway.me 5 | 6 | @author homeway 7 | @version 15.04.29 8 | @link http://homeway.me 9 | @function: crontab schedule, send email to you about system information 10 | 11 | how to use: 12 | step1: configure you email information in this script 13 | step2: mkdir /root/log && mkdir /root/script 14 | step3: mv /path/to/crontab.lua /root/script/send 15 | step4: chmod +x /root/script/send 16 | step5: echo "0,10,20,30,40,50 * * * * /root/script/send" > /root/script/schedule 17 | step6: crontab /root/script/schedule 18 | 19 | must install uci、luasocket(http://see.sl088.com/wiki/Luasocket)、python 20 | 21 | ]]-- 22 | 23 | 24 | local smtp = require("socket.smtp") 25 | local M ={} 26 | 27 | M.user = {["from"]="", ["to"]="", ["password"]=""} 28 | M.mail = {["subject"]="", ["body"]=""} 29 | M.sys = {["server"]=""} 30 | M.data = "" 31 | M.sysPath = "/root/log/sys.log" 32 | 33 | M.set = function(data) 34 | M.user = data.user 35 | M.mail = data.mail 36 | M.sys = data.sys 37 | end 38 | 39 | M.send = function() 40 | rcpt = { 41 | M.user["to"] 42 | } 43 | mesgt = { 44 | headers = { 45 | from = M.user["from"], 46 | to = M.user["to"], --收件人 47 | cc = "", --抄送 48 | subject = M.mail["subject"] --主题 49 | }, 50 | body = M.mail["body"] 51 | } 52 | 53 | r, e = smtp.send{ 54 | from = M.user["from"], 55 | rcpt = rcpt, 56 | source = smtp.message(mesgt), 57 | server = M.sys["server"], 58 | user = M.user["from"], 59 | password = M.user["password"], 60 | } 61 | if not r then 62 | print(e) 63 | else 64 | print("send ok!") 65 | end 66 | end 67 | 68 | M.readFile = function(where) 69 | local fp=io.open(where, 'r') 70 | if fp~=nil then 71 | data = fp:read("*all") 72 | fp:close() 73 | return data 74 | end 75 | return nil 76 | end 77 | 78 | os.execute("/root/script/sysinfo.py > "..M.sysPath) 79 | M.msg = M.readFile(M.sysPath) 80 | 81 | local data = {} 82 | data.user = {["from"]="", ["to"]="", ["password"]=""} 83 | data.mail = {["subject"]="系统当前信息", ["body"]=M.data} 84 | data.sys = {["server"]="smtp.qq.com"} 85 | 86 | M.set(data) 87 | M.send() -------------------------------------------------------------------------------- /lua/lib/email.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | --[[ 3 | Copyright 2015 http://homeway.me 4 | 5 | @author homeway 6 | @version 15.04.29 7 | @link http://homeway.me 8 | @function lua email module 9 | 10 | ]]-- 11 | 12 | local smtp = require("socket.smtp") 13 | local M ={} 14 | 15 | M.user = {["from"]="", ["to"]="", ["password"]=""} 16 | M.mail = {["subject"]="", ["body"]=""} 17 | M.sys = {["server"]=""} 18 | 19 | M.set = function(data) 20 | M.user = data.user 21 | M.mail = data.mail 22 | M.sys = data.sys 23 | end 24 | 25 | M.send = function() 26 | rcpt = { 27 | M.user["to"] 28 | } 29 | mesgt = { 30 | headers = { 31 | from = M.user["from"], 32 | to = M.user["to"], --收件人 33 | cc = "", --抄送 34 | subject = M.mail["subject"] --主题 35 | }, 36 | body = M.mail["body"] 37 | } 38 | 39 | r, e = smtp.send{ 40 | from = M.user["from"], 41 | rcpt = rcpt, 42 | source = smtp.message(mesgt), 43 | server = M.sys["server"], 44 | port = M.sys["port"], 45 | user = M.user["from"], 46 | password = M.user["password"], 47 | } 48 | if not r then 49 | print(e) 50 | else 51 | print("send ok!") 52 | end 53 | end 54 | return M -------------------------------------------------------------------------------- /lua/lib/gpio.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | --[[ 3 | Copyright 2015 http://homeway.me 4 | 5 | @author homeway 6 | @version 15.04.29 7 | @link http://homeway.me 8 | @function OpenWRT gpio module 9 | 10 | ]]-- 11 | 12 | local M = {} 13 | 14 | M.id = "" 15 | M.path = "/sys/class/gpio/gpio" 16 | M.router = "/sys/class/gpio" 17 | 18 | M.check = function(where) 19 | print("check path => "..where) 20 | local f=io.open(where, "r") 21 | if f~=nil then io.close(f) return true else return false end 22 | end 23 | 24 | -- set mode && check type 25 | M.mode = function(id, mtype) 26 | M.id = id 27 | where = M.path..M.id 28 | -- if id not use 29 | if false==M.check(M.path..id..'/direction') then 30 | --M.writeFile(M.router.."/unexport",id) 31 | M.writeFile(M.router.."/export", id) 32 | end 33 | -- if type different 34 | if mtype ~= M.readFile(M.path..id..'/direction') then 35 | print("type =>"..mtype.." direction=>"..M.readFile(M.path..id..'/direction').." different") 36 | M.writeFile(M.path..id..'/direction', mtype) 37 | end 38 | end 39 | 40 | -- file write 41 | M.writeFile = function(where, what) 42 | print("write path => "..where.." data =>"..what) 43 | local fp=io.open(where, 'w') 44 | fp:write(what) 45 | fp:close() 46 | end 47 | 48 | -- file read 49 | M.readFile = function(where) 50 | print("read path => "..where) 51 | local fp=io.open(where, 'r') 52 | if fp~=nil then 53 | data = fp:read("*all") 54 | fp:close() 55 | return data 56 | end 57 | return nil 58 | end 59 | 60 | M.set = function(id) 61 | M.id = id 62 | end 63 | 64 | M.read = function() 65 | res = M.readFile(M.path..M.id..'/value') 66 | return res 67 | end 68 | 69 | M.write = function(value) 70 | res = M.writeFile(M.path..M.id..'/value', value) 71 | end 72 | 73 | M.close = function() 74 | print("sleep io => "..M.id) 75 | os.execute("sleep " .. tonumber(M.id)) 76 | end 77 | 78 | return M -------------------------------------------------------------------------------- /lua/test.gpio.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | --[[ 3 | Copyright 2015 http://homeway.me 4 | 5 | @author homeway 6 | @version 15.04.29 7 | @link http://homeway.me 8 | @function OpenWRT gpio module test 9 | 10 | ]]-- 11 | 12 | x=require("lib.gpio") 13 | print("Please input io id =>") 14 | id = io.read("*num") 15 | x.mode(id, "out") 16 | 17 | function readGPIO(id) 18 | value = x.read() 19 | print("read data from => `"..id.."` =>"..value) 20 | end 21 | 22 | function writeGPIO(id, data) 23 | x.write(data) 24 | print("write data to => `"..id.."` =>"..data) 25 | end 26 | 27 | --readGPIO(id) 28 | 29 | count=1 30 | repeat 31 | count=count+1 32 | print("Please input value =>") 33 | data = io.read("*num") 34 | 35 | writeGPIO(id, data) 36 | readGPIO(id) 37 | --x.close(id) 38 | until count>3 -------------------------------------------------------------------------------- /lua/test.mail.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | 3 | --[[ 4 | Copyright 2015 http://homeway.me 5 | 6 | @author homeway 7 | @version 15.04.29 8 | @link http://homeway.me 9 | @function email send test 10 | 11 | ]]-- 12 | 13 | local mail = require("lib.email") 14 | 15 | local data = {} 16 | data.user = {["from"]="sender@gmail.com", ["to"]="receiver@gmail.com", ["password"]="password"} 17 | data.mail = {["subject"]="测试邮件模块", ["body"]="这是主体内容..."} 18 | data.sys = {["server"]="smtp.gmail.com", ["port"]=587} 19 | 20 | mail.set(data) 21 | mail.send() -------------------------------------------------------------------------------- /python/README.md: -------------------------------------------------------------------------------- 1 | # About 2 | 3 | WRTnode开发中几个脚本包,目前主要包含Email、获取system信息等。 4 | 5 | # include: 6 | 7 | * Email 8 | * Sys 9 | 10 | # How To Use 11 | 12 | See test file 13 | -------------------------------------------------------------------------------- /python/email.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | ''' 4 | @author homeway 5 | @version 15.04.29 6 | @link http://homeway.me 7 | @function python send email 8 | ''' 9 | 10 | import smtplib 11 | smtpObj = smtplib.SMTP("mstp.qq.com", 25) 12 | 13 | sender = 'sender@gmail.com' 14 | receivers = ['receivers@gmail.com'] 15 | 16 | message = """From: From Person 17 | To: To Person 18 | Subject: SMTP e-mail test 19 | 20 | This is a test e-mail message. 21 | """ 22 | 23 | try: 24 | smtpObj.sendmail(sender, receivers, message) 25 | print "Successfully sent email" 26 | except SMTPException: 27 | print "Error: unable to send email" -------------------------------------------------------------------------------- /python/sys.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | ''' 3 | 4 | @author homeway 5 | @version 15.04.29 6 | @link http://homeway.me 7 | @function python get OpenWRT system info 8 | 9 | ''' 10 | 11 | 12 | import os 13 | # Return CPU temperature as a character string 14 | def getCPUtemperature(): 15 | res = os.popen('vcgencmd measure_temp').readline() 16 | return(res.replace("temp=","").replace("'C\n","")) 17 | 18 | # Return RAM information (unit=kb) in a list 19 | # Index 0: total RAM 20 | # Index 1: used RAM 21 | # Index 2: free RAM 22 | def getRAMinfo(): 23 | p = os.popen('free') 24 | i = 0 25 | while 1: 26 | i = i + 1 27 | line = p.readline() 28 | if i==2: 29 | return(line.split()[1:4]) 30 | 31 | # Return % of CPU used by user as a character string 32 | def getCPUuse(): 33 | return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip())) 34 | 35 | # Return information about disk space as a list (unit included) 36 | # Index 0: total disk space 37 | # Index 1: used disk space 38 | # Index 2: remaining disk space 39 | # Index 3: percentage of disk used 40 | def getDiskSpace(): 41 | p = os.popen("df -h /") 42 | i = 0 43 | while 1: 44 | i = i +1 45 | line = p.readline() 46 | if i==2: 47 | return(line.split()[1:5]) 48 | 49 | def getSystem(): 50 | p = os.popen("uname -amnrspv") 51 | while 1: 52 | line = p.readline() 53 | return(line) 54 | 55 | def getExtranetIp(): 56 | p = os.popen('wget "http://www.ip138.com/ips1388.asp" -q -O - | sed -nr \'s/.*\[(([0-9]+\.){3}[0-9]+)\].*/\1/p\'') 57 | while 1: 58 | line = p.readline() 59 | print line 60 | return(line) 61 | 62 | def getIntranetIp(): 63 | p = os.popen('ifconfig apcli0 | grep inet\ addr') 64 | while 1: 65 | line = p.readline() 66 | return(line) 67 | 68 | def getSsid(): 69 | p = os.popen('uci get wireless.@wifi-iface[0].ApCliSsid') 70 | while 1: 71 | line = p.readline() 72 | return(line) 73 | 74 | # CPU informatiom 75 | CPU_temp = getCPUtemperature() 76 | CPU_usage = getCPUuse() 77 | 78 | # RAM information 79 | # Output is in kb, here I convert it in Mb for readability 80 | RAM_stats = getRAMinfo() 81 | RAM_total = round(int(RAM_stats[0]) / 1000,1) 82 | RAM_used = round(int(RAM_stats[1]) / 1000,1) 83 | RAM_free = round(int(RAM_stats[2]) / 1000,1) 84 | 85 | # Disk information 86 | DISK_stats = getDiskSpace() 87 | DISK_total = DISK_stats[0] 88 | DISK_used = DISK_stats[1] 89 | DISK_perc = DISK_stats[3] 90 | 91 | # system info 92 | SYSTEM_info = getSystem() 93 | 94 | # NET infomation 95 | NET_extranet_ip = getExtranetIp() 96 | NET_internet_ip = getIntranetIp().lstrip('') 97 | NET_connect_ssid = getSsid() 98 | 99 | if __name__ == '__main__': 100 | print('-------------------------------------------') 101 | print("System info ="+str(SYSTEM_info)) 102 | print('-------------------------------------------') 103 | print('RAM Total = '+str(RAM_total)+' MB') 104 | print('RAM Used = '+str(RAM_used)+' MB') 105 | print('RAM Free = '+str(RAM_free)+' MB') 106 | print('-------------------------------------------') 107 | print('DISK Total Space = '+str(DISK_total)+'B') 108 | print('DISK Used Space = '+str(DISK_used)+'B') 109 | print('DISK Used Percentage = '+str(DISK_perc)) 110 | print('-------------------------------------------') 111 | print('NET Extranet Ip ='+str(NET_extranet_ip)) 112 | print('NET Connect Ssid ='+str(NET_connect_ssid)) 113 | print('NET Internet Wan Ip ='+str(NET_internet_ip)) 114 | 115 | -------------------------------------------------------------------------------- /shell/README.md: -------------------------------------------------------------------------------- 1 | # About 2 | 3 | WRTnode开发中几个脚本包,目前主要包含扫描wifi AP、获取外网ip等。 4 | 5 | # How To Use 6 | 7 | See test file 8 | -------------------------------------------------------------------------------- /shell/aps.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # @function: scan wifi and information 4 | 5 | i=0 6 | #echo "HTTP/1.1 200 OK" 7 | echo "Content-Type: application/json" 8 | echo "" 9 | echo "{" 10 | iwpriv ra0 set SiteSurvey=0 11 | iwpriv ra0 get_site_survey | grep '^[0-9]' | while read line 12 | do 13 | chanel=`echo $line | awk '{print $1}'` 14 | #if ${line:5:1} == ' ' 15 | if [ "${line:5:1}" = " " ]; then 16 | ssid="[unknown]" 17 | bssid=`echo $line | awk '{print $2}'` 18 | security=`echo $line | awk '{print $3}'` 19 | signal=`echo $line | awk '{print $4}'` 20 | wmode=`echo $line | awk '{print $5}'` 21 | extch=`echo $line | awk '{print $6}'` 22 | nt=`echo $line | awk '{print $7}'` 23 | wps=`echo $line | awk '{print $8}'` 24 | else 25 | ssid=`echo $line | awk '{print $2}'` 26 | bssid=`echo $line | awk '{print $3}'` 27 | security=`echo $line | awk '{print $4}'` 28 | signal=`echo $line | awk '{print $5}'` 29 | wmode=`echo $line | awk '{print $6}'` 30 | extch=`echo $line | awk '{print $7}'` 31 | nt=`echo $line | awk '{print $8}'` 32 | wps=`echo $line | awk '{print $9}'` 33 | fi 34 | 35 | if [ $i -ne 0 ]; then 36 | echo "," 37 | fi 38 | 39 | let i+=1 40 | echo "\"$ssid\":{" 41 | echo "\"chanel\":"$chanel"," 42 | #echo "\"ssid\":\""$ssid"\"," 43 | echo "\"bssid\":\""$bssid"\"," 44 | echo "\"security\":\""$security"\"," 45 | echo "\"signal\":\""$signal"\"," 46 | echo "\"wmode\":\""$wmode"\"," 47 | echo "\"extch\":\""$extch"\"," 48 | echo "\"nt\":\""$nt"\"," 49 | echo "\"wps\":\""$wps"\"" 50 | echo -n "}" 51 | done 52 | echo "}" 53 | -------------------------------------------------------------------------------- /shell/getip.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | #@function: get ip 4 | 5 | wget "http://www.ip138.com/ips1388.asp" -q -O - | sed -nr 's/.*\[(([0-9]+\.){3}[0-9]+)\].*/\1/p' --------------------------------------------------------------------------------