Configuration Training ~ Lua script ~

Yamaha Router
Configuration Training
~ Lua script ~
Lua script
Simple and powerful scripting language.
- http://www.lua.org
Lua script interpreter is embedded in Yamaha router.
With Yamaha router …
- Monitor the system log and change the configuration.
- Send e-mail to report the network status .
- Change the routing policy dynamically.
- Shutdown the switch’s port that connect to malicious computer.
- Write a simple network server/client with LuaSocket library.
2
© Yamaha Corporation
Topology
RTX810
Console cable
(Tera Term)
3
LAN cable
© Yamaha Corporation
Hello, world!
Challenge
Make a script that print out the string “Hello, world!”.
1.
2.
3.
4.
4
Start the memo.
Write “ print (“Hello, world!”) “.
Create a new line at the bottom of file.
Save the memo as “helloworld.lua” with ascii code.
© Yamaha Corporation
USB Memory settings
1. Save helloworld.lua to USB memory.
In this case, save to root directory.
2. Connect the USB Memory to RTX810’s USB port.
5
© Yamaha Corporation
Execute Lua script
Command
Lua <LUA_SCRIPT>
Sample:
・helloworld.lua is automatically
compiled and execute.
・script output is displayed
on console.
6
© Yamaha Corporation
Other method of executing Lua
1. Transfer the Lua script file to router by using TFTP.
1-1. Set “tftp host any” on router console.
1-2. Transfer the Lua script to router.
2. Execute “ Lua <LUA_SCRIPT>” on router console.
7
© Yamaha Corporation
Other method of executing Lua
Command
Lua –e ‘<LUA_SCRIPT>’
Sample:
・calculate the number.
・calculate negative number.
・decimal number can not be calculate.
8
© Yamaha Corporation
Test case (variable)
Number
9
String
a = 12
b=2
a=a*b
str1 = “I like “
str2 = “music.”
str1 = str1 .. str2
print(a)
print(str1)
Addition of strings
© Yamaha Corporation
Test case (for loop)
Check the result of following script.
for i=1, 10 do
print (i, “Hello, world!”)
end
10
© Yamaha Corporation
Lua status check
In case of Lua script was not executed correctly …
・Use “show status lua”.
・Check Compile error.
11
© Yamaha Corporation
Training1
Challenge
Make a Lua script that calculate n2 (n = 1~10)
Result
# lua /double.lua
1
2
3
4
1
4
9
16
Example
Example
for i = 0, 10 do
print (i, i * i)
end
i=0
while i < 10 do
print (i, i * i)
i=i+1
end
・・・
10
12
100
© Yamaha Corporation
Training2
Challenge
Make a Lua script that calculate n! (n = 1~10)
Result
# lua /factorial.lua
tostring () … connect the string and number.
1! = 1
2! = 2
3! = 6
4! = 24
Example (I am 30 years old.)
-----------------------------------------------------------age = 30
print (“I am ” .. tostring (age) .. “ years old”)
------------------------------------------------------------
・・・
10! = 3628800
13
Hint
Example
factorial = 1
for i = 1, 10 do
factorial = factorial * i
print (tostring(i) .. “! = “ .. tostring(factorial))
end
© Yamaha Corporation
Execute router command
Lua script library
ARG1, ARG2 = rt.command (<COMMAND>)
14
ARG1 … true or false
ARG2 … output of COMMAND
© Yamaha Corporation
Execute router command
Example
In case of ping is available,
rtn is true.
In case of ping is
not available,
rtn is false.
15
© Yamaha Corporation
Training 3
Challenge
Make a Lua script that display the status of all lan interface.
Hint: use for loop and “show status lanN” command.
16
© Yamaha Corporation
Training 3
Example code
cmd = “show status lan”
for i = 1, 2 do
cmd_n = cmd .. tostring (i)
rtn, str = rt.command(cmd_n)
if rtn then
print (cmd_n .. “ output:¥r¥n” .. str)
else
print (“Command error:” .. cmd_n)
end
end
17
・ ¥r¥n means line feed.
© Yamaha Corporation
Terminate Lua
In case of Lua script become an infinite loop process …
Command
terminate lua <LUA_TASK_ID>
terminate lua file ‘<LUA_SCRIPT>’
Example
while true do
i=1
end
18
Infinite loop
© Yamaha Corporation
Send E-mail
Lua script library
rt.mail (<TABLE>)
Sample:
・Make table for mail.
※ If necessary, set ID & password.
smtp_auth_name = ID
smtp_auth_password = password
・Check the result.
19
© Yamaha Corporation
Training 4
Challenge
Make a Lua script that send e-mail to you.
E-mail title is command.
E-mail contents is the output of “show environment”.
Hint: Set the string to table factor.
mail_tbl.subject = NAME
mail_tbl.text = STRING
20
© Yamaha Corporation
Training 4
Example
cmd = “show environment”
mail_tbl = {
smtp_address = “mail.test.test”,
from = [email protected],
to = [email protected]
}
21
rtn, str = rt.command(cmd)
if rtn then
mail_tbl.text = str
mail_tbl.subject = cmd
if not rt.mail(mail_tbl) then
print(“Send mail failure”)
end
end
・Set the output of command.
・Set the command to subject.
・In case of fail to send email,
error output print to console.
© Yamaha Corporation
Execute command regularly
Lua script library
rt.sleep (<SLEEP_TIME>)
Sample:
・Make a Lua script that show the
configuration at 30 second intervals.
・Delay the process of Lua script
until after the specified time.
22
© Yamaha Corporation
Function definition
Challenge
Make a Lua script that execute ping to www.example.com
at 30 second intervals.
Sample:
・Make a function that execute ping.
・Delay the process of Lua script
until after the specified time.
23
© Yamaha Corporation
Take out the string
Lua script library
string.match(<STRING>, <PATTERN>)
Search the <PATTERN> from <STRING>
Challenge
Make a Lua script that print the number of dhcp leased addresses.
Sample:
・Search “Leased: “ and
print the number that following
the string “Leased: “.
24
© Yamaha Corporation
Training 5
Challenge
Make a Lua script that send e-mail to you at 30 second intervals.
E-mail title is “CPU utilization”.
E-mail contents is “Current CPU utilization is ○○%.”
Hint: For Getting the number of CPU utilization,
string.match (str, “CPU:%s+(%d+)%%”)
25
© Yamaha Corporation
Training 5
Example
-- execute command and return result
function exec_cmd(cmd)
rtn, str = rt.command (cmd)
if rtn and str then
return true, str
else
return false
end
end
mail_tbl = {
subject = "CPU utilization"
・・・ (omission)
}
cmd = "show environment"
26
Continuation of Example
interval = 30
output = "Current CPU utilization is "
while true do
rtn, str = exec_cmd (cmd)
if rtn then
mail_tbl.text = output
.. string.match(str, "CPU:%s+(%d+)%%") .. "%"
if not rt.mail(mail_tbl) then
print ("Fail")
end
end
rt.sleep (interval)
end
© Yamaha Corporation
Get number from system log
Lua script library
rt.syslogwatch (<PATTERN>)
Search the <PATTERN> from router’s system log.
Challenge
Make a Lua script that print the information of LAN linkup.
Sample:
・Monitor the system log
and detect the specified log,
move to next process.
・Get the port number from
detected system log.
27
© Yamaha Corporation
Training 6
Challenge
Make a Lua script that output the information of
LAN2 linkup and linkdown to system log.
format : “LAN2 interface linked up/down”
In default, LAN2 linked up.
Hint: The system log that you should detect.
- linkup … LAN2: link up
- linkdown … LAN2: link down
Use rt.syslog(<TYPE>, <TEXT>)
to output the string to system log.
28
© Yamaha Corporation
Training 6
Example
Pattern = “LAN2: link”
While true do
rtn = rt.syslogwatch (pattern .. “down”)
if rtn then
rt.syslog (“info”, “LAN2 interface linked down”)
end
rtn = rt.syslogwatch (pattern .. “up”)
if rtn then
rt.syslog (“info”, “LAN2 interface linked up”)
end
end
29
© Yamaha Corporation
Training 7
Make a Lua script that …
・Send E-mail to you at 60 second intervals.
・E-mail title is router’s current time, format is YEAR/MONTH/DATE XX:XX:XX.
・E-mail contents are following
----------------------------CPU: XX% (5sec)
CPU: XX% (1min)
CPU: XX% (5min)
Memory: XX% used
----------------------------Procedure
1. Get the information about current time and CPU from “show environment”.
2. Make a content from the information.
Hint : Use string.find() and string.sub. ※ But you may use other functions.
3. Send E-mail to yourself.
30
© Yamaha Corporation
Training 8
Make a Lua script that …
・If the PPPoE is disconnected, use the route through USB 3G to access internet
and output the log “Change the route to mobile”.
・If the PPPoE is connected, use the route through PPPoE to access internet
and output the log “Change the route to PPPoE”.
Procedure
USB 3G
Internet
PPPoE (LAN2)
LAN1
192.168.100.0/24
1. Make a environment to access internet
with PPPoE and USB 3G.
2. Monitor the following log
- “PPPOE[01] PPPoE Connect”
- “PPPOE[01] Disconnected”
3. Change the route “ip route default gateway **”.
4. Check the connectivity by using ping from PC.
31
© Yamaha Corporation