Run vyos commands in post config bootup script

Hello team,

Dumb question, how do I run vyos cli command e.g. renew in /config/scripts/vyos-postconfig-bootup.script?

I understand the concept of a pre/post boot up script, but running vyos specific commands don’t seem available there, only debian native commands.

Hopefully this page will give you all the help you need!

In short you need to include the script-template to get the vbash which is the op-mode (the CLI you get when logged in through console or SSH).

From here you can type the same commands as you normally would in CLI either if its op-mode (like a show command or so) or conf-mode (enter with configure and then issue the commands followed with a commit and save if wanted).

Example:

#!/bin/vbash

# Include VyOS functions
source /opt/vyatta/etc/functions/script-template

# Script debugging
#set -x

# Set variables
HOSTNAME=example

# Perform stuff
show version
configure
set system hostname ${HOSTNAME}
commit
save
exit

The good thing is that all regular bash stuff is supported in case you want to do a FOR-loop or whatever.

Here is one example from my tests of injecting x number of static routes to find out how VyOS behaves (turned out to be not good since commit and boot times will increase dramatically after a few hundred routes - in this case I also dont do any save so I can just reboot the box and have it reset after tests or perform a manual save in the cli if I want to keep the changes for a reboot):

#!/bin/vbash

# Include VyOS functions
source /opt/vyatta/etc/functions/script-template

# Script debugging
#set -x

# Set variables
GATEWAY=192.168.1.254
OCTET_A_START=0
OCTET_A_END=0
OCTET_B_START=0
OCTET_B_END=0
OCTET_C_START=0
OCTET_C_END=0
OCTET_D_START=0
OCTET_D_END=199

# Perform stuff
configure
for OCTET_A in `seq ${OCTET_A_START} ${OCTET_A_END}`
do
        for OCTET_B in `seq ${OCTET_B_START} ${OCTET_B_END}`
        do
                for OCTET_C in `seq ${OCTET_C_START} ${OCTET_C_END}`
                do
                        for OCTET_D in `seq ${OCTET_D_START} ${OCTET_D_END}`
                        do
                                echo "set vrf name INTERNET protocols static route ${OCTET_A}.${OCTET_B}.${OCTET_C}.${OCTET_D}/32 next-hop ${GATEWAY} distance 1"
                                set vrf name INTERNET protocols static route ${OCTET_A}.${OCTET_B}.${OCTET_C}.${OCTET_D}/32 next-hop ${GATEWAY} distance 1
                        done
                done
        done
done
commit
exit

Edit:

I would put my script in its own file like in /config/custom/filename.sh and then call it from the /config/scripts/vyos-postconfig-bootup.script.

That is the content of /config/scripts/vyos-postconfig-bootup.script would be:

#!/bin/sh
# This script is executed at boot time after VyOS configuration is fully applied.
# Any modifications required to work around unfixed bugs
# or use services not available through the VyOS CLI system can be placed here.

/config/custom/filename.sh
2 Likes

Thank you so much, makes perfectly. My original confusion was I wasn’t able to run vbash commands in /config/scripts/vyos-postconfig-bootup.script when every other debian commands were fine.

Now I understand the logic perfectly. Thank you so much and keep up the great work!

As you don’t use it from vyattacfg group I think you’ll get router that you can’t configure anymore. It depends.