EdgeRouter - How to run operational mode command from scripts?


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


Example script:

ubnt@ubnt:~$ cat myscript 
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
$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                    u/D  secure LAN                  
eth1                    u/D  public LAN                  
eth1.100                        u/D                              
eth2                        u/u  WAN                         
lo                        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
Powered by Zendesk