forked from ksperis/libvirt-wakeonlan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibvirtwol.py
executable file
·160 lines (143 loc) · 5.32 KB
/
libvirtwol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#! /bin/sh
"true" '''\'
if command -v python2 > /dev/null; then
exec python2 "$0" "$@"
else
exec python "$0" "$@"
fi
exit $?
'''
# LibVirt Wake On Lan
# Copyright (C) 2012 Simon Cadman
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# dmacias - added fixes for ether proto 0x0842
import pcap
import sys
import socket
import struct
import string
import libvirt
import logging
from xml.dom import minidom
class LibVirtWakeOnLan:
@staticmethod
def StartServerByMACAddress(mac):
conn = libvirt.open(None)
if conn is None:
logging.error('Failed to open connection to the hypervisor')
sys.exit(1)
domains = conn.listDefinedDomains()
for domainName in domains:
domain = conn.lookupByName(domainName)
params = []
# TODO - replace with api calls to fetch network interfaces
xml = minidom.parseString(domain.XMLDesc(0))
devices = xml.documentElement.getElementsByTagName("devices")
for device in devices:
for interface in device.getElementsByTagName("interface"):
macadd = interface.getElementsByTagName("mac")
foundmac = macadd[0].getAttribute("address")
if foundmac == mac:
logging.info("Waking up %s", domainName)
domain.create()
return True
logging.info("Didn't find a VM with MAC address %s", mac)
return False
@staticmethod
def GetMACAddress(s):
# added fix for ether proto 0x0842
size = len(s)
bytes = map(lambda x: '%.2x' % x, map(ord, s))
counted = 0
macpart = 0
maccounted = 0
macaddress = None
newmac = ""
for byte in bytes:
if counted < 6:
# find 6 repetitions of 255 and added fix for ether proto 0x0842
if byte == "ff" or size < 110:
counted += 1
else:
# find 16 repititions of 48 bit mac
macpart += 1
if newmac != "":
newmac += ":"
newmac += byte
if macpart is 6 and macaddress is None:
macaddress = newmac
if macpart is 6:
#if macaddress != newmac:
#return None
newmac = ""
macpart = 0
maccounted += 1
if counted > 5 and maccounted > 5:
return macaddress
@staticmethod
def DecodeIPPacket(s):
if len(s) < 20:
return None
d = {}
d['version'] = (ord(s[0]) & 0xf0) >> 4
d['header_len'] = ord(s[0]) & 0x0f
d['tos'] = ord(s[1])
d['total_len'] = socket.ntohs(struct.unpack('H', s[2:4])[0])
d['id'] = socket.ntohs(struct.unpack('H', s[4:6])[0])
d['flags'] = (ord(s[6]) & 0xe0) >> 5
d['fragment_offset'] = socket.ntohs(struct.unpack('H', s[6:8])[0] & 0x1f)
d['ttl'] = ord(s[8])
d['protocol'] = ord(s[9])
d['checksum'] = socket.ntohs(struct.unpack('H', s[10:12])[0])
d['source_address'] = pcap.ntoa(struct.unpack('i', s[12:16])[0])
d['destination_address'] = pcap.ntoa(struct.unpack('i', s[16:20])[0])
if d['header_len'] > 5:
d['options'] = s[20:4 * (d['header_len'] - 5)]
else:
d['options'] = None
d['data'] = s[4 * d['header_len']:]
return d
@staticmethod
def InspectIPPacket(pktlen, data, timestamp):
if not data:
return
decoded = LibVirtWakeOnLan.DecodeIPPacket(data[14:])
macaddress = LibVirtWakeOnLan.GetMACAddress(decoded['data'])
if not macaddress:
return
return LibVirtWakeOnLan.StartServerByMACAddress(macaddress)
if __name__ == '__main__':
from lvwolutils import Utils
Utils.SetupLogging()
# line below is replaced on commit
LVWOLVersion = "20140814 231218"
Utils.ShowVersion(LVWOLVersion)
if len(sys.argv) < 2:
print('usage: libvirtwol <interface>')
sys.exit(0)
interface = sys.argv[1]
p = pcap.pcapObject()
net, mask = pcap.lookupnet(interface)
# set promiscuous to 1 so all packets are captured
p.open_live(interface, 1600, 1, 100)
# added support for ether proto 0x0842
p.setfilter('udp port 7 or udp port 9 or ether proto 0x0842', 0, 0)
while True:
try:
p.dispatch(1, LibVirtWakeOnLan.InspectIPPacket)
except KeyboardInterrupt:
break
except Exception:
continue