Exec configure command in my C program?

Beside using http api, how could a C program running in vyos exec config command, such as:

set interfaces ethernet eth0 address dhcp

get interfaces ethernet eth0 address

Thanks.

For op-mode you can use op mode wrapper “/opt/vyatta/bin/vyatta-op-cmd-wrapper show version”

Thanks!

/opt/vyatta/bin/vyatta-op-cmd-wrapper show interfaces
Codes: S - State, L - Link, u - Up, D - Down, A - Admin Down
Interface        IP Address                        S/L  Description
---------        ----------                        ---  -----------
eth0             192.168.21.171/24                 u/u  
eth1             192.168.48.31/24                  u/u  
lo               127.0.0.1/8                       u/u  
                 ::1/128

Do we have something like /opt/vyatta/bin/vyatta-config-cmd-wrapper ?
I want to set/get configure in my C program.

Not sure if it will work in C
But you should use “source”

source /opt/vyatta/etc/functions/script-template

configure
set protocols bgp system-as 65536
set protocols bgp neighbor 192.168.122.1 remote-as 65002
commit
exit

I’m not C guy
but something like this

#include <stdlib.h>

int main() {
    system("source /opt/vyatta/etc/functions/script-template");
    system("configure");
    system("set protocols bgp system-as 65536");
    system("set protocols bgp neighbor 192.168.122.1 remote-as 65002");
    system("commit");
    system("exit");

    return 0;
}

Another example:

cfg="/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper"

$cfg begin
$cfg set interfaces ethernet eth1 description TEST
$cfg commit
$cfg save
$cfg end

I.e. C

#include <stdlib.h>

int main() {
    system("/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper begin");
    system("/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper set interfaces ethernet eth1 description TEST");
    system("/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper commit");
    system("/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper end");

    return 0;
}

That’s not going to work. Each time you call system() it forks a child process that executes a POSIX shell (/bin/sh) with the -c string parameter that you specify. This means that every such call starts a new shell process. Your call to source, for example, is useless as once that system() call is returning, all your environment set by the template is gone.

Thanks, guys, and it works:

#include <stdlib.h>

int main() {
    system("/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper begin;/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper set interfaces ethernet eth1 description TEST;/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper commit;/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper end;");
    return 0;
}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.