Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37062 > unrolled thread

Sending a broadcast message using raw sockets

Started byPeter Steele <pwsteele@gmail.com>
First post2013-01-18 23:32 -0800
Last post2013-01-22 17:21 -0800
Articles 13 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Sending a broadcast message using raw sockets Peter Steele <pwsteele@gmail.com> - 2013-01-18 23:32 -0800
    Re: Sending a broadcast message using raw sockets Rob Williscroft <rtw@rtw.me.uk> - 2013-01-21 09:10 +0000
      Re: Sending a broadcast message using raw sockets Peter Steele <pwsteele@gmail.com> - 2013-01-21 17:06 -0800
        Re: Sending a broadcast message using raw sockets Rob Williscroft <rtw@rtw.me.uk> - 2013-01-22 09:06 +0000
          Re: Sending a broadcast message using raw sockets Peter Steele <pwsteele@gmail.com> - 2013-01-22 07:11 -0800
            Re: Sending a broadcast message using raw sockets Corey LeBleu <coreylebleu@gmail.com> - 2013-01-22 10:07 -0600
              Re: Sending a broadcast message using raw sockets Peter Steele <pwsteele@gmail.com> - 2013-01-22 09:57 -0800
                Re: Sending a broadcast message using raw sockets Chris Angelico <rosuav@gmail.com> - 2013-01-23 08:19 +1100
                  Re: Sending a broadcast message using raw sockets Peter Steele <pwsteele@gmail.com> - 2013-01-22 13:36 -0800
                  Re: Sending a broadcast message using raw sockets Peter Steele <pwsteele@gmail.com> - 2013-01-22 13:36 -0800
              Re: Sending a broadcast message using raw sockets Peter Steele <pwsteele@gmail.com> - 2013-01-22 09:57 -0800
            Re: Sending a broadcast message using raw sockets Rob Williscroft <rtw@rtw.me.uk> - 2013-01-22 21:58 +0000
              Re: Sending a broadcast message using raw sockets peter@peaxy.net - 2013-01-22 17:21 -0800

#37062 — Sending a broadcast message using raw sockets

FromPeter Steele <pwsteele@gmail.com>
Date2013-01-18 23:32 -0800
SubjectSending a broadcast message using raw sockets
Message-ID<f37ccb35-8439-42cd-a063-962249b44903@googlegroups.com>
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.

[toc] | [next] | [standalone]


#37171

FromRob Williscroft <rtw@rtw.me.uk>
Date2013-01-21 09:10 +0000
Message-ID<XnsA14F5D5D4670FrtwfreenetREMOVEcouk@88.198.244.100>
In reply to#37062
Peter Steele wrote in
news:f37ccb35-8439-42cd-a063-962249b44903@googlegroups.com 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.

[toc] | [prev] | [next] | [standalone]


#37238

FromPeter Steele <pwsteele@gmail.com>
Date2013-01-21 17:06 -0800
Message-ID<0c2b3482-df46-4324-8bf9-2c45d3f6b516@googlegroups.com>
In reply to#37171
On Monday, January 21, 2013 1:10:06 AM UTC-8, Rob Williscroft wrote:
> Peter Steele wrote in
> 
> news:f37ccb35-8439-42cd-a063-962249b44903@googlegroups.com 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.

[toc] | [prev] | [next] | [standalone]


#37257

FromRob Williscroft <rtw@rtw.me.uk>
Date2013-01-22 09:06 +0000
Message-ID<XnsA1505CBA6AF48rtwfreenetREMOVEcouk@88.198.244.100>
In reply to#37238
Peter Steele wrote in
news:0c2b3482-df46-4324-8bf9-2c45d3f6b516@googlegroups.com in
comp.lang.python: 

> On Monday, January 21, 2013 1:10:06 AM UTC-8, Rob Williscroft wrote:
>> Peter Steele wrote in
>> 
>> news:f37ccb35-8439-42cd-a063-962249b44903@googlegroups.com in
>> 
>> comp.lang.python: 
>> 
>> > I want to write a program in Python that sends a broadcast message

[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
-- 

[toc] | [prev] | [next] | [standalone]


#37281

FromPeter Steele <pwsteele@gmail.com>
Date2013-01-22 07:11 -0800
Message-ID<96947c45-f16b-4e97-b055-edc1241ee4a1@googlegroups.com>
In reply to#37257
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.

[toc] | [prev] | [next] | [standalone]


#37306

FromCorey LeBleu <coreylebleu@gmail.com>
Date2013-01-22 10:07 -0600
Message-ID<mailman.809.1358874138.2939.python-list@python.org>
In reply to#37281

[Multipart message — attachments visible in raw view] — view raw

If you don't *have* to use the actual socket library, you might want to
have a look at scapy.  It's a packet manipulation program/library. It might
make things a little easier.

http://www.secdev.org/projects/scapy/

 On Jan 22, 2013 9:17 AM, "Peter Steele" <pwsteele@gmail.com> wrote:

> 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.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [next] | [standalone]


#37317

FromPeter Steele <pwsteele@gmail.com>
Date2013-01-22 09:57 -0800
Message-ID<28c05d48-1af1-46db-a316-69d29089d487@googlegroups.com>
In reply to#37306
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...

On Tuesday, January 22, 2013 8:07:12 AM UTC-8, Corey LeBleu wrote:
> If you don't *have* to use the actual socket library, you might want to have a look at scapy.  It's a packet manipulation program/library. It might make things a little easier.
> 
> http://www.secdev.org/projects/scapy/
> 
>  
> 
> On Jan 22, 2013 9:17 AM, "Peter Steele" <pwst...@gmail.com> wrote:
> 
> 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.
> 
> 
> 
> 
> --
> 
> http://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#37350

FromChris Angelico <rosuav@gmail.com>
Date2013-01-23 08:19 +1100
Message-ID<mailman.838.1358889562.2939.python-list@python.org>
In reply to#37317
On Wed, Jan 23, 2013 at 4:57 AM, Peter Steele <pwsteele@gmail.com> wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#37351

FromPeter Steele <pwsteele@gmail.com>
Date2013-01-22 13:36 -0800
Message-ID<662fc299-e362-4720-85d2-9ab07addd8d7@googlegroups.com>
In reply to#37350
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.

On Tuesday, January 22, 2013 1:19:14 PM UTC-8, Chris Angelico wrote:
> On Wed, Jan 23, 2013 at 4:57 AM, Peter Steele <pwsteele@gmail.com> wrote:
> 
> > 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

[toc] | [prev] | [next] | [standalone]


#37353

FromPeter Steele <pwsteele@gmail.com>
Date2013-01-22 13:36 -0800
Message-ID<mailman.840.1358891237.2939.python-list@python.org>
In reply to#37350
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.

On Tuesday, January 22, 2013 1:19:14 PM UTC-8, Chris Angelico wrote:
> On Wed, Jan 23, 2013 at 4:57 AM, Peter Steele <pwsteele@gmail.com> wrote:
> 
> > 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

[toc] | [prev] | [next] | [standalone]


#37319

FromPeter Steele <pwsteele@gmail.com>
Date2013-01-22 09:57 -0800
Message-ID<mailman.816.1358878129.2939.python-list@python.org>
In reply to#37306
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...

On Tuesday, January 22, 2013 8:07:12 AM UTC-8, Corey LeBleu wrote:
> If you don't *have* to use the actual socket library, you might want to have a look at scapy.  It's a packet manipulation program/library. It might make things a little easier.
> 
> http://www.secdev.org/projects/scapy/
> 
>  
> 
> On Jan 22, 2013 9:17 AM, "Peter Steele" <pwst...@gmail.com> wrote:
> 
> 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.
> 
> 
> 
> 
> --
> 
> http://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#37354

FromRob Williscroft <rtw@rtw.me.uk>
Date2013-01-22 21:58 +0000
Message-ID<XnsA150DF93DEAF5rtwfreenetREMOVEcouk@88.198.244.100>
In reply to#37281
Peter Steele wrote in
news:96947c45-f16b-4e97-b055-edc1241ee4a1@googlegroups.com 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.
-- 

[toc] | [prev] | [next] | [standalone]


#37390

Frompeter@peaxy.net
Date2013-01-22 17:21 -0800
Message-ID<c4aba5e9-0879-45c6-b7cd-3451da0a08c5@googlegroups.com>
In reply to#37354
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.

On Tuesday, January 22, 2013 1:58:07 PM UTC-8, Rob Williscroft wrote:
> Try 
> 
>     	s = socket.socket( socket.AF_INET, socket.SOCK_RAW )
> 
> 
> 
> I tried this on windows and it needed admin privaleges to
> 
> run.
> 
> 
> 
> Rob.
> 
> --

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web