python数据包的作用_使用python探查网络数据包

#!/usr/bin/env python

import struct

import sys,os

import socket

import binascii

rawSocket=socket.socket(socket.PF_PACKET,socket.SOCK_RAW,socket.htons(0x0800))

#ifconfig eth0 promisc up

receivedPacket=rawSocket.recv(2048)

#Ethernet Header...

ethernetHeader=receivedPacket[0:14]

ethrheader=struct.unpack("!6s6s2s",ethernetHeader)

destinationIP= binascii.hexlify(ethrheader[0])

sourceIP= binascii.hexlify(ethrheader[1])

protocol= binascii.hexlify(ethrheader[2])

print "Destination: " + destinationIP

print "Source: " + sourceIP

print "Protocol: "+ protocol

#IP Header...

ipHeader=receivedPacket[14:34]

ipHdr=struct.unpack("!12s4s4s",ipHeader)

destinationIP=socket.inet_ntoa(ipHdr[2])

sourceIP=socket.inet_ntoa(ipHdr[1])

print "Source IP: " +sourceIP

print "Destination IP: "+destinationIP

#TCP Header...

tcpHeader=receivedPacket[34:54]

tcpHdr=struct.unpack("!2s2s16s",tcpHeader)

sourcePort=socket.inet_ntoa(tcpHdr[0])

destinationPort=socket.inet_ntoa(tcpHdr[1])

print "Source Port: " + sourcePort

print "Destination Port: " + destinationPort

我正在尝试捕获数据包,以及MAC添加的端口和ip地址。但我得到了这个错误。

查找端口地址时出错

在TCP头段中

有一个错误

^{pr2}$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值