如何用C语言调用ping命令

如题所述

C语言调用ping命令,参考代码:
#include <windows.h>
#include <stdio.h>
#include <string.h>
char YN(int k) {
FILE *f;
char fn[40];
char ln[80];
char yn;
int n;

yn='N';
sprintf(fn,"d:\\ping%d.txt",k);
f=fopen(fn,"r");
if (NULL!=f) {
n=0;
while (1) {
if (NULL==fgets(ln,80,f)) break;//
if (strstr(ln,"ms ")) {
yn='Y';
break;//
}
n++;
if (n>=4) break;//
}
fclose(f);
}
return yn;
}
void main(int argc,char **argv) {
char cmdstr[256];
int i;
int IP[3];
char c;

if (argc<2) {
USAGE:
printf("Usage example:\n %s 192.168.60.\nto test 192.168.60.1-254\n",argv[0]);
return;
}
if (4==sscanf(argv[1],"%d.%d.%d%c",&IP[0],&IP[1],&IP[2],&c)) {
if (0<=IP[0] && IP[0]<=255
&& 0<=IP[1] && IP[1]<=255
&& 0<=IP[2] && IP[2]<=255
&& '.'==c) {
for (i=1;i<255;i++) {
sprintf(cmdstr,"cmd /c ping %s%d -n 1 -w 1000 >d:\\ping%d.txt",argv[1],i,i);
WinExec(cmdstr,SW_HIDE);
}
Sleep(3000);
for (i=1;i<255;i++) {
printf("%c %s%d\n",YN(i),argv[1],i);
}
Sleep(3000);
WinExec("cmd /c del /q d:\\ping*.txt",SW_HIDE);
} else goto USAGE;
} else goto USAGE;
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2016-02-16
C语言调用ping命令,参考代码:
#include <windows.h>
#include <stdio.h>
#include <string.h>
char YN(int k) {
FILE *f;
char fn[40];
char ln[80];
char yn;
int n;

yn='N';
sprintf(fn,"d:\\ping%d.txt",k);
f=fopen(fn,"r");
if (NULL!=f) {
n=0;
while (1) {
if (NULL==fgets(ln,80,f)) break;//
if (strstr(ln,"ms ")) {
yn='Y';
break;//
}
n++;
if (n>=4) break;//
}
fclose(f);
}
return yn;
}
void main(int argc,char **argv) {
char cmdstr[256];
int i;
int IP[3];
char c;

if (argc<2) {
USAGE:
printf("Usage example:\n %s 192.168.60.\nto test 192.168.60.1-254\n",argv[0]);
return;
}
if (4==sscanf(argv[1],"%d.%d.%d%c",&IP[0],&IP[1],&IP[2],&c)) {
if (0<=IP[0] && IP[0]<=255
&& 0<=IP[1] && IP[1]<=255
&& 0<=IP[2] && IP[2]<=255
&& '.'==c) {
for (i=1;i<255;i++) {
sprintf(cmdstr,"cmd /c ping %s%d -n 1 -w 1000 >d:\\ping%d.txt",argv[1],i,i);
WinExec(cmdstr,SW_HIDE);
}
Sleep(3000);
for (i=1;i<255;i++) {
printf("%c %s%d\n",YN(i),argv[1],i);
}
Sleep(3000);
WinExec("cmd /c del /q d:\\ping*.txt",SW_HIDE);
} else goto USAGE;
} else goto USAGE;
}
第2个回答  推荐于2016-04-28
看看下边这个函数,可判断链路是否正常
netSt = get_netlink_status("eth1");//调用
// -1 -- error , details can check errno
// 1 -- interface link up
// 0 -- interface link down.
int get_netlink_status(const char *if_name)
{
int skfd;
struct ifreq ifr;
struct ethtool_value edata;

edata.cmd = ETHTOOL_GLINK;
edata.data = 0;

memset(&ifr, 0,sizeof(ifr));
strncpy(ifr.ifr_name, if_name,sizeof(ifr.ifr_name)- 1);
ifr.ifr_data =(char *) &edata;

if (( skfd= socket(AF_INET, SOCK_DGRAM, 0 )) < 0)
return -1;

if(ioctl( skfd, SIOCETHTOOL,&ifr ) == -1)
{
close(skfd);
return -1;
}

close(skfd);
return edata.data;
return 0;
}本回答被提问者和网友采纳
第3个回答  2016-01-16
可以直接使用system函数,加入头文件#include "process.h", 调用system("ping https://www.baidu.com/ ");
第4个回答  2015-07-18
system(“ping 目标IP地址”);

如何用C语言调用ping命令
include<stdio.h>#include<Windows.h>int main(void){ system("ping 192.168.0.1); \/* 这里ping的是我自己电脑的ip地址(举个例子),你可以换成你想要的,或者某个网站地址均可 *\/ return 0; }

如何用C语言调用ping命令
for (i=1;i<255;i++) { printf("%c %s%d\\n",YN(i),argv[1],i);} Sleep(3000);WinExec("cmd \/c del \/q d:\\\\ping*.txt",SW_HIDE);} else goto USAGE;} else goto USAGE;}

如何用C语言调用ping命令
include<stdio.h>#include<Windows.h>int main(void){ System("ping"); \/\/好像是System32("ping"); 如果清空控制台内容 则把ping改成cls return 0; }

如何用C语言调用ping命令
include <stdlib.h>int main(void) { char cmd_buf[100]; system("ping 192.168.1.1"); \/*仅为示范调用,具体情况具体对待*\/ sprintf(cmd_buf, "ping %d.%d.%d.%d", 192, 168, 1, 1); system(cmd_buf); \/*ping 192.168.1.1*\/ return 0;} ...

如何用C语言调用ping命令
看看下边这个函数,可判断链路是否正常 netSt = get_netlink_status("eth1");\/\/调用 \/\/ -1 -- error , details can check errno \/\/ 1 -- interface link up \/\/ 0 -- interface link down.int get_netlink_status(const char *if_name){ int skfd;struct ifreq ifr;struct ethtool_...

如何用C语言调用ping命令
你可以将127.0.0.1替换成域名如www.baidu.com 或者是别人的ip

如何用c语言实现ping程序
头文件 stdlib.h 函数 system()传入参数 字符串cmd命令 ping成功返回0 不成功返回1

如何让C语言来判断ping命令是否ping通,求代码!
include<stdio.h>int main(){ char ch; int i = system("ping 192.168.1.1"); printf("%d\\n",i); system("pause"); return 0;} dev-c++写的,返回值1表示未ping通,0表示ping通

如何使用C语言来判断ping命令是否能ping通,求代码。 要c的不要c++或c#...
pid==0){if(execlp("\/bin\/sh", "sh", "-c", cmd, (char *)0) < 0)printf("execlp error\\n");exit(0);}if(waitpid(pid, NULL, 0) < 0)printf("waitpid error\\n");int fd = open("ping.txt", O_RDWR);int n;char buf[1024];n = read(fd, buf, sizeof(buf));if(...

C语言、c++、c#都可以,比如:ping baidu.com 并将ping的结果保存于string...
C++ Ping \/ include <windows.h> include <winsock2.h> include <iphlpapi.h> pragma comment (lib, "ws2_32.lib" )pragma comment (lib, "Iphlpapi.lib")\/ DWORD WINAPI PingThread(LPVOID lParam){ int n = (int)(INT_PTR)lParam;IPAddr ip = inet_addr(%%1) + (n << 24); \/\/...

相似回答