Created
November 27, 2008 04:51
-
-
Save yoshimov/29683 to your computer and use it in GitHub Desktop.
Send Wake On LAN Magic Packet from Groovy script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Send Wake On LAN Magic Packet | |
def int PORT = 9 | |
def cli = new CliBuilder() | |
cli.h(longOpt: 'help', 'usage information') | |
cli.i(argName: 'networkInterface', longOpt: 'interface', args: 1, required: true, 'MAC address of network interface(IPv4)') | |
cli.b(argName: 'broadcast', longOpt: 'broadcast', args: 1, required: true, 'Broadcast address') | |
def opt = cli.parse(args) | |
if (!opt) return | |
if (opt.h) cli.usage() | |
// Read MAC address | |
String mac = opt.i.replaceAll("-", "").replaceAll(":", "") | |
byte[] macBytes = new byte[6] | |
for (pos in 0..5) { | |
macBytes[pos] = Integer.valueOf(mac.substring(pos*2,pos*2+2), 16) | |
} | |
// Construct packet data | |
byte[] data = new byte[6 + 16 * 6] | |
for (i in 0..5) { | |
data[i] = 0xff | |
} | |
for (i in 1..16) { | |
System.arraycopy(macBytes, 0, data, i * 6, 6) | |
} | |
// Send packet | |
InetAddress address = InetAddress.getByName(opt.b) | |
DatagramPacket packet = new DatagramPacket(data, data.length, address, PORT) | |
DatagramSocket socket = new DatagramSocket() | |
socket.send(packet) | |
socket.close() | |
println "Wake-on-LAN packet sent." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment