| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2661 人关注过本帖
标题:请教怎样快速扫描局域网(C 语言版)
只看楼主 加入收藏
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:0 
以下是引用madfrogme在2012-8-19 16:28:56的发言:

我已经把-n 该成-c  
把-w 该成-W了,
--
如果你ping得地址是没错的话 应该是没问题的啊

linux下跑这个挺快的嘛

程序代码:
#!/bin/bash
STEP=32
IP=127.0.0.
for ((i=1;i<256;i=i+STEP))
do
    echo ping $IP$i
    for ((j=i;j<i+STEP&&j<256;j++))
    do
        ping $IP$j -c 1 -W 1 > $j.txt &
    done
    # sleep 1
done
cat /dev/null > result.txt
for ((i=1;i<256;i++))
do
    cat $i.txt >> result.txt
done


[root@kel tmp]# cat result.txt
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_req=1 ttl=64 time=0.077 ms
--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.077/0.077/0.077/0.000 ms
PING 127.0.0.2 (127.0.0.2) 56(84) bytes of data.
64 bytes from 127.0.0.2: icmp_req=1 ttl=64 time=0.029 ms
--- 127.0.0.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.029/0.029/0.029/0.000 ms
PING 127.0.0.3 (127.0.0.3) 56(84) bytes of data.
64 bytes from 127.0.0.3: icmp_req=1 ttl=64 time=0.053 ms
--- 127.0.0.3 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.053/0.053/0.053/0.000 ms
PING 127.0.0.4 (127.0.0.4) 56(84) bytes of data.
64 bytes from 127.0.0.4: icmp_req=1 ttl=64 time=0.030 ms
--- 127.0.0.4 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.030/0.030/0.030/0.000 ms
PING 127.0.0.5 (127.0.0.5) 56(84) bytes of data.
64 bytes from 127.0.0.5: icmp_req=1 ttl=64 time=0.029 ms
--- 127.0.0.5 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.029/0.029/0.029/0.000 ms
PING 127.0.0.6 (127.0.0.6) 56(84) bytes of data.
64 bytes from 127.0.0.6: icmp_req=1 ttl=64 time=0.032 ms
--- 127.0.0.6 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.032/0.032/0.032/0.000 ms
PING 127.0.0.7 (127.0.0.7) 56(84) bytes of data.
64 bytes from 127.0.0.7: icmp_req=1 ttl=64 time=0.050 ms
--- 127.0.0.7 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.050/0.050/0.050/0.000 ms
PING 127.0.0.8 (127.0.0.8) 56(84) bytes of data.
64 bytes from 127.0.0.8: icmp_req=1 ttl=64 time=0.041 ms
--- 127.0.0.8 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.041/0.041/0.041/0.000 ms
PING 127.0.0.9 (127.0.0.9) 56(84) bytes of data.
64 bytes from 127.0.0.9: icmp_req=1 ttl=64 time=0.034 ms
--- 127.0.0.9 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.034/0.034/0.034/0.000 ms
PING 127.0.0.10 (127.0.0.10) 56(84) bytes of data.
64 bytes from 127.0.0.10: icmp_req=1 ttl=64 time=0.027 ms
--- 127.0.0.10 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.027/0.027/0.027/0.000 ms
PING 127.0.0.11 (127.0.0.11) 56(84) bytes of data.
64 bytes from 127.0.0.11: icmp_req=1 ttl=64 time=0.026 ms
--- 127.0.0.11 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.026/0.026/0.026/0.000 ms
PING 127.0.0.12 (127.0.0.12) 56(84) bytes of data.
64 bytes from 127.0.0.12: icmp_req=1 ttl=64 time=0.056 ms
--- 127.0.0.12 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.056/0.056/0.056/0.000 ms
PING 127.0.0.13 (127.0.0.13) 56(84) bytes of data.
64 bytes from 127.0.0.13: icmp_req=1 ttl=64 time=0.027 ms
--- 127.0.0.13 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.027/0.027/0.027/0.000 ms
PING 127.0.0.14 (127.0.0.14) 56(84) bytes of data.
64 bytes from 127.0.0.14: icmp_req=1 ttl=64 time=0.030 ms
--- 127.0.0.14 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.030/0.030/0.030/0.000 ms
PING 127.0.0.15 (127.0.0.15) 56(84) bytes of data.
64 bytes from 127.0.0.15: icmp_req=1 ttl=64 time=0.026 ms
--- 127.0.0.15 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.026/0.026/0.026/0.000 ms
PING 127.0.0.16 (127.0.0.16) 56(84) bytes of data.
64 bytes from 127.0.0.16: icmp_req=1 ttl=64 time=0.047 ms
--- 127.0.0.16 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.047/0.047/0.047/0.000 ms
PING 127.0.0.17 (127.0.0.17) 56(84) bytes of data.
64 bytes from 127.0.0.17: icmp_req=1 ttl=64 time=0.033 ms
--- 127.0.0.17 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.033/0.033/0.033/0.000 ms
PING 127.0.0.18 (127.0.0.18) 56(84) bytes of data.
64 bytes from 127.0.0.18: icmp_req=1 ttl=64 time=0.025 ms
--- 127.0.0.18 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.025/0.025/0.025/0.000 ms
PING 127.0.0.19 (127.0.0.19) 56(84) bytes of data.
64 bytes from 127.0.0.19: icmp_req=1 ttl=64 time=0.033 ms
--- 127.0.0.19 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.033/0.033/0.033/0.000 ms
PING 127.0.0.20 (127.0.0.20) 56(84) bytes of data.
64 bytes from 127.0.0.20: icmp_req=1 ttl=64 time=0.026 ms
--- 127.0.0.20 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.026/0.026/0.026/0.000 ms
PING 127.0.0.21 (127.0.0.21) 56(84) bytes of data.
64 bytes from 127.0.0.21: icmp_req=1 ttl=64 time=0.027 ms
--- 127.0.0.21 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.027/0.027/0.027/0.000 ms
PING 127.0.0.22 (127.0.0.22) 56(84) bytes of data.
64 bytes from 127.0.0.22: icmp_req=1 ttl=64 time=0.026 ms
--- 127.0.0.22 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.026/0.026/0.026/0.000 ms
PING 127.0.0.23 (127.0.0.23) 56(84) bytes of data.
64 bytes from 127.0.0.23: icmp_req=1 ttl=64 time=0.027 ms
--- 127.0.0.23 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.027/0.027/0.027/0.000 ms
PING 127.0.0.24 (127.0.0.24) 56(84) bytes of data.
64 bytes from 127.0.0.24: icmp_req=1 ttl=64 time=0.060 ms
--- 127.0.0.24 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.060/0.060/0.060/0.000 ms
PING 127.0.0.25 (127.0.0.25) 56(84) bytes of data.
64 bytes from 127.0.0.25: icmp_req=1 ttl=64 time=0.034 ms
--- 127.0.0.25 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.034/0.034/0.034/0.000 ms
PING 127.0.0.26 (127.0.0.26) 56(84) bytes of data.
64 bytes from 127.0.0.26: icmp_req=1 ttl=64 time=0.029 ms
--- 127.0.0.26 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.029/0.029/0.029/0.000 ms
PING 127.0.0.27 (127.0.0.27) 56(84) bytes of data.
64 bytes from 127.0.0.27: icmp_req=1 ttl=64 time=0.032 ms
--- 127.0.0.27 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.032/0.032/0.032/0.000 ms
PING 127.0.0.28 (127.0.0.28) 56(84) bytes of data.
64 bytes from 127.0.0.28: icmp_req=1 ttl=64 time=0.031 ms
--- 127.0.0.28 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.031/0.031/0.031/0.000 ms
PING 127.0.0.29 (127.0.0.29) 56(84) bytes of data.
64 bytes from 127.0.0.29: icmp_req=1 ttl=64 time=0.028 ms
--- 127.0.0.29 ping statistics ---
后面省略
2012-08-19 16:39
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:9 
像 11楼 那样,把它们放到后台慢慢 ping 去就行了,相当于是同时开了多个 ping。
没想过这个问题,感觉 ping 一般不是用来干这个的。另外那几个程序我也没研究过。学习了。
2012-08-22 01:53
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
以下是引用pangding在2012-8-22 02:53:22的发言:

像 11楼 那样,把它们放到后台慢慢 ping 去就行了,相当于是同时开了多个 ping。
没想过这个问题,感觉 ping 一般不是用来干这个的。另外那几个程序我也没研究过。学习了。

这里面应该有太多编程方面的细节要理解了,

感觉这个问题问的太早了,

可能自己都不知道这个问题能牵扯出多大一箩筐问题出来

比如用raw socket自己组装packet

怎样并行发送包之类吧

The quieter you become, the more you can hear
2012-08-22 07:22
iFreeBSD
Rank: 4
等 级:业余侠客
威 望:4
帖 子:474
专家分:236
注 册:2007-11-5
收藏
得分:0 
这么个东西用C多累呀。
直接Perl

#!/usr/bin/perl

use warnings ;
use strict   ;
use Net::Ping::External qw(ping) ;
use threads ;

sub Ping
{
    my $ip = shift  ;
    my $packsize = shift ;

    my $alive = 1 ;
    $alive = ping(host=>$ip , size=>$packsize) ;
    exit unless $alive ;
    print "$ip is alive\n" ;   
}

undef my @threadPool ;
undef my @ips ;

@ips = <> ;
chomp @ips ;

for (my $i = 0  ; $i < @ips ; $i++)
{
    $_ = threads->create(\&Ping , $ips[$i] , 256 ) ;
    select(undef , undef ,undef , 0.2) ;
    push @threadPool , $_ ;
}

foreach my $idx (@threadPool)
{        
    $idx->join() if ($idx->is_joinable) ;
    push @threadPool , $idx ;   
}


看看我写得这个Perl快不快,直接多线程解决并发。

[ 本帖最后由 iFreeBSD 于 2012-8-22 23:57 编辑 ]
收到的鲜花
  • madfrogme2012-08-23 06:57 送鲜花  6朵  

without further ado, let’s get started
2012-08-22 23:43
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:0 
回复 14楼 iFreeBSD
膜拜楼上。
2012-08-23 02:10
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
以下是引用iFreeBSD在2012-8-23 00:43:54的发言:

这么个东西用C多累呀。
直接Perl



看看我写得这个Perl快不快,直接多线程解决并发。

大牛明白我想问什么,认真阅读代码,非常感谢

The quieter you become, the more you can hear
2012-08-23 06:41
快速回复:请教怎样快速扫描局域网(C 语言版)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040652 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved