popen and /bin/bash -c

This is a discussion on popen and /bin/bash -c within the C Programming forums, part of the General Programming Boards category; Hello, I use fp = popen("/bin/bash -c command" , "r") to read the output of the command. The problem is ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    43

    popen and /bin/bash -c

    Hello,

    I use fp = popen("/bin/bash -c command" , "r") to read the output of the command.
    The problem is that i don't take the whole output, i take only the output that an unmaximize shell window shows. Is it possible to take the whole text?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Of course. #1) what's the command. #2) post the code you are using to read from it.

    Be aware some command output is to stderr, not stdout, and popen only reads from stdout. However, you can redirect by appending "2>&1" to the command.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    My guessing muscles are a little rusty, but I would guess that means that you are using fgets to read into an 80-character buffer, meaning you will only get 80 characters at a time (although notice that subsequent reads should get you the rest of the line). If you want longer lines, make the lines bigger (heck, go crazy, use BUFSIZ).

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    i use this :

    fp2 = popen("/bin/bash -c 'airodump-ng mon0 2>&1' ", "r");

    setvbuf ( fp2, NULL, _IOLBF, NULL);

    i read with this :

    fgets(path, PATH_MAX, fp2);

    and i get this

    CH 1 ][ Elapsed: 8 s ][ 2010-05-02 00:56

    BSSID PWR Beacons #Data, #/s CH MB ENC CIPHER AUTH ESSID

    00:1F:9F:CF:F5:A3 -48 10 0 0 1 54 WPA2 CCMP PSK Thoms

    BSSID STATION PWR Rate Lost Packets Probes

    instead of this

    CH 8 ][ Elapsed: 0 s ][ 2010-05-02 00:59

    BSSID PWR Beacons #Data, #/s CH MB ENC CIPHER AUTH ESSID

    00:1F:9F:CF:F5:A3 -46 6 0 0 1 54 WPA2 CCMP PSK Thomson679897

    BSSID STATION PWR Rate Lost Packets Probes

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hmm. It could be that this app detects the terminal size to decide how to format the output, and uses an 80 char default. That's sort of a design flaw, you should check to see there are no command line switches to control this.

    What happens if you redirect the output to a file while in an unmaximized terminal?

    airodump-ng mon0 > tmp.txt
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    same thing. if the terminal is unmaximized i only get thom :/ how can i fix it? i want the whole output

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Quote Originally Posted by tabstop View Post
    My guessing muscles are a little rusty, but I would guess that means that you are using fgets to read into an 80-character buffer, meaning you will only get 80 characters at a time (although notice that subsequent reads should get you the rest of the line). If you want longer lines, make the lines bigger (heck, go crazy, use BUFSIZ).

    could you give me an example? it's 2 am here in Greece, my mind myscles don't work at all :/
    Last edited by TuXaKoS; 05-01-2010 at 05:21 PM.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, so it is the app itself then. There may be nothing you can do about it, like I said this would be a design flaw/oversight. Unless there is a switch in the documentation...

    There is a bash environment, COLUMNS, which changes when you resize the terminal. Hopefully, your app uses that.

    With the terminal unmaximized, type:

    [prompt] echo $COLUMNS

    The answer is probably 80. Now try to spoof the value:

    [prompt] COLUMNS=50

    No spaces around the equals sign! Now execute your command. If it prints out only fifty chars wide, that's your ticket (also try settting it to 120, output to a file, and check that).

    I can't say for sure that you can set an env variable for popen because I've never tried, but if that works, try using setenv() to set COLUMNS before the popen() call. You could also use "COLUMNS=120 && airodump-ng mon0"
    Last edited by MK27; 05-01-2010 at 05:30 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Noep, nothing changed :/ maybe the problem is at the buffer?

    i change the columns to 50. I use echo $COLUMNS it returns 50. I run the airodump-ng mon0 > tmp.txt and the columns change to 80 :/
    Last edited by TuXaKoS; 05-01-2010 at 05:35 PM.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yeah, it doesn't affect the shell's own internal commands either. It's just the only applicable one I could find.

    Are you sure there is no option to the command at all for formatting? Do you have the source code?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Quote Originally Posted by MK27 View Post
    Yeah, it doesn't affect the shell's own internal commands either. It's just the only applicable one I could find.

    Are you sure there is no option to the command at all for formatting? Do you have the source code?
    Yes, i have the source no command for formating :/ pff i need the whole output :/

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Wow. Well, you could try poking around the source to find out how it does this. It should be there somewhere. Seeing as how the default is 80, you could try searching for that number, methinks they do set it since using popen does not involve a terminal, it is not bash which provides that number.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    hm.. i don't think that COLUMNS is the problem. :/ i use setenv to change it to 125. I use getenv before and after popen and it's still 125 but the output is only 80. :/

    i creater a buffer, char *testbuffer[BUFSIZ]

    and i used

    setvbuf ( fp2, testbuffer, _IOLBF, BUFSIZ);

    but nothing changed :/
    Last edited by TuXaKoS; 05-01-2010 at 06:29 PM.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yeah, COLUMNS may only be used by bash to format select() output.

    Looks like there are a few ways of doing this in C tho. Check the source to see if they include <termcap.h> or <sys/ioctl.h>. ioctl things are all set/got with the ioctl() C function. There's tons of them, but they are mostly undocumented.

    Have a look here:
    Getting terminal width in C? - Stack Overflow
    Notice they are using various pre-defined structures and bitflags such as "winsize" and TIOCGWINSZ. These will be undocumented too, but since you are only looking to see if they are used, that is not such a big deal.

    If you can dig around in the source and figure out how this decision is being made, you're most of the way to a solution. Sort of tedious, but you may learn some stuff. ioctl() is a nifty thing to know about, as the name implies it's kind of a catch-all for dealing with various system I/O protocols.
    Last edited by MK27; 05-01-2010 at 06:38 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    Did you try calling airodump directly instead of calling it through bash? What is the reason for this by the way?

    Also, so I'm assuming this is for wireless cracking ?


    EDIT: Also if you just run ENVVAR="blah" alone by itself it just sets that variable to "blah" then promptly discards it and restores its original value. The correct way would be to either "export ENVVAR=blah" or to include it on the call to airodump on the command line:
    Code:
    me@box$ ENVVAR=blah airodump-ng whatever 2>&1
    Last edited by nonpuz; 05-01-2010 at 06:45 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #!/bin/bash question
    By elsheepo in forum C Programming
    Replies: 10
    Last Post: 03-09-2009, 12:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21