其实直接把完整的ping命令以及地址写到字符数组里即可。
程序代码:
root@~ #cat 4.c
#include <stdio.h>
int main (void) {
char ip[]="ping ",ch; //char ping[]="ping -c1 hostname"
printf ("Enter IP:");
int i=5;
do {
ch=getchar();
ip[i]=ch;
i++;
}while(ch!='\n');
ip[i]='\0';
system(ip); //直接系统调用
return 0;
}
测试:
root@~ #./4
Enter IP:127.0.0.1
PING 127.0.0.1 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0 ms
----127.0.0.1 PING Statistics----
5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip (ms)
min/avg/max/med = 0/0/0/0
root@~ #
[
本帖最后由 ansic 于 2011-3-26 21:12 编辑 ]