-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrebootHNAP.sh
executable file
·97 lines (79 loc) · 2.67 KB
/
rebootHNAP.sh
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
#!/bin/bash
# reboot HNAP compatible device
# works eg. for Dlink DIR 880L
#
# by https://github.com/stuffo/
#
IP="192.168.0.1" # your HNAP device IP
Username="Admin" # admin user
PIN="password" # PIN or password for authentication
# SOAP defaults
contentType="Content-Type: text/xml; charset=utf-8"
soapHead="<?xml version=\"1.0\" encoding=\"utf-8\"?>
<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">
<soap:Body>"
soapTail="</soap:Body></soap:Envelope>"
soapUrl="http://purenetworks.com/HNAP1/"
set -e
function getResult {
echo -n "$2" | grep -Po "(?<=<$1>).*(?=</$1>)"
}
function hash_hmac {
local data="$1"
local key="$2"
echo -n "$data" | openssl dgst "-md5" -hmac "$key" -binary | xxd -ps -u
}
function hnap_auth {
local method=$1
local timestamp=$(date +%s)
local auth_str="$timestamp\"$soapUrl$method\""
local auth=$(hash_hmac "$auth_str" "$privatekey")
echo "HNAP_AUTH: $auth $timestamp"
}
function soap_body {
local method=$1
local data=$2
echo "<$method xmlns=\"$soapUrl\">$data</$method>"
}
function soap_call {
local soapAction=$1
local data=$2
local auth=$3
local authHeaders
if [ "$auth" = "y" ] ; then
authHeaders=('-H' "$(hnap_auth $soapAction)")
authHeaders+=('-H' "$cookie")
fi
curl -s -S -f --connect-timeout 3 -s -X POST -H "$contentType" \
-H "SOAPAction: \"$soapUrl$soapAction\"" \
"${authHeaders[@]}" --data-binary "$soapHead$(soap_body $soapAction $data)$soapTail" \
http://$IP/HNAP1/
}
function soap_get_result {
local method=$1
local data=$2
local auth=$3
ret=$(soap_call $method $data $auth)
getResult ${method}Result "$ret"
}
login_ret=$(soap_call Login "<Action>request</Action><Username>$Username</Username><LoginPassword>password</LoginPassword><Captcha/>")
echo "Challenge Request Result: $(getResult LoginResult "$login_ret")"
challenge=$(getResult Challenge "$login_ret")
cookie="Cookie: uid="$(getResult Cookie "$login_ret")
publickey=$(getResult PublicKey "$login_ret")$PIN
privatekey=$(hash_hmac "$challenge" "$publickey")
password=$(hash_hmac "$challenge" "$privatekey")
ret=$(soap_get_result Login "<Action>login</Action><Username>$Username</Username><LoginPassword>$password</LoginPassword><Captcha/>" y)
echo "Login Result: $ret"
if [ "$ret" != "success" ] ; then
echo "Login Failed."
exit 1
fi
ret=$(soap_get_result Reboot "</empty>" y)
echo "Reboot Result: $ret"
if [ "$ret" == "REBOOT" ] ; then
echo "Reboot success"
exit 0
fi