免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2613 | 回复: 9

我又来请教了(NET::Pcap模块抓包的问题) [复制链接]

论坛徽章:
0
发表于 2009-02-12 11:19 |显示全部楼层
[table=95%][tr][td][font=FixedSys][color=#000000]
[color=#0000FF]use[/color] Net[color=#0000CC]:[/color][color=#0000CC]:[/color]Pcap[color=#0000CC];[/color]
[color=#0000FF]use[/color] strict[color=#0000CC];[/color]
[color=#0000FF]use[/color] [color=#FF0000]Socket[/color][color=#0000CC];[/color]


[color=#0000FF]sub[/color] got_a_dev[color=#0000CC]{[/color]
 [color=#0000FF]my[/color] [color=#0000FF]$[/color][color=#008080]err[/color] [color=#0000CC]=[/color] [color=#FF00FF]''[/color][color=#0000CC];[/color]
 [color=#0000FF]my[/color] [color=#0000FF]$[/color][color=#008080]net[/color] [color=#0000CC]=[/color] [color=#FF00FF]''[/color][color=#0000CC];[/color]
 [color=#0000FF]my[/color] [color=#0000FF]$[/color][color=#008080]mask[/color] [color=#0000CC]=[/color] [color=#FF00FF]''[/color][color=#0000CC];[/color]
 [color=#0000FF]my[/color] [color=#0000FF]%[/color][color=#800000]devinfo[/color][color=#0000CC];[/color]
 [color=#0000FF]my[/color] [color=#0000FF]$[/color][color=#008080]good_dev[/color][color=#0000CC]=[/color][color=#FF00FF]''[/color][color=#0000CC];[/color]

 [color=#FF9900]#获得所有的设备
[/color]
 [color=#0000FF]my[/color] [color=#0000FF]@[/color][color=#808000]devs[/color] [color=#0000CC]=[/color] Net[color=#0000CC]:[/color][color=#0000CC]:[/color]Pcap[color=#0000CC]:[/color][color=#0000CC]:[/color]pcap_findalldevs[color=#0000CC]([/color][color=#0000CC]\[/color][color=#0000FF]%[/color][color=#800000]devinfo[/color][color=#0000CC],[/color] \[color=#0000FF]$[/color][color=#008080]err[/color][color=#0000CC])[/color][color=#0000CC];[/color]


 [color=#FF0000]print[/color] [color=#FF00FF]"The number of devices: "[/color][color=#0000CC],[/color][color=#0000FF]$[/color][color=#FF9900]#devs+1,"\n";
[/color]

 [color=#0000FF]for[/color] [color=#0000FF]my[/color] [color=#0000FF]$[/color][color=#008080]dev[/color] [color=#0000CC]([/color][color=#0000FF]@[/color][color=#808000]devs[/color][color=#0000CC])[/color] [color=#0000CC]{[/color]
 

 [color=#FF9900]#Net::Pcap::lookupnet function for finding which network is attached to a particular device.
[/color]
 [color=#0000FF]if[/color] [color=#0000CC]([/color] [color=#0000CC]([/color]Net[color=#0000CC]:[/color][color=#0000CC]:[/color]Pcap[color=#0000CC]:[/color][color=#0000CC]:[/color]lookupnet[color=#0000CC]([/color][color=#0000FF]$[/color][color=#008080]dev[/color][color=#0000CC],[/color] \[color=#0000FF]$[/color][color=#008080]net[/color][color=#0000CC],[/color] \[color=#0000FF]$[/color][color=#008080]mask[/color][color=#0000CC],[/color] \[color=#0000FF]$[/color][color=#008080]err[/color][color=#0000CC])[/color] [color=#0000CC])[/color] [color=#0000CC]=[/color][color=#0000CC]=[/color] [color=#0000CC]-[/color]1 [color=#0000CC])[/color] [color=#0000CC]{[/color]
   [color=#FF0000]die[/color] [color=#FF00FF]"Net::Pcap::lookupnet failed. Error was $err"[/color][color=#0000CC];[/color]
 [color=#0000CC]}[/color]
 [color=#0000FF]else[/color][color=#0000CC]{[/color]
 [color=#0000FF]$[/color][color=#008080]net[/color] [color=#0000CC]=[/color] inet_ntoa[color=#0000CC]([/color][color=#FF0000]pack[/color] [color=#FF00FF]"N"[/color][color=#0000CC],[/color] [color=#0000FF]$[/color][color=#008080]net[/color][color=#0000CC])[/color][color=#0000CC];[/color] [color=#FF9900]# 2281480192 --> 135.252.160.0
[/color]
 [color=#0000FF]$[/color][color=#008080]mask[/color] [color=#0000CC]=[/color] inet_ntoa[color=#0000CC]([/color][color=#FF0000]pack[/color] [color=#FF00FF]"N"[/color][color=#0000CC],[/color] [color=#0000FF]$[/color][color=#008080]mask[/color][color=#0000CC])[/color][color=#0000CC];[/color] [color=#FF9900]# 4294966784 --> 255.255.254.0
[/color]
 [color=#FF9900]#print "Net:$net\nMask:$mask\n";
[/color]
 
  [color=#0000CC]}[/color]
  [color=#0000FF]if[/color] [color=#0000CC]([/color][color=#0000FF]$[/color][color=#008080]devinfo[/color][color=#0000CC]{[/color][color=#0000FF]$[/color][color=#008080]dev[/color][color=#0000CC]}[/color] [color=#0000CC]=[/color][color=#0000CC]~[/color][color=#0000CC]/[/color]Intel[color=#0000CC]/[/color][color=#0000CC])[/color][color=#0000CC]{[/color]
 [color=#0000FF]$[/color][color=#008080]good_dev[/color][color=#0000CC]=[/color][color=#0000FF]$[/color][color=#008080]dev[/color][color=#0000CC];[/color]
 [color=#0000FF]return[/color] [color=#0000FF]$[/color][color=#008080]good_dev[/color][color=#0000CC];[/color]
 [color=#0000CC]}[/color]
[color=#FF9900]# print "show : $dev : $devinfo{$dev}\n";
[/color]


  [color=#0000CC]}[/color]


[color=#0000CC]}[/color]

[color=#0000FF]my[/color] [color=#0000FF]$[/color][color=#008080]interface[/color][color=#0000CC]=[/color][color=#0000CC]&[/color]got_a_dev[color=#0000CC]([/color][color=#0000CC])[/color][color=#0000CC];[/color]
[color=#FF0000]print[/color] [color=#FF00FF]"get:"[/color][color=#0000CC].[/color][color=#0000FF]$[/color][color=#008080]interface[/color][color=#0000CC].[/color][color=#FF00FF]"\n"[/color][color=#0000CC];[/color]

[color=#0000FF]my[/color] [color=#0000FF]$[/color][color=#008080]err[/color] [color=#0000CC]=[/color] [color=#FF00FF]''[/color][color=#0000CC];[/color]
[color=#0000FF]my[/color] [color=#0000FF]$[/color][color=#008080]pcap[/color] [color=#0000CC]=[/color] Net[color=#0000CC]:[/color][color=#0000CC]:[/color]Pcap[color=#0000CC]:[/color][color=#0000CC]:[/color]open_live[color=#0000CC]([/color]
                [color=#0000FF]$[/color][color=#008080]interface[/color][color=#0000CC],[/color]1600[color=#0000CC],[/color]1[color=#0000CC],[/color]0[color=#0000CC],[/color][color=#0000CC]\[/color][color=#0000FF]$[/color][color=#008080]err[/color]
   [color=#0000CC])[/color] [color=#0000FF]or[/color] [color=#FF0000]die[/color] [color=#FF00FF]"loop1: $!\n"[/color] [color=#0000CC];[/color]
   
   
Net[color=#0000CC]:[/color][color=#0000CC]:[/color]Pcap[color=#0000CC]:[/color][color=#0000CC]:[/color]loop[color=#0000CC]([/color][color=#0000FF]$[/color][color=#008080]pcap[/color][color=#0000CC],[/color] 10[color=#0000CC],[/color] \[color=#0000CC]&[/color]callback[color=#0000CC],[/color] [color=#FF00FF]"user_data"[/color][color=#0000CC])[/color] [color=#0000FF]or[/color] [color=#FF0000]die[/color] [color=#FF00FF]"loop2: $!\n"[/color][color=#0000CC];[/color]
  
[color=#0000FF]sub[/color] callback [color=#0000CC]{[/color]
            [color=#0000FF]my[/color] [color=#0000CC]([/color][color=#0000FF]$[/color][color=#008080]user_data[/color][color=#0000CC],[/color] [color=#0000FF]$[/color][color=#008080]header[/color][color=#0000CC],[/color] [color=#0000FF]$[/color][color=#008080]packet[/color][color=#0000CC])[/color] [color=#0000CC]=[/color] [color=#0000FF]@[/color][color=#808000]_[/color][color=#0000CC];[/color]
            [color=#FF0000]print[/color] [color=#0000FF]$[/color][color=#008080]header[/color][color=#0000CC].[/color][color=#FF00FF]"\n"[/color][color=#0000CC];[/color]
        [color=#0000CC]}[/color][/color][/font][/td][/tr][/table]



抓包的时候Net::Pcap::loop函数会提示一个bad file descriptor错误....在线等待大大,,,感谢~~哈哈

论坛徽章:
0
发表于 2009-02-12 11:20 |显示全部楼层

靠,,,什么东西..

我发过哈...晕倒



use Net:cap;
use strict;
use Socket;


sub got_a_dev{
my $err = '';
my $net = '';
my $mask = '';
my %devinfo;
my $good_dev='';

#获得所有的设备
my @devs = Net:cap::pcap_findalldevs(\%devinfo, \$err);


print "The number of devices: ",$#devs+1,"\n";

for my $dev (@devs) {


#Net:cap::lookupnet function for finding which network is attached to a particular device.
if ( (Net:cap::lookupnet($dev, \$net, \$mask, \$err) ) == -1 ) {
   die "Net:cap::lookupnet failed. Error was $err";
}
else{
$net = inet_ntoa(pack "N", $net); # 2281480192 --> 135.252.160.0
$mask = inet_ntoa(pack "N", $mask); # 4294966784 --> 255.255.254.0
#print "Netnet\nMaskmask\n";

  }
  if ($devinfo{$dev} =~/Intel/){
$good_dev=$dev;
return $good_dev;
}
# print "show : $dev : $devinfo{$dev}\n";


  }


}

my $interface=&got_a_dev();
print "get:".$interface."\n";

my $err = '';
my $pcap = Net:cap:pen_live(
                $interface,1600,1,0,\$err
   ) or die "loop1: $!\n" ;
   
   
Net:cap::loop($pcap, 10, \&callback, "user_data" or die "loop2: $!\n";
  
sub callback {
            my ($user_data, $header, $packet) = @_;
            print $header."\n";
        }

论坛徽章:
0
发表于 2009-02-12 11:39 |显示全部楼层
怎么还有笑脸,看着太费劲啊。

论坛徽章:
0
发表于 2009-02-12 11:53 |显示全部楼层

use Net::Pcap;
use strict;
use Socket;


sub got_a_dev{
my $err = '';
my $net = '';
my $mask = '';
my %devinfo;
my $good_dev='';

#获得所有的设备

my @devs = Net::Pcap::pcap_findalldevs(\%devinfo, \$err);


print "The number of devices: ",$#devs+1,"\n";


for my $dev (@devs) {


#Net::Pcap::lookupnet function for finding which network is attached to a particular device.

if ( (Net::Pcap::lookupnet($dev, \$net, \$mask, \$err) ) == -1 ) {
   die "Net::Pcap::lookupnet failed. Error was $err";
}
else{
$net = inet_ntoa(pack "N", $net); # 2281480192 --> 135.252.160.0

$mask = inet_ntoa(pack "N", $mask); # 4294966784 --> 255.255.254.0

#print "Net:$net\nMask:$mask\n";


  }
  if ($devinfo{$dev} =~/Intel/){
$good_dev=$dev;
return $good_dev;
}
# print "show : $dev : $devinfo{$dev}\n";



  }


}

my $interface=&got_a_dev();
print "get:".$interface."\n";

my $err = '';
my $pcap = Net::Pcap::open_live(
                $interface,1600,1,0,\$err
   ) or die "loop1: $!\n" ;
   
   
Net::Pcap::loop($pcap, 10, \&callback, "user_data") or die "loop2: $!\n";
  
sub callback {
            my ($user_data, $header, $packet) = @_;
            print $header."\n";
        }




抓包的时候Net::Pcap::loop函数会提示一个bad file descriptor错误....在线等待大大,,,感谢~~哈哈

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
发表于 2009-02-12 12:03 |显示全部楼层

回复 #4 cobrawgl 的帖子

好人。
我的Net::Pcap不赞成pcap_findalldevs

[ 本帖最后由 ynchnluiti 于 2009-2-12 12:56 编辑 ]

论坛徽章:
0
发表于 2009-02-12 13:18 |显示全部楼层
楼上的是说用pcap_lookupdev(\$err)吗?

论坛徽章:
0
发表于 2009-02-12 13:19 |显示全部楼层
我发过哈...晕倒



use Net:cap;
use strict;
use Socket;


sub got_a_dev{
my $err = '';
my $net = '';
my $mask = '';
my %devinfo;
my $good_dev='';

#获得所有的设备
my @devs = Net:cap::pcap_findalldevs(\%devinfo, \$err);


print "The number of devices: ",$#devs+1,"\n";

for my $dev (@devs) {


#Net:cap::lookupnet function for finding which network is attached to a particular device.
if ( (Net:cap::lookupnet($dev, \$net, \$mask, \$err) ) == -1 ) {
   die "Net:cap::lookupnet failed. Error was $err";
}
else{
$net = inet_ntoa(pack "N", $net); # 2281480192 --> 135.252.160.0
$mask = inet_ntoa(pack "N", $mask); # 4294966784 --> 255.255.254.0
#print "Netnet\nMaskmask\n";

  }
  if ($devinfo{$dev} =~/Intel/){
$good_dev=$dev;
return $good_dev;
}
# print "show : $dev : $devinfo{$dev}\n";


  }


}

my $interface=&got_a_dev();
print "get:".$interface."\n";

my $err = '';
my $pcap = Net:cap:pen_live(
                $interface,1600,1,0,\$err
   ) or die "loop1: $!\n" ;
   
   
Net:cap::loop($pcap, 10, \&callback, "user_data" or die "loop2: $!\n";
  
sub callback {
            my ($user_data, $header, $packet) = @_;
            print $header."\n";
        }

论坛徽章:
0
发表于 2009-02-12 13:20 |显示全部楼层
怎么搞成4楼那样有颜色啊?

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
发表于 2009-02-12 13:44 |显示全部楼层
原帖由 yangchaohi 于 2009-2-12 13:20 发表
怎么搞成4楼那样有颜色啊?


编辑帖子时,工具栏里有个图标(c++,鼠标放上去有提示)插入程序代码。弹出窗口:选择perl,贴上代码。

论坛徽章:
0
发表于 2009-02-12 16:57 |显示全部楼层
3q
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP