Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Quagga: Dev

executing vtysh with execl()

 

 

Quagga dev RSS feed   Index | Next | Previous | View Threaded


a.m.huq at aalto

Apr 26, 2011, 6:55 AM

Post #1 of 2 (623 views)
Permalink
executing vtysh with execl()

Hi,
I have managed to execute vtysh as an external process using popen().

i.e. the following code gives me:
FILE* read_fp;
char buffer[BUFSIZ +1];
int num_of_char_read;

memset(buffer,'\0',sizeof(buffer));
read_fp = popen("vtysh -c \"show ip bgp\"","r");

if(read_fp != NULL)
{
num_of_char_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);

while(num_of_char_read > 0)
{
buffer[num_of_char_read - 1] = '\0';
printf("reading: \n %s \n",buffer);
num_of_char_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
}

pclose(read_fp);
}

the following output:

root [at] ahuq-kitche:/home/ahuq/MappingServer# ./zebra_connect
reading:
BGP table version is 0, local router ID is 10.144.18.117
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
r RIB-failure, S Stale, R Removed
Origin codes: i - IGP, e - EGP, ? - incomplete

Network Next Hop Metric LocPrf Weight Path
*> 192.168.57.0 10.144.18.162 0 70 0 100 i

Total number of prefixes 1

------------- this here is my expected output.-----------------------------------

But when I try to do it with execl(), i.e.

#define SHELL "/usr/bin/vtysh"
int main()
{

int pid;
pid = fork();

if(pid == 0)
{
execl(SHELL,"-c","\" show ip bgp\"",NULL);
printf("\n in child");
}
else
{
printf("\n parent");
}
return 0;
}
I get the following output:
root [at] ahuq-kitche:/home/ahuq/MappingServer# ./zebra_connect

parentroot [at] ahuq-kitche:/home/ahuq/MappingServer#
Hello, this is Quagga (version 0.99.17).
Copyright 1996-2005 Kunihiro Ishiguro, et al.

ahuq-kitchen#

--------------- That is, the option "-c" and the command "show ip bgp" isn't getting passed to vtysh. Why is happening? How can I correct it? My goal is to read certain info from vtysh and write certain info to vtysh.

Bye.


barryf at google

Apr 26, 2011, 7:26 AM

Post #2 of 2 (596 views)
Permalink
Re: executing vtysh with execl() [In reply to]

Try:

execl(SHELL, SHELL, "-c","show ip bgp",NULL);

From execl() man page regarding the arg list:

"The first argument, by convention, should point to the filename
associated with the file being executed."

Also the escaped quotes on the command string are not needed when
using execl() and they cause "Unknown command" error.

Regards,
Barry

On Tue, Apr 26, 2011 at 6:55 AM, Huq A. 1 <a.m.huq [at] aalto> wrote:
> Hi,
> I have managed to execute vtysh as an external process using popen().
>
> i.e. the following code gives me:
> FILE* read_fp;
>     char buffer[BUFSIZ +1];
>     int num_of_char_read;
>
>     memset(buffer,'\0',sizeof(buffer));
>     read_fp = popen("vtysh -c \"show ip bgp\"","r");
>
>     if(read_fp != NULL)
>     {
>         num_of_char_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
>
>         while(num_of_char_read > 0)
>         {
>             buffer[num_of_char_read - 1] = '\0';
>             printf("reading: \n %s \n",buffer);
>             num_of_char_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
>         }
>
>         pclose(read_fp);
>     }
>
> the following output:
>
> root [at] ahuq-kitche:/home/ahuq/MappingServer# ./zebra_connect
> reading:
>  BGP table version is 0, local router ID is 10.144.18.117
> Status codes: s suppressed, d damped, h history, * valid, > best, i -
> internal,
>               r RIB-failure, S Stale, R Removed
> Origin codes: i - IGP, e - EGP, ? - incomplete
>
>    Network          Next Hop            Metric LocPrf Weight Path
> *> 192.168.57.0     10.144.18.162            0     70      0 100 i
>
> Total number of prefixes 1
>
> ------------- this here is my expected
> output.-----------------------------------
>
> But when I try to do it with execl(), i.e.
>
> #define SHELL "/usr/bin/vtysh"
> int main()
> {
>
>     int pid;
>     pid = fork();
>
>     if(pid == 0)
>     {
>         execl(SHELL,"-c","\" show ip bgp\"",NULL);
>         printf("\n in child");
>     }
>     else
>     {
>         printf("\n parent");
>     }
> return 0;
> }
> I get the following output:
> root [at] ahuq-kitche:/home/ahuq/MappingServer# ./zebra_connect
>
>  parentroot [at] ahuq-kitche:/home/ahuq/MappingServer#
> Hello, this is Quagga (version 0.99.17).
> Copyright 1996-2005 Kunihiro Ishiguro, et al.
>
> ahuq-kitchen#
>
> --------------- That is, the option "-c" and the command "show ip bgp" isn't
> getting passed to vtysh. Why is happening? How can I correct it? My goal is
> to read certain info from vtysh and write certain info to vtysh.
>
> Bye.
>
>
> _______________________________________________
> Quagga-dev mailing list
> Quagga-dev [at] lists
> http://lists.quagga.net/mailman/listinfo/quagga-dev
>
>
_______________________________________________
Quagga-dev mailing list
Quagga-dev [at] lists
http://lists.quagga.net/mailman/listinfo/quagga-dev

Quagga dev RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.