-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_wan_ip
executable file
·94 lines (82 loc) · 2.88 KB
/
get_wan_ip
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
#!/bin/bash
# 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/>.
################################################################################
#
# This script can be used to obtain the WAN ip address from a SMCD3GNV router.
# Software version: eMTA & DOCSIS Software Version: 3.1.5.3.3_NCS
#
# Home page: https://github.com/samiam/ddclient-SMCD3GNV
#
# ddclient example setup:
# use=cmd, cmd=/etc/ddclient/get_wan_ip
# protocol=freedns
# server=freedns.afraid.org
# login=<your_login>
# password=<your_password>
# <your_ddns_hostname>
#
################################################################################
## Start config
ROUTER_IP="10.0.0.1" # default
ROUTER_PASSWORD="password" # default
## End config
export PATH=/bin:/usr/bin:$PATH
COOKIE_JAR=/var/tmp/cookie_jar # for curl to store cookies
TEMPFILE=$(tempfile) # downloaded file
################################################################################
# Login to router and add cookie
function router_login () {
curl --silent \
--cookie-jar $COOKIE_JAR \
--output /dev/null \
--form "usernamehaha=admin&passwordhaha=$ROUTER_PASSWORD" \
--referer http://$ROUTER_IP/goform/login \
http://$ROUTER_IP/goform/login
}
# Get connection status page
# --location will follow redirect to home_loggedout.asp
# if cookie doesn't work
function fetch_connection_status () {
curl --silent \
--cookie $COOKIE_JAR \
--output $TEMPFILE \
--referer http://$ROUTER_IP/goform/login \
--location \
http://$ROUTER_IP/user/connection_status.asp
}
# Sanity check that we got page
function check_connection_status () {
grep -q connection_status.asp $TEMPFILE
}
function print_wan_ip () {
perl -ne '/^var wan_ipAddress = \"(.*)\"/ && print "$1\n";' $TEMPFILE
}
function cleanup () {
rm $TEMPFILE
exit 0
}
################################################################################
## Mainline
# Try old cookie...
if [ -s $COOKIE_JAR ]; then
#echo "Using cached cookie..."
fetch_connection_status
check_connection_status && print_wan_ip && cleanup # success!
fi
# Cached cookie unsuccessful... reset
router_login
fetch_connection_status
check_connection_status && print_wan_ip && cleanup # success!
echo "Error: Unable to obtain WAN connection ip address: see $TEMPFILE"
exit 1