Advertisement
Guest User

Wol.py

a guest
Mar 23rd, 2013
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # wol.py
  3.  
  4. import socket
  5. import struct
  6.  
  7. def wake_on_lan(macaddress):
  8.     """ Switches on remote computers using WOL. """
  9.  
  10.     # Check macaddress format and try to compensate.
  11.     if len(macaddress) == 12:
  12.         pass
  13.     elif len(macaddress) == 12 + 5:
  14.         sep = macaddress[2]
  15.         macaddress = macaddress.replace(sep, '')
  16.     else:
  17.         raise ValueError('Incorrect MAC address format')
  18.  
  19.     # Pad the synchronization stream.
  20.     data = ''.join(['FFFFFFFFFFFF', macaddress * 20])
  21.     send_data = ''
  22.  
  23.     # Split up the hex values and pack.
  24.     for i in range(0, len(data), 2):
  25.         send_data = ''.join([send_data,
  26.                              struct.pack('B', int(data[i: i + 2], 16))])
  27.  
  28.     # Broadcast it to the LAN.
  29.     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  30.     sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
  31.     sock.sendto(send_data, ('<broadcast>', 7))
  32.    
  33.  
  34. if __name__ == '__main__':
  35.     # Use macaddresses with any seperators.
  36.     wake_on_lan('0F:0F:DF:0F:BF:EF')
  37.     wake_on_lan('0F-0F-DF-0F-BF-EF')
  38.     # or without any seperators.
  39.     wake_on_lan('0F0FDF0FBFEF')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement