Top Banner
Yamaha Router Configuration Training ~ Lua script ~
31

Configuration Training ~ Lua script ~

Jan 03, 2017

Download

Documents

ngotruc
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Configuration Training ~ Lua script ~

Yamaha Router Configuration Training

~ Lua script ~

Page 2: Configuration Training ~ Lua script ~

© Yamaha Corporation

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

Page 3: Configuration Training ~ Lua script ~

© Yamaha Corporation

Topology

RTX810

Console cable (Tera Term)

LAN cable

3

Page 4: Configuration Training ~ Lua script ~

© Yamaha Corporation 4

Hello, world!

Make a script that print out the string “Hello, world!”.

1. Start the memo. 2. Write “ print (“Hello, world!”) “. 3. Create a new line at the bottom of file. 4. Save the memo as “helloworld.lua” with ascii code.

Challenge

Page 5: Configuration Training ~ Lua script ~

© Yamaha Corporation 5 5

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.

Page 6: Configuration Training ~ Lua script ~

© Yamaha Corporation

Execute Lua script

6

Sample:

・helloworld.lua is automatically compiled and execute. ・script output is displayed on console.

Lua <LUA_SCRIPT>

Command

Page 7: Configuration Training ~ Lua script ~

© 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

Page 8: Configuration Training ~ Lua script ~

© Yamaha Corporation

Other method of executing Lua

・calculate the number. ・calculate negative number. ・decimal number can not be calculate.

Lua –e ‘<LUA_SCRIPT>’

Command

Sample:

8

Page 9: Configuration Training ~ Lua script ~

© Yamaha Corporation

Test case (variable)

Number

a = 12 b = 2 a = a * b print(a)

String

str1 = “I like “ str2 = “music.” str1 = str1 .. str2 print(str1) Addition of strings

9

Page 10: Configuration Training ~ Lua script ~

© Yamaha Corporation

Test case (for loop)

for i=1, 10 do print (i, “Hello, world!”) end

Check the result of following script.

10

Page 11: Configuration Training ~ Lua script ~

© Yamaha Corporation

Lua status check

・Use “show status lua”. ・Check Compile error.

In case of Lua script was not executed correctly …

11

Page 12: Configuration Training ~ Lua script ~

© Yamaha Corporation

Training1

# lua /double.lua 1 1 2 4 3 9 4 16 ・・・ 10 100

Result for i = 0, 10 do print (i, i * i) end

Example i = 0 while i < 10 do print (i, i * i) i = i + 1 end

Example

Make a Lua script that calculate n2 (n = 1~10) Challenge

12

Page 13: Configuration Training ~ Lua script ~

© Yamaha Corporation

Training2

# lua /factorial.lua 1! = 1 2! = 2 3! = 6 4! = 24 ・・・ 10! = 3628800

Result tostring () … connect the string and number. Example (I am 30 years old.) ------------------------------------------------------------ age = 30 print (“I am ” .. tostring (age) .. “ years old”) ------------------------------------------------------------

Hint

factorial = 1 for i = 1, 10 do factorial = factorial * i print (tostring(i) .. “! = “ .. tostring(factorial)) end

Example

Make a Lua script that calculate n! (n = 1~10) Challenge

13

Page 14: Configuration Training ~ Lua script ~

© Yamaha Corporation

Execute router command

ARG1 … true or false ARG2 … output of COMMAND ARG1, ARG2 = rt.command (<COMMAND>)

Lua script library

14

Page 15: Configuration Training ~ Lua script ~

© 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

Page 16: Configuration Training ~ Lua script ~

© Yamaha Corporation

Training 3

Make a Lua script that display the status of all lan interface. Hint: use for loop and “show status lanN” command.

Challenge

16

Page 17: Configuration Training ~ Lua script ~

© Yamaha Corporation

Training 3

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

Example code

・ ¥r¥n means line feed.

17

Page 18: Configuration Training ~ Lua script ~

© Yamaha Corporation

Terminate Lua In case of Lua script become an infinite loop process …

while true do i = 1 end

Example Infinite loop

terminate lua <LUA_TASK_ID> terminate lua file ‘<LUA_SCRIPT>’

Command

18

Page 19: Configuration Training ~ Lua script ~

© Yamaha Corporation

Send E-mail

・Make table for mail. ※ If necessary, set ID & password. smtp_auth_name = ID smtp_auth_password = password ・Check the result.

rt.mail (<TABLE>)

Lua script library

Sample:

19

Page 20: Configuration Training ~ Lua script ~

© Yamaha Corporation

Training 4

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

Challenge

20

Page 21: Configuration Training ~ Lua script ~

© Yamaha Corporation

Training 4

・Set the output of command. ・Set the command to subject. ・In case of fail to send email, error output print to console.

cmd = “show environment” mail_tbl = { smtp_address = “mail.test.test”, from = [email protected], to = [email protected] } 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

Example

21

Page 22: Configuration Training ~ Lua script ~

© Yamaha Corporation

Execute command regularly

・Make a Lua script that show the configuration at 30 second intervals. ・Delay the process of Lua script until after the specified time.

rt.sleep (<SLEEP_TIME>)

Lua script library

Sample:

22

Page 23: Configuration Training ~ Lua script ~

© Yamaha Corporation

Function definition

・Make a function that execute ping. ・Delay the process of Lua script until after the specified time.

Sample:

Make a Lua script that execute ping to www.example.com at 30 second intervals.

Challenge

23

Page 24: Configuration Training ~ Lua script ~

© Yamaha Corporation

Take out the string

string.match(<STRING>, <PATTERN>) Search the <PATTERN> from <STRING>

Lua script library

Sample:

Make a Lua script that print the number of dhcp leased addresses. Challenge

・Search “Leased: “ and print the number that following the string “Leased: “.

24

Page 25: Configuration Training ~ Lua script ~

© Yamaha Corporation

Training 5

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+)%%”)

Challenge

25

Page 26: Configuration Training ~ Lua script ~

© Yamaha Corporation

Training 5

-- 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"

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

Continuation of Example

26

Page 27: Configuration Training ~ Lua script ~

© Yamaha Corporation

Get number from system log

rt.syslogwatch (<PATTERN>) Search the <PATTERN> from router’s system log.

Lua script library

Sample:

Make a Lua script that print the information of LAN linkup. Challenge

・Monitor the system log and detect the specified log, move to next process. ・Get the port number from detected system log.

27

Page 28: Configuration Training ~ Lua script ~

© Yamaha Corporation

Training 6

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.

Challenge

28

Page 29: Configuration Training ~ Lua script ~

© 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

Page 30: Configuration Training ~ Lua script ~

© Yamaha Corporation

Training 7

・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 -----------------------------

Make a Lua script that …

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

Page 31: Configuration Training ~ Lua script ~

© Yamaha Corporation

Training 8

Internet

・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”.

Make a Lua script that …

PPPoE (LAN2)

USB

3G

LAN1

192.168.100.0/24

Procedure

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