info_i_25x25.png See important information about Ubiquiti Devices and KRACK Vulnerability in this article. We will update this document as more information becomes available.

EdgeRouter - How to run operational mode command from scripts?

Overview


Readers will learn how to run operational mode commands from scripts. 

Example


Example script:

ubnt@ubnt:~$ cat myscript 
#!/bin/bash 
show version 
show interfaces

I made sure the script was executable with:

ubnt@ubnt:~$ chmod +x myscript

But when I run it, I get the following errors:

ubnt@ubnt:~$ ./myscript
./myscript: line 2: show: command not found
./myscript: line 3: show: command not found

Why isn't it working? 

Answer: The script is missing the environment variables it needs.  To fix it you can run the command with command wrapper that has the environment variables it needs.  For example:

ubnt@ubnt:~$ cat myscript
#!/bin/bash
run=/opt/vyatta/bin/vyatta-op-cmd-wrapper
$run show version
$run show interfaces

Now when the script is run we get:

ubnt@ubnt:~$ ./myscript
Version:      v1.2.0alpha2
Build ID:     4558456
Build on:     05/02/13 16:24
Copyright:    2012-2013 Ubiquiti Networks, Inc.
HW model:     EdgeRouter Lite 3-Port
HW S/N:       DC9FDB29DBEE
Uptime:       22:09:25 up 11 days,  1:25,  1 user,  load average: 0.08, 0.02, 0.01

Codes: S - State, L - Link, u - Up, D - Down, A - Admin Down
Interface    IP Address                        S/L  Description                 
---------    ----------                        ---  -----------                 
eth0         192.168.1.1/24                    u/D  secure LAN                  
eth1         192.168.2.1/24                    u/D  public LAN                  
eth1.100     2.2.2.3/24                        u/D                              
eth2         1.1.1.2/24                        u/u  WAN                         
lo           127.0.0.1/8                       u/u           

In order for the script to survive an upgrade you'll want to move it into the /config directory since that whole directory gets copied in the upgrade process.

mv myscript /config/scripts

Ubiquiti Employee & Ubiquiti Employee