Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 08-03-2010, 05:34 AM   #1
LQ Newbie
Registered: Aug 2010
Posts: 4

Rep: Reputation: 0
i need to write a c code to ping IP addresses which are read from a text file

i need to write a c code to ping IP addresses which are read from a text file and need to log the response time in another text file. please help me with some guidelines.
Old 08-03-2010, 05:46 AM   #2
Registered: Nov 2009
Location: Kolkata, India
Distribution: Fedora 11
Posts: 136

Rep: Reputation: 22
Well it sounds like your homework. But still if you explain why specifically a C program. I believe shell script could do that just as well.

Old 08-03-2010, 06:02 AM   #3
LQ Newbie
Registered: Oct 2006
Location: Belgium, Europe
Distribution: OpenSUSE 11.3 KDE, PCLinuxOS 2010 KDE
Posts: 29

Rep: Reputation: 17
There are two ways to tackle this problem.

The simplest way is to use a system call to the built-in ping program. It has the disadvantage of being (somewhat) OS-dependant, and you'll likely need to parse the output later on.

#include <stdio.h> //snprintf, file I/O
#include <stdlib.h> //system()
#define OUTFILE "out.txt"


//char** ipaddrlist; int addrlistlen;

//read IP addresses from file ...

//call the 'ping' utility
char temp[80];
int i;
for (i = 0; i < addrlistlen; ++i ) {
    addr = ipaddrlist[i];
    snprintf( temp , 80 , "ping %s >> %s" , addr , OUTFILE );
    system( temp );
// Open and parse OUTFILE
The more correct alternative is to compose the ICMP message yourself and listen for replies. This is rather difficult, however. Refer to beej's guide on network programming. Alternatively use a 3rd party library to make this easier.

Last edited by Freex; 08-03-2010 at 06:06 AM.
1 members found this post helpful.
Old 08-09-2010, 07:56 AM   #4
LQ Newbie
Registered: Aug 2010
Posts: 4

Original Poster
Rep: Reputation: 0
thanx imagine & ya freex...i hav used system("...")

Process Flow:
1. Get the name of the file, which contains the following in order
a. Ip address to be pinged
b. Interval of pinging in seconds
c. No_of_pings to be considered for taking mean
d. No_of_seconds to run the monitor
e. File-name, where the output has to be stored

2. Read these values from the file
3. Compose the system command
a. To store the roundtrip times temporarily
b. To append the average value in a permanent log file (1.e)

4. Run the monitoring process
a. Calculate the total time that the monitoring has to be done
b. Monitor till the time elapses
i. Run the system commands (3.a and 3.b)

5. return the average time and the round trip time

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
//volatile sig_atomic_t keep_monitoring = 1;
char ip_address[17],outputfile[50],avg_output[10];
int interval, count_for_averaging,seconds_to_monitor;
char command1[100],command2[100];
char latest_avg[10],round_trip_time[10];


void file_read(){
FILE *fp;
char file_name[50];
printf("enter the name of the file which contains the following in order\n");
printf("\n 1) ip address\n 2) interval to ping (default is 1 sec)\n 3) no of pings to be counted for average(default is 5 secs)\n 4) no of seconds to be monitored(default - 5 secs)\n 5) file name where the calculated average has to be stored(default - avg_file)\n");
if((fp=fopen(file_name, "r")) == NULL) {
printf("Cannot open file.\n");

while( fscanf(fp, "%s%d%d%d%s",ip_address,&interval,&count_for_averaging,&seconds_to_monitor,outputfile)!=EOF){
printf("the file contents\n------------------------------------------------------------------\n");
fprintf(stdout,"\n%s\n%d\n%d\n%d\n%s\n ",ip_address,interval,count_for_averaging,seconds_to_monitor,outputfile);

void comp_command(){
char avg_log[50];
sprintf(command1,"ping %s -c 5 -i 1 | cut -d= -s -f4 | cut -d' ' -f1 > temp_file",ip_address);
// printf("the command1 is %s\n\n",command1);
// printf("enter a file name where the average values has to be stored\n");
// scanf("%s",avg_log);
sprintf(command2,"cat temp_file | xargs -L 5 echo | awk '{print ($1+$2+$3+$4+$5)/5}' >> %s",outputfile);
// printf("the command2 is %s\n\n",command2);

printf("\t\tCOMMANDS EXECUTED\n1) %s\n2) %s\n",command1,command2);
printf("please wait for the process to finish...");


void do_exec_sys(void){


void monitor(){
int keep_monitoring=0;
// printf("for how many seconds do you want to monitor the machine?\n");
// scanf("%d",&seconds);
printf("No of seconds should be in multiples of 5");
printf("This is because the average is calculated once every 5 seconds");
// printf("please wait for the process to finish...");
// signal (SIGALRM, catch_alarm);
// alarm (seconds);
while (keep_monitoring){
printf("\n\n\nmonitoring over....please check the log file");
//return EXIT_SUCCESS;
void return_response_avg(){
FILE *avg_file;
char avg_value[10];
if((avg_file=fopen(outputfile, "r")) == NULL) {
printf("Cannot open file.\n");

while( fscanf(avg_file,"%s",avg_value)!=EOF){


FILE *ping_file;
char resp_time[10];
if((ping_file=fopen("temp_file","r")) == NULL) {
printf("Cannot open file.\n");

while( fscanf(ping_file,"%s",resp_time)!=EOF){

printf("\n------------------------------------------------------------\nThese values have to be returned to the calling function\n\n");
printf("ROUND TRIP TIME - %s ms\n",round_trip_time);
printf("AVERAGE - %s ms\n",latest_avg);
Old 08-09-2010, 07:57 AM   #5
LQ Newbie
Registered: Aug 2010
Posts: 4

Original Poster
Rep: Reputation: 0
i have been trying to implement the same using sockets...!!
Old 10-13-2014, 12:23 AM   #6
LQ Newbie
Registered: Oct 2014
Posts: 2

Rep: Reputation: Disabled
how to write scripting

please help me to write scripting program to ping ip address sequently.. its not my homework.i am student interest to learn please explain step by step linux or c program...


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
[SOLVED] BASH: Write only unique strings to text file (cat or while read question) SilversleevesX Programming 32 08-11-2010 03:24 AM
Cannot read text from text file and store it in a variable using shell script anurupr Linux - Newbie 2 03-03-2010 02:38 PM
bash - read or write to specific line in text file? babag Programming 11 08-23-2008 02:44 PM
a code to read a text file plssssssssssssssssss Medoo Programming 23 04-19-2008 07:48 AM
Simple application to write IP addresses on a network to a file n0kx Linux - Software 3 09-12-2007 05:35 AM

All times are GMT -5. The time now is 01:02 PM.

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