Thread: How to catch the result of linux command

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    8

    How to catch the result of linux command

    I am writing a cgi in c and try to figure out how to print out
    the result from linux command.

    ex: print of the result of "date" or "ls"

    Can anyone give me some hint or search keyword to begin with?

    I tried execl() but it seems I am not use it correctly .

    Thanks

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    the exec* family of commands do not return unless something went horribly wrong. They replace the current running process with an image of the new one.

    I think you want system()

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I am writing a cgi in c
    Hello security holes.
    You'd better make sure there are no buffer overflow possibilities in that code.

    > how to print out the result from linux command.
    Are any of the commands the result of anything the user can type in at the web interface?
    If so, hello to more security holes. You need to strongly verify the names of any programs which you get requests to run.

    Try using a pipe
    Code:
    FILE *fp = popen("ls -l", "r" );
    if ( fp != NULL ) {
      /* read lines using fgets(), just like you would with a regular file  */
      pclose( fp );
    }

    Better make sure your stuff is running as nobody, but I rather suspect it's running as root.
    Oh well.....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    8
    Thanks for the response.

    I tried it but still not quit sure how to make it work.
    Maybe I didn't state it clearly.

    I need to use cgi to run some linux command and display
    the result to a HTML page.
    I tried system("ls") but it seems not work.

    Is it possible to provide me some sample concept code?

    I am not quit sure how to handling the security issue but
    I will do some research after I figure things out.

    Thanks for remind me that.

    Best,

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > and display the result to a HTML page.
    Assuming you can display text of your choice, I just did show you how to do it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    8
    Is it possible to catch all result and store in a char* and print it out
    later?

    like ..

    printf("<TR><TD></TD></TR>"); and print the linux "Date"
    command within the <TD></TD> tag?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You mean like
    Code:
    printf( "<TR><TD>" );
    {
      FILE *fp = popen("date", "r" );
      if ( fp != NULL ) {
        char buff[BUFSIZ];
        while ( fgets( buff, sizeof buff, fp ) != NULL ) {
          puts( buff );
        }
        pclose( fp );
      }
    }
    printf( "</TD></TR>" );
    Though for something that simple, calling time() and strftime() would be much more efficient.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    8
    It seems work.

    I don't really wanna use "date" but some function I wrote.
    date is just for testing.

    thanks for your help.

    I am really not a c guy but suddently I need to face it.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    8
    How can I split the result I got from Linux command?

    If I got IP address 255.255.255.255, how can I store them
    to a array of string or to 4 different string.

    Best,

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do you want to split it in the shell command (say using the 'tr' utility), or split it inside the C program (say using sscanf) ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    8
    I think I would like to know how to do it in C program

    If you don't mind, I would like to learn both of them.
    In case I need them in the future so I know how to deal with it.

    Actually I will run a customize shell script to return bunch of
    information from a device.
    The responsed output will like .ini file format.
    [xxx]
    name=value
    name2=value2
    name3=255.255.255.255
    name4=1,2,3,4

    I need to parse those value out to generate a HTML forum by cgi

    For example I need to use 4
    <input type=text ..> to display the ip value

    thanks for your quick response

    best,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone help with my seg fault?
    By John_L in forum C++ Programming
    Replies: 23
    Last Post: 03-01-2008, 04:04 PM
  2. exe files in -c- language
    By enjoy in forum C Programming
    Replies: 6
    Last Post: 05-18-2004, 04:36 PM
  3. Linux Linux why Linux??
    By afreedboy in forum Tech Board
    Replies: 146
    Last Post: 01-21-2004, 06:27 PM
  4. Command Interface : Black Box testing
    By GlowinCelica in forum C Programming
    Replies: 1
    Last Post: 03-10-2003, 10:21 PM
  5. cd command in linux console
    By TheUnheardHuman in forum Tech Board
    Replies: 2
    Last Post: 11-19-2002, 03:59 PM