Forum Discussion

mjensen's avatar
mjensen
Icon for Employee rankEmployee
3 months ago

Lua Script from the Restorepoint Automation PowerHour

This is an annotated version of the Lua device control script that was shown in last week's PowerHour about Restorepoint automation.

The objective was to connect to a Cisco IOS device, identify any GigabitEthernet interfaces whose current MTU was >1500, and then configure those interfaces to use an MTU of 1500.  We can do this by running the "show interfaces" command and processing the output. Here is the script I described during the PowerHour:

timeout (10)

-- Run the "show interfaces" command, collect the output, and store that output
-- as a list of text lines.
sendget("show interfaces","#")
output = before()
lines=splitlines(output)

-- Start looping through the output lines.
for i,line in pairs(lines) do

-- Look for lines like "InterfaceName is up|down, line protocol is up|down".
   if line:match(".* is [a-z]+, line protocol is [a-z]+") then

-- When we find one, save the interface name as intf.
      intf=line:match("^(.*) is [a-z]+, line protocol is [a-z]+")
      mtu=nil
   end
-- If we find a line with MTU data, convert the MTU to a numeric value and save it.
   if line:match("MTU") then
      mtu = tonumber(line:match("MTU ([0-9]+) bytes"))
-- Check if the interface meets our criteria of being a GigabitEthernet interface
-- with a current MTU above 1500.
      if intf:match("GigabitEthernet") and mtu > 1500 then

-- If it is, execute the Cisco IOS commands to change the configuration. We use a local
-- variable 'in_config_mode' to avoid going in/out of config mode multiple times.
         if not in_config_mode then    
            sendget("conf terminal","#")
            in_config_mode = true
         end
         sendget("int "..intf,"#")
         sendget("mtu 1500","#")
         sendget("exit","#")
         print("Changed MTU on "..intf.." from "..mtu.." to 1500 bytes")
      end
   end
end

-- Finally, exit from config mode if needed.
if in_config_mode then
   sendget("exit","#")
end
No RepliesBe the first to reply