ping命令 只输出一次“is alive”,busybox配置FEATURE_FANCY_PING解决
调试Allwinner V536
测试ping功能发现输出结果非常简洁
root@tina:/# ping 192.168.11.1 192.168.11.1 is alive! root@tina:/# root@tina:/# ping www.baidu.com www.baidu.com is alive! root@tina:/# ping 192.168.11.1 -s 192.168.11.1 is alive! root@tina:/# ping BusyBox v1.27.2 () multi-call binary. Usage: ping HOST Send ICMP ECHO_REQUEST packets to network hosts root@tina:/# ping 127.0.0.1 127.0.0.1 is alive! root@tina:/# ping 192.168.11.1 -n 1000 192.168.11.1 is alive!
与以往使用ping 的输出很不一样,根据关键字"is alive“,找到源码部分
288 #if !ENABLE_PING6 289 # define common_ping_main(af, argv) common_ping_main(argv) 290 #endif 291 static int common_ping_main(sa_family_t af, char **argv) 292 { 293 len_and_sockaddr *lsa; 294 295 INIT_G(); 296 297 #if ENABLE_PING6 298 while ((++argv)[0] && argv[0][0] == '-') { 299 if (argv[0][1] == '4') { 300 af = AF_INET; 301 continue; 302 } 303 if (argv[0][1] == '6') { 304 af = AF_INET6; 305 continue; 306 } 307 bb_show_usage(); 308 } 309 #else 310 argv++; 311 #endif 312 313 G.hostname = *argv; 314 if (!G.hostname) 315 bb_show_usage(); 316 317 #if ENABLE_PING6 318 lsa = xhost_and_af2sockaddr(G.hostname, 0, af); 319 #else 320 lsa = xhost_and_af2sockaddr(G.hostname, 0, AF_INET); 321 #endif 322 /* Set timer _after_ DNS resolution */ 323 signal(SIGALRM, noresp); 324 alarm(5); /* give the host 5000ms to respond */ 325 326 create_icmp_socket(lsa); 327 G.myid = (uint16_t) getpid(); 328 #if ENABLE_PING6 329 if (lsa->u.sa.sa_family == AF_INET6) 330 ping6(lsa); 331 else 332 #endif 333 ping4(lsa); 334 printf("%s is alive!\n", G.hostname); 335 return EXIT_SUCCESS; 336 } 337 338 339 #else /* FEATURE_FANCY_PING */ 340 341 342 /* Full(er) version */ 343 344 #define OPT_STRING ("qvc:+s:t:+w:+W:+I:np:4" IF_PING6("6"))
末尾有个宏定义以及Full(er) version,说明这里有两个版本当前运行的是简化版本,还有一个全功能版本,就在#else后面
339 #else /* FEATURE_FANCY_PING */
340
341
342 /* Full(er) version */
860 static int common_ping_main(int opt, char **argv) 861 { 862 len_and_sockaddr *lsa; 863 char *str_s, *str_p; 864 865 INIT_G(); 866 867 /* exactly one argument needed; -v and -q don't mix; -c NUM, -t NUM, -w NUM, -W NUM */ 868 opt_complementary = "=1:q--v:v--q"; 869 opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I, &str_p); 870 if (opt & OPT_s) 871 datalen = xatou16(str_s); // -s 872 if (opt & OPT_I) { // -I 873 if_index = if_nametoindex(str_I); 874 if (!if_index) { 875 /* TODO: I'm not sure it takes IPv6 unless in [XX:XX..] format */ 876 source_lsa = xdotted2sockaddr(str_I, 0); 877 str_I = NULL; /* don't try to bind to device later */ 878 } 879 } 880 if (opt & OPT_p) 881 G.pattern = xstrtou_range(str_p, 16, 0, 255); 882 883 myid = (uint16_t) getpid(); 884 hostname = argv[optind]; 885 #if ENABLE_PING6 886 { 887 sa_family_t af = AF_UNSPEC; 888 if (opt & OPT_IPV4) 889 af = AF_INET; 890 if (opt & OPT_IPV6) 891 af = AF_INET6; 892 lsa = xhost_and_af2sockaddr(hostname, 0, af); 893 } 894 #else 895 lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET); 896 #endif 897 898 if (source_lsa && source_lsa->u.sa.sa_family != lsa->u.sa.sa_family) 899 /* leaking it here... */ 900 source_lsa = NULL; 901 902 dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa); 903 ping(lsa); 904 print_stats_and_exit(EXIT_SUCCESS); 905 /*return EXIT_SUCCESS;*/ 906 } 907 #endif /* FEATURE_FANCY_PING */
后面这个
common_ping_main
才是我需要的功能,
往前找#if FEATURE_FANCY_PING开始的地方
181 182 #if !ENABLE_FEATURE_FANCY_PING 183 184 /* Simple version */
说明需要配置
ENABLE_FEATURE_FANCY_PING
这个宏定才会编译全功能版本
回到croot 执行 make menuconfig
选择
Base system ---> <*>busybox............ Networking Utilities ---> [*] ping [*] ping6 [*] Enable fancy ping output
选上 Enable fancy ping output 即可,save 退出.
重新编译busybox 打包烧录测试结果如下
root@tina:/# ping 192.168.11.1 PING 192.168.11.1 (192.168.11.1): 56 data bytes 64 bytes from 192.168.11.1: seq=0 ttl=64 time=3.362 ms 64 bytes from 192.168.11.1: seq=1 ttl=64 time=2.890 ms 64 bytes from 192.168.11.1: seq=2 ttl=64 time=2.547 ms 64 bytes from 192.168.11.1: seq=3 ttl=64 time=3.168 ms 64 bytes from 192.168.11.1: seq=4 ttl=64 time=2.894 ms ^C --- 192.168.11.1 ping statistics --- 5 packets transmitted, 5 packets received, 0% packet loss round-trip min/avg/max = 2.547/2.972/3.362 ms root@tina:/# ping www.baidu.com PING www.baidu.com (14.215.177.39): 56 data bytes 64 bytes from 14.215.177.39: seq=0 ttl=54 time=10.579 ms 64 bytes from 14.215.177.39: seq=1 ttl=54 time=10.299 ms 64 bytes from 14.215.177.39: seq=2 ttl=54 time=8.868 ms 64 bytes from 14.215.177.39: seq=3 ttl=54 time=10.585 ms 64 bytes from 14.215.177.39: seq=4 ttl=54 time=8.259 ms 64 bytes from 14.215.177.39: seq=5 ttl=54 time=10.996 ms ^C --- www.baidu.com ping statistics --- 6 packets transmitted, 6 packets received, 0% packet loss round-trip min/avg/max = 8.259/9.931/10.996 ms root@tina:/#
深圳宝安华美居