i need to write a c code to ping IP addresses which are read from a text file
Linux - NewbieThis 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!
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.
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.
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.
Distribution: OpenSUSE 11.3 KDE, PCLinuxOS 2010 KDE
Posts: 29
Rep:
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.
Code:
#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.
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
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");
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");
printf("-----------------------------------------------------------------\n");
scanf("%s",file_name);
if((fp=fopen(file_name, "r")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}
//-----------------------------------------------------------------------------------------------
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("-------------------------------------------------------------------------------\n");
printf("please wait for the process to finish...");
}
void monitor(){
int keep_monitoring=0;
// printf("for how many seconds do you want to monitor the machine?\n");
// scanf("%d",&seconds);
if((seconds_to_monitor%count_for_averaging)!=0){
printf("No of seconds should be in multiples of 5");
printf("This is because the average is calculated once every 5 seconds");
monitor();
}
// printf("please wait for the process to finish...");
// signal (SIGALRM, catch_alarm);
// alarm (seconds);
keep_monitoring=(seconds_to_monitor/count_for_averaging);
while (keep_monitoring){
do_exec_sys();
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");
exit(1);
}
}
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);
printf("------------------------------------------------------\n";
}
//----------------------------------------------------------------------main(){
file_read();
comp_command();
monitor();
return_response_avg();
}
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 ..in linux or c program...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.