Thread: help issuing linux command to console and using result in the program

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    26

    help issuing linux command to console and using result in the program

    I am quite new to c/c++ programming and would love to incorperate it more into what I do.

    Right now I have a bash script that uses standerd unix commands such as grep, awk, cut and sed. My intent was to write this to remove the ip from ifconfig and cut it down to just the subnet without the periods. such as 169244255

    Code:
    address=$(/sbin/ifconfig eth1 | grep addr | awk '{print $2}' | cut-c5- | cut -c2-12 | sed -e 's/\.//g')
    so $address is now my variable that I can hold and echo, or what have you as I like.

    I would like some help, not writing a full program, but how could I write in C to issue the above command to the console and get back its output, or store it as an int or something so I could use the 169244255 or w/e comes out of my unix command in my c program.

    to make it much simpler to understand so I can get some help, it could be as simple as using the linux environmental var $PATH or any command that could be issued to the console and its output used in the initial C program. I believe I have done something simuler in windows with a dos command a long time ago but cant seem to remember.

    MUCH thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Study the output of
    $ strace /sbin/ifconfig eth0

    Write the code to access the information directly from the C program rather than trying to re-implement bash in C.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    Quote Originally Posted by Salem
    Study the output of
    $ strace /sbin/ifconfig eth0

    Write the code to access the information directly from the C program rather than trying to re-implement bash in C.
    Well im not a master a writing c programs

    strace /sbin/ifconfig eth0 gives me

    Code:
    master ~/programming # strace /sbin/ifconfig eth0
    execve("/sbin/ifconfig", ["/sbin/ifconfig", "eth0"], [/* 40 vars */]) = 0
    uname({sys="Linux", node="master", ...}) = 0
    brk(0)                                  = 0x8054848
    access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
    open("/etc/ld.so.cache", O_RDONLY)      = 3
    fstat64(3, {st_mode=S_IFREG|0644, st_size=28188, ...}) = 0
    mmap2(NULL, 28188, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40015000
    close(3)                                = 0
    open("/lib/libc.so.6", O_RDONLY)        = 3
    read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\340V\1"..., 512) = 512
    fstat64(3, {st_mode=S_IFREG|0755, st_size=1228960, ...}) = 0
    mmap2(NULL, 1142148, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x4001c000
    mprotect(0x4012c000, 28036, PROT_NONE)  = 0
    mmap2(0x4012d000, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x110) = 0x4012d000
    mmap2(0x40131000, 7556, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x40131000
    close(3)                                = 0
    mprotect(0x4012d000, 4096, PROT_READ)   = 0
    munmap(0x40015000, 28188)               = 0
    open("/dev/urandom", O_RDONLY)          = 3
    read(3, "\236%\344\222", 4)             = 4
    close(3)                                = 0
    brk(0)                                  = 0x8054848
    brk(0x8075848)                          = 0x8075848
    brk(0x8076000)                          = 0x8076000
    uname({sys="Linux", node="master", ...}) = 0
    access("/proc/net", R_OK)               = 0
    access("/proc/net/unix", R_OK)          = 0
    socket(PF_FILE, SOCK_DGRAM, 0)          = 3
    socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 4
    access("/proc/net/if_inet6", R_OK)      = -1 ENOENT (No such file or directory)
    access("/proc/net/ax25", R_OK)          = -1 ENOENT (No such file or directory)
    access("/proc/net/nr", R_OK)            = -1 ENOENT (No such file or directory)
    access("/proc/net/rose", R_OK)          = -1 ENOENT (No such file or directory)
    access("/proc/net/ipx", R_OK)           = -1 ENOENT (No such file or directory)
    access("/proc/net/appletalk", R_OK)     = -1 ENOENT (No such file or directory)
    access("/proc/sys/net/econet", R_OK)    = -1 ENOENT (No such file or directory)
    access("/proc/sys/net/ash", R_OK)       = -1 ENOENT (No such file or directory)
    access("/proc/net/x25", R_OK)           = -1 ENOENT (No such file or directory)
    open("/proc/net/dev", O_RDONLY)         = 5
    fstat64(5, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
    mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40015000
    read(5, "Inter-|   Receive               "..., 1024) = 577
    close(5)                                = 0
    munmap(0x40015000, 4096)                = 0
    ioctl(4, SIOCGIFFLAGS, {ifr_name="eth0", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
    ioctl(4, SIOCGIFHWADDR, {ifr_name="eth0", ifr_hwaddr=00:50:da:0f:cc:7f}) = 0
    ioctl(4, SIOCGIFMETRIC, {ifr_name="eth0", ifr_metric=0}) = 0
    ioctl(4, SIOCGIFMTU, {ifr_name="eth0", ifr_mtu=1500}) = 0
    ioctl(4, SIOCGIFMAP, 0xbffff73c)        = 0
    ioctl(4, SIOCGIFMAP, 0xbffff73c)        = 0
    ioctl(4, SIOCGIFTXQLEN, 0xbffff73c)     = 0
    ioctl(4, SIOCGIFADDR, {ifr_name="eth0", ifr_addr={AF_INET, inet_addr("192.168.1.1")}}) = 0
    ioctl(4, SIOCGIFDSTADDR, {ifr_name="eth0", ifr_dstaddr={AF_INET, inet_addr("192.168.1.1")}}) = 0
    ioctl(4, SIOCGIFBRDADDR, {ifr_name="eth0", ifr_broadaddr={AF_INET, inet_addr("192.168.1.255")}}) = 0
    ioctl(4, SIOCGIFNETMASK, {ifr_name="eth0", ifr_netmask={AF_INET, inet_addr("255.255.255.0")}}) = 0
    fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
    mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40015000
    write(1, "eth0      Link encap:Ethernet  H"..., 58eth0      Link encap:Ethernet  HWaddr 00:50:DA:0F:CC:7F
    ) = 58
    write(1, "          inet addr:192.168.1.1 "..., 73          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0
    ) = 73
    open("/proc/net/if_inet6", O_RDONLY)    = -1 ENOENT (No such file or directory)
    write(1, "          UP BROADCAST RUNNING M"..., 61          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
    ) = 61
    write(1, "          RX packets:215069171 e"..., 69          RX packets:215069171 errors:0 dropped:0 overruns:0 frame:0
    ) = 69
    write(1, "          TX packets:216064497 e"..., 71          TX packets:216064497 errors:0 dropped:0 overruns:0 carrier:0
    ) = 71
    write(1, "          collisions:0 txqueuele"..., 40          collisions:0 txqueuelen:1000
    ) = 40
    write(1, "          RX bytes:1441299436 (1"..., 73          RX bytes:1441299436 (1374.5 Mb)  TX bytes:649580468 (619.4 Mb)
    ) = 73
    write(1, "          Interrupt:11 Base addr"..., 44          Interrupt:11 Base address:0xdc00
    ) = 44
    write(1, "\n", 1
    )                       = 1
    close(4)                                = 0
    munmap(0x40015000, 4096)                = 0
    exit_group(0)                           = ?

    so basicly what your saying is I could write a c program that could give me simuler results as running it in bash.. without interacting with the shell at all?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yeah, basically you read the files in /proc/net

    So, why are you trying to run a bunch of programs inside a C program as if it were the shell?
    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.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    26
    well I could essentially do it completly in bash.. but I really wanted to get the output of
    Code:
    /sbin/ifconfig eth1 | grep addr | awk '{print $2}' | cut-c5- | cut -c2-12 | sed -e 's/\.//g'
    saved as an int or something in my program so I could check the output against something else in the program, or do while that int stays true, if it changes go on to the rest of the stuff in my program. Its hard to explain but thats essentially what im trying to do in c.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if it changes go on to the rest of the stuff in my program
    How would the local IP address of the machine change?

    That is after all the information you're extracting.

    Also, you can do all this using awk
    Code:
    /sbin/ifconfig eth0 | awk '/addr:[0-9]/{a=$2;sub("addr:","",a);gsub("\\.","",a);print a}'
    Perhaps in C, you could try
    Code:
    char buff[BUFSIZ];
    FILE *fp = popen("/sbin/ifconfig eth0 | awk '/addr:[0-9]/{a=$2;sub("addr:","",a);gsub("\\.","",a);print a}'*,"r");
    fgets( buff, sizeof buff, fp );
    pclose( fp );
    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.

Popular pages Recent additions subscribe to a feed