http://vyos.net/wiki/Command_scripting on helium beta

I am trying to run a script as root (from cron, for example in /etc/cron.daily) using command scripting, on 1.1.0-beta1

#!/bin/vbash
source /opt/vyatta/etc/functions/script-template
run show ip route
if [ condition ]; then
    configure
    set ... some routing
    commit
    save
    exit
    echo done with commit/save
fi

So if the condition is false, we have

source /opt/vyatta/etc/functions/script-template
run show ip route

Every time that runs we get a new unionfs-fuse process, and new directories in /opt/vyatta/config/tmp. Eventually the system runs out of memory.

When the condition is met, we try to modify the configuration, but get:

configure: command not found
commit: command not found
You must provide a file name to save the config to

I’m not sure this script work for long time (It is mean that I’m not sure system run out of memory.)

Have you ever tried Shell-API?
http://vyos.net/wiki/Shell_API

I can set host-name from cron script.

#!/bin/bash

SHELL_API=/opt/vyatta/sbin/my_cli_shell_api

D=$( date +"%Y%m%d%H%M" )

wrapper=/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper
SET=/opt/vyatta/sbin/my_set
COMMIT=/opt/vyatta/sbin/my_commit

$wrapper show ip route

session_env=$($SHELL_API getSessionEnv $PPID)
eval $session_env
$SHELL_API setupSession

$SET system host-name $D
$COMMIT
* * * * * root /home/vyos/cron_test >> /tmp/log 2>&1 


Hiroyuki Sato.

Hi Carl,

I have a very similar requirement as yours. To make it work there are 2 specific things I had to do.

  1. The shell-api script must be run as user vyos.
  2. Use a script with the correct environment.

I have a script run by cron which does stuff, when it needs to it calls the shell-api script to add/remove routes from my routing table…

The Shell API code I wrote is here: (Mostly copied from the VyOS Wiki. Also, I know 80% of those vars are not needed, I include them for completeness)

#!/bin/bash

if [ -z $4 ]; then
    $0 $@ RUN 2>&1 >> /data/dynamic_route.log
    exit 0
fi

if [ $UID -ne 1000 ]; then
    echo "Must run $0 as user vyos!" 1>&2
    exit 1
fi

export vyatta_datadir=/opt/vyatta/share
export vyatta_op_templates=/opt/vyatta/share/vyatta-op/templates
export vyatta_sysconfdir=/opt/vyatta/etc
export vyatta_sharedstatedir=/opt/vyatta/com
export vyatta_sbindir=/opt/vyatta/sbin
export vyatta_cfg_templates=/opt/vyatta/share/vyatta-cfg/templates
export vyatta_bindir=/opt/vyatta/bin
export vyatta_libdir=/opt/vyatta/lib
export vyatta_libexecdir=/opt/vyatta/libexec
export vyatta_prefix=/opt/vyatta
export vyatta_datarootdir=/opt/vyatta/share
export vyatta_configdir=/opt/vyatta/config
export vyatta_localedir=/opt/vyatta/share/locale

export SHELL_API=/bin/cli-shell-api


# Obtain session environment
session_env=$($SHELL_API getSessionEnv $PPID)

# Evaluate environment string
eval $session_env

# Setup the session
cli-shell-api setupSession

cli-shell-api inSession
if [ $? -ne 0 ]; then
   echo "Something went wrong!"
   exit 1
fi

export SET=${vyatta_sbindir}/my_set
export DELETE=${vyatta_sbindir}/my_delete
export COPY=${vyatta_sbindir}/my_copy
export MOVE=${vyatta_sbindir}/my_move
export RENAME=${vyatta_sbindir}/my_rename
export ACTIVATE=${vyatta_sbindir}/my_activate
export DEACTIVATE=${vyatta_sbindir}/my_activate
export COMMENT=${vyatta_sbindir}/my_comment
export COMMIT=${vyatta_sbindir}/my_commit
export DISCARD=${vyatta_sbindir}/my_discard
export SAVE=${vyatta_sbindir}/vyatta-save-config.pl

#
#
#echo "User: $USER"
#echo "Args: $@"
#echo "Env: `env`"
#
#

if [ $1 == "add" ]; then
    echo "`date`: CLI-SHELL-API: Adding route..."
    $SET protocols static route $2 next-hop $3
elif [ $1 == "del" ]; then
    echo "`date`: CLI-SHELL-API: Removing route..."
    $DELETE protocols static route $2
else
    echo "Unknown action: $1" 1>&2
    exit 1
fi

$COMMIT

Hope that helps!

No. I find that to be less readable than

cmd="configure
set ...
save
exit
exit
"

echo "$cmd" | ssh -t -t vyos@localhost