LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-05-2005, 08:12 AM   #1
jagman
LQ Newbie
 
Registered: Feb 2005
Posts: 17

Rep: Reputation: 0
Question 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.
 
Old 04-05-2005, 09:30 AM   #2
sigsegv
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: Reputation: 47
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" ...
 
Old 04-05-2005, 03:00 PM   #3
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
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.
 
Old 04-05-2005, 04:17 PM   #4
jagman
LQ Newbie
 
Registered: Feb 2005
Posts: 17

Original Poster
Rep: Reputation: 0
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.
 
Old 04-05-2005, 04:58 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script call API treotan Programming 1 08-22-2005 10:59 PM
Call a shell script from php? jharper101 Programming 2 02-15-2005 12:51 AM
Urgent: Call a C program through Linux shell script nuwandee Programming 14 04-10-2004 07:31 PM
call shell or other program out of my_program lea Programming 3 10-09-2002 10:51 AM
how to put a shell call in a C/C++ program? Hano Programming 8 05-19-2002 02:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration