python: using raw socket with OSX -
i found code online , found doesn't work on osx. know correct method without using third party library?
import socket import struct import binascii rawsocket = socket.socket(socket.af_packet, socket.sock_raw, socket.htons(0x0003)) while true: packet = rawsocket.recvfrom(2048) ethernet_header = packet[0][0:14] ethernet_detailed = struct.unpack(“!6s6s2s”, ethernet_header) arp_header = packet[0][14:42] arp_detailed = struct.unpack(“2s2s1s1s2s6s4s6s4s”, arp_header) # skip non-arp packets ethertype = ethernet_detailed[2] if ethertype != ‘\x08\x06’: continue source_mac = binascii.hexlify(arp_detailed[5]) dest_ip = socket.inet_ntoa(arp_detailed[8]) if source_mac == ‘74c24671971c’: print “tide button pressed!, ip = “ + dest_ip
apparantly osx not have af_packet or pf_packet , af_inet high level think, or @ least requires more recoding drop in replacement.
thanks
ok figured 1 out, on mac have use pcap library. here code came with.
#!/usr/bin/env python2.7 import sys, binascii, subprocess import dpkt, pcap, socket cottonelle = 'f0272d8b52c0' def main(): name = pcap.lookupdev() try: pc = pcap.pcap(name) except: print pc.geterr() try: print 'listening on %s' % (pc.name) ts, pkt in pc: eth = dpkt.ethernet.ethernet(pkt) ip_hdr = eth.data if eth.type != dpkt.ethernet.eth_type_arp: continue if binascii.hexlify(eth.src) == cottonelle: subprocess.call("/usr/local/bin/stopsim", shell=true) except exception e: print e, pc.geterr() if __name__ == '__main__': main()
Comments
Post a Comment