Sending a broadcast message using raw sockets

P

Peter Steele

I want to write a program in Python that sends a broadcast message using raw sockets. The system where this program will run has no IP or default route defined, hence the reason I need to use a broadcast message.

I've done some searches and found some bits and pieces about using raw sockets in Python, but I haven't been able to find an example that explains how to construct a broadcast message using raw sockets.

Any pointers would be appreciated.
 
R

Rob Williscroft

Peter Steele wrote in
in
comp.lang.python:
I want to write a program in Python that sends a broadcast message
using raw sockets. The system where this program will run has no IP or
default route defined, hence the reason I need to use a broadcast
message.

I've done some searches and found some bits and pieces about using raw
sockets in Python, but I haven't been able to find an example that
explains how to construct a broadcast message using raw sockets.

Any pointers would be appreciated.

This is part of my Wake-On-Lan script:

def WOL_by_mac( mac, ip = '<broadcast>', port = 9 ):
import struct, socket

a = mac.replace( ':', '-' ).split( '-' )
addr = struct.pack( 'B'*6, *[ int(_, 16) for _ in a ] )
msg = b'\xff' * 6 + addr * 16

s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
s.setsockopt( socket.SOL_SOCKET, socket.SO_BROADCAST, 1 )
s.sendto( msg, ( ip, port ) )
s.close()


The mac address is 6 pairs of hex digits seperated by '-' or ':'.

http://en.wikipedia.org/wiki/Wake-on-LAN



Rob.
 
P

Peter Steele

Peter Steele wrote in

in

comp.lang.python:
I want to write a program in Python that sends a broadcast message
using raw sockets. The system where this program will run has no IP or
default route defined, hence the reason I need to use a broadcast
message.

I've done some searches and found some bits and pieces about using raw
sockets in Python, but I haven't been able to find an example that
explains how to construct a broadcast message using raw sockets.

Any pointers would be appreciated.

This is part of my Wake-On-Lan script:

def WOL_by_mac( mac, ip = '<broadcast>', port = 9 ):

import struct, socket

a = mac.replace( ':', '-' ).split( '-' )
addr = struct.pack( 'B'*6, *[ int(_, 16) for _ in a ] )
msg = b'\xff' * 6 + addr * 16

s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
s.setsockopt( socket.SOL_SOCKET, socket.SO_BROADCAST, 1 )
s.sendto( msg, ( ip, port ) )
s.close()

The mac address is 6 pairs of hex digits seperated by '-' or ':'.

Thanks for the code sample. Does this code work if the box has no IP or default route assigned? I'm away from the office at the moment so I can't test this.
 
R

Rob Williscroft

Peter Steele wrote in
in
comp.lang.python:
Peter Steele wrote in

in

comp.lang.python:
[snip]
This is part of my Wake-On-Lan script:

def WOL_by_mac( mac, ip = '<broadcast>', port = 9 ):
[snip]

Thanks for the code sample. Does this code work if the box has no IP
or default route assigned? I'm away from the office at the moment so I
can't test this.

No idea, but the sockets system must be up and running before the
card (interface) has an IP (otherwise how would it ever get assigned)
and I presume DHCP works in a similar manner.

However the "route assignemt" is irrelevent, broadcast messages never
get routed.

Rob
--
 
P

Peter Steele

I just tried running you code, and the "sendto" call fails with "Network isunreachable". That's what I expected, based on other tests I've done. That's why I was asking about how to do raw sockets, since tools like dhclient use raw sockets to do what they do. It can clearly be duplicated in Python,I just need to find some code samples on how to construct a raw packet.
 
P

Peter Steele

In fact, I have used scapy in the past, but I am working in a restricted environment and don't have this package available. It provides tones more than I really need anyway, and I figured a simple raw socket send/receive can't be *that* hard. I may have to reverse engineer some C code, such as dhclient...
 
P

Peter Steele

In fact, I have used scapy in the past, but I am working in a restricted environment and don't have this package available. It provides tones more than I really need anyway, and I figured a simple raw socket send/receive can't be *that* hard. I may have to reverse engineer some C code, such as dhclient...
 
C

Chris Angelico

In fact, I have used scapy in the past, but I am working in a restricted environment and don't have this package available. It provides tones more than I really need anyway, and I figured a simple raw socket send/receive can't be *that* hard. I may have to reverse engineer some C code, such as dhclient...

Yeah, I think you're working with something fairly esoteric there -
bypassing the lower tiers of support (routing etc). Chances are you
won't find any good Python examples, and C's all you'll have. Are you
reasonably familiar with C?

Point to note: Raw sockets *may* require special privileges. Some
systems require that only root employ them, for security's sake.

ChrisA
 
P

Peter Steele

Actually, I used to teach C, so yeah, I know it pretty well. :)

Scapy is a possibility, I just need to add it to my environment (which doesn't have a C compiler). I can jury rig something though.
 
P

Peter Steele

Actually, I used to teach C, so yeah, I know it pretty well. :)

Scapy is a possibility, I just need to add it to my environment (which doesn't have a C compiler). I can jury rig something though.
 
R

Rob Williscroft

Peter Steele wrote in
in
comp.lang.python:
I just tried running you code, and the "sendto" call fails with
"Network is unreachable". That's what I expected, based on other tests
I've done. That's why I was asking about how to do raw sockets, since
tools like dhclient use raw sockets to do what they do. It can clearly
be duplicated in Python, I just need to find some code samples on how
to construct a raw packet.

Try
s = socket.socket( socket.AF_INET, socket.SOCK_RAW )

I tried this on windows and it needed admin privaleges to
run.

Rob.
--
 
P

peter

Yes, raw sockets need admin privileges, I knew that. The app I'm writing runs as root so that's not a problem. It runs during the %pre script stage of a kickstart controlled install.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top