Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-05-2005, 08:12 AM
|
#1
|
LQ Newbie
Registered: Feb 2005
Posts: 17
Rep:
|
call a c program in a shell script
good day to all of you,
Im trying to do a C program that gets information from another program (TCPDUMP) and what i`m trying to do is run tcpdump from the terminal and get the information that I need from the output of tcpdump, and use it in my C program. My question is:
In a C program I can run a command from the shell, and use the output of that command in the C program?
Or in a Shell script I can call a C program? and pass the output of some functions in the script to the C program?
If there is a another way to do what I explained above,tell me, please.
Thanks for your time.
|
|
|
04-05-2005, 09:30 AM
|
#2
|
Senior Member
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197
Rep:
|
You could pipe the output of tcpdump into your C program, and then have the C program read stdin.
You could also have tcpdump capture to a file (see -w) and then just read the file back with your C program.
You could also just use libpcap directly in your C program to monitor the stream directly, but without more info on your purpose, that's probably "reinventing the wheel" ...
|
|
|
04-05-2005, 03:00 PM
|
#3
|
Member
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192
Rep:
|
Quote:
You could pipe the output of tcpdump into your C program, and then have the C program read stdin.
You could also have tcpdump capture to a file (see -w) and then just read the file back with your C program.
|
you could also do both. having your c program read from stdin is extremely handy for using it in a pipe line. you could have your c program read from stdin if there are no command line arguments, and otherwise use the filename passed to it throught the command line as input.
|
|
|
04-05-2005, 04:17 PM
|
#4
|
LQ Newbie
Registered: Feb 2005
Posts: 17
Original Poster
Rep:
|
thanks for the advice.
If i decide to read the file, saved with the option -w of tcpdump, in the C program, how can read and save only some fields of the file, for expample grab only the src/dst port and the bytes sent?
because i only need these fields to work with them.
|
|
|
04-05-2005, 04:58 PM
|
#5
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536
Rep: 
|
Quote:
In a C program I can run a command from the shell, and use the output of that command in the C program?
|
I'd use popen().
Quote:
If i decide to read the file, saved with the option -w of tcpdump, in the C program, how can read and save only some fields of the file, for expample grab only the src/dst port and the bytes sent?
because i only need these fields to work with them.
|
Have a look at libpcap.
|
|
|
All times are GMT -5. The time now is 07:07 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|