summaryrefslogtreecommitdiff
path: root/wol-valiant
blob: d0e20f3ff1db645bcecd15ea70b671ab0710991f (plain)
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
#!/usr/bin/python

# wake up a host when people try to ssh to it.
# requires a iptables forward rule like this (ferm style):
#
#  domain (ip ip6) table filter chain $ch
#                        daddr <ipv4addr>
#                                proto tcp dport 22 tcp-flags (SYN ACK FIN RST) SYN
#                                mod state state NEW
#                                mod limit limit 1/minute limit-burst 1
#                                NFLOG nflog-group 1371 nflog-prefix "ssh to valiant";
#
# run from cron:
# @reboot screen -d -S wol -m ./wol-valiant <aa:bb:cc:dd:ee:ff> <ipv4addr> <interface>

# Copyright 2013-2017 Peter Palfrader
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


import dpkt, nflog, socket, subprocess, sys, time
import optparse

NFLOG_GROUP = 1371

def cb(payload):
	#print payload
	#print payload.__dict__

	#print "payload len ", payload.get_length()
	data = payload.get_data()
	pkt = dpkt.ip.IP(data)
	#print "proto:", pkt.p
	#print "source: %s" % socket.inet_ntoa(pkt.src)
	#print "dest: %s" % socket.inet_ntoa(pkt.dst)
	#if pkt.p == dpkt.ip.IP_PROTO_TCP:
	# 	print "  sport: %s" % pkt.tcp.sport
	# 	print "  dport: %s" % pkt.tcp.dport

	#return 1
	print "[%s] python callback called because of %s!"%(time.strftime("%Y-%m-%d %H:%M:%S"), socket.inet_ntoa(pkt.src))
	subprocess.call(['wakeonlan', MAC])
	subprocess.call(['ip', 'neigh', 'replace', IPADDR, 'lladdr', MAC, 'nud', 'stale', 'dev', INTERFACE])
	sys.stdout.flush()

parser = optparse.OptionParser()
parser.set_usage("%prog <mac addr> <ip addr> <interface>")
(options, args) = parser.parse_args()

if len(args) != 3:
    parser.print_help()
    sys.exit(1)
MAC = args[0]
IPADDR = args[1]
INTERFACE = args[2]


l = nflog.log()

print "setting callback"
l.set_callback(cb)

print "open"
l.fast_open(NFLOG_GROUP, socket.AF_INET)

print "trying to run"
try:
	l.try_run()
except KeyboardInterrupt, e:
	print "interrupted"


print "unbind"
l.unbind(socket.AF_INET)

print "close"
l.close()