>> a=Ether()/ARP()
>> a.show()
###[ Ethernet ]###
dst= 00:50:56:ef:49:1f
src= 00:0c:29:e2:bb:15
type= 0x806
###[ ARP ]###
hwtype= 0x1
ptype= 0x800
hwlen= 6
plen= 4
op= who-has
hwsrc= 00:0c:29:e2:bb:15
psrc= 192.168.80.250
hwdst= 00:00:00:00:00:00
pdst= 0.0.0.0>>arp1=srp(Ether(src=‘00:0c:29:e2:bb:15‘,dst=‘FF:FF:FF:FF:FF:FF‘)/ARP(op=1,hwsrc=‘00:0c:29:e2:bb:15‘,hwdst=‘00:00:00:00:00:00‘,psrc=‘192.168.80.250‘,pdst=‘192.168.80.251‘,))
Begin emission:
*Finished to send 1 packets.
Received 1 packets, got 1 answers, remaining 0 packets
1)>>> print(arp1)
(<Results: TCP:0 UDP:0 ICMP:0 Other:1>, <Unanswered: TCP:0 UDP:0 ICMP:0 Other:0>)
产生收到响应与没收到响应元组;
2)查看arp1的数据类型:
>> print(type(arp1))
<type ‘tuple‘> 为元组
3)用元组的方法只打印接收报文:
>> print(arp1[0])
<Results: TCP:0 UDP:0 ICMP:0 Other:1>
4)显示arp1[0]数据类型:
>> print(type(arp1[0]))
<class ‘scapy.plist.SndRcvList‘>
5)查看该‘scapy.plist.SndRcvList数据类型处理方法(上网查看https://fossies.org/dox/scapy-2.3.3/)用res的方法
6)产生响应数据包中的发送与接收的包,并把包res方法列出来
>> print(arp1[0].res)*第一对发送包与收发包
[(<Ether dst=FF:FF:FF:FF:FF:FF src=00:0c:29:e2:bb:15 type=0x806 |<ARP op=who-has hwsrc=00:0c:29:e2:bb:15 psrc=192.168.80.250 hwdst=00:00:00:00:00:00 pdst=192.168.80.251 |>>, <Ether dst=00:0c:29:e2:bb:15 src=00:0c:29:21:fd:03 type=0x806 |<ARP hwtype=0x1 ptype=0x800 hwlen=6 plen=4 op=is-at hwsrc=00:0c:29:21:fd:03 psrc=192.168.80.251 hwdst=00:0c:29:e2:bb:15 pdst=192.168.80.250 |<Padding load=‘\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00‘ |>>>)]
7)提出收包数据
>> print(arp1[0].res[0][1])
8)查看arp1[0].res[0][1]数据类型:
>> print(type(arp1[0].res[0][1]))
<class ‘scapy.layers.l2.Ether‘>
9)查看该scapy.layers.l2.Ether数据类型处理方法(上网查看https://fossies.org/dox/scapy-2.3.3/)用
Static Public Attributes
string name = "Ethernet"
list fields_desc
的方法
10)用fields方法(产生相应得字典)提取收包数据:
print(arp1[0].res[0][1].fields)
{‘src‘: ‘00:0c:29:21:fd:03‘, ‘dst‘: ‘00:0c:29:e2:bb:15‘, ‘type‘: 2054}
11)用show方法读取收包数据:
print(arp1[0].res[0][1].show())
###[ Ethernet ]###
dst= 00:0c:29:e2:bb:15
src= 00:0c:29:21:fd:03
type= 0x806
###[ ARP ]###
hwtype= 0x1
ptype= 0x800
hwlen= 6
plen= 4
op= is-at
hwsrc= 00:0c:29:21:fd:03
psrc= 192.168.80.251
hwdst= 00:0c:29:e2:bb:15
pdst= 192.168.80.250
###[ Padding ]###
load= ‘\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00‘
12)用show方法读取收包数据中ARP:
>> print(arp1[0].res[0][1][1].show())
###[ ARP ]###
hwtype= 0x1
ptype= 0x800
hwlen= 6
plen= 4
op= is-at
hwsrc= 00:0c:29:21:fd:03
psrc= 192.168.80.251
hwdst= 00:0c:29:e2:bb:15
pdst= 192.168.80.250
###[ Padding ]###
load= ‘\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00‘
13)产生自己想要的print结果:
>> print(‘ip:‘ + arp1[0].res[0][1][1].fields[‘psrc‘]+ ‘ mac:‘ + arp1[0].res[0][1][1].fields[‘hwsrc‘])
ip:192.168.80.251 mac:00:0c:29:21:fd:03
14)数据结构:
原文地址:http://blog.51cto.com/baoer0717/2087221