forked from loblab/noip-renew
-
Notifications
You must be signed in to change notification settings - Fork 19
/
setup.sh
executable file
·201 lines (176 loc) · 6.04 KB
/
setup.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/bash
set -e
PYTHON=python3
PYTHON35=false
USER=$(whoami)
if [ "$USER" == "root" ]; then
USER=$1
if [ -z "$USER" ]; then
echo "Chrome is safer to run as normal user instead of 'root', so"
echo "run the script as a normal user (with sudo permission), "
echo "or specify the user: $0 <user>"
exit 1
fi
HOME=/home/$USER
else
SUDO=sudo
fi
function config() {
LOGDIR=/var/log/noip-renew/$USER
INSTDIR=/usr/local/bin
INSTEXE=$INSTDIR/noip-renew-$USER.sh
CRONJOB="0 1 * * * $INSTEXE $LOGDIR"
}
function install() {
OS=$(hostnamectl | grep -i "operating system")
echo "$OS"
case $OS in
*Arch?Linux*)
install_arch
;;
*)
install_debian
;;
esac
if [ "$PYTHON35" = true ]; then
$SUDO $PYTHON -m pip install future-fstrings
fi
}
function install_arch(){
$SUDO pacman -Qi cronie > /dev/null || $SUDO pacman -S cronie
$SUDO pacman -Qi python > /dev/null || $SUDO pacman -S python
$SUDO pacman -Qi python-pip > /dev/null || $SUDO pacman -S python-pip
$SUDO pacman -Qi python-pyotp > /dev/null || $SUDO pacman -S python-pyotp
$SUDO pacman -Qi chromium > /dev/null || $SUDO pacman -S chromium
$SUDO $PYTHON -m pip install selenium
}
function install_debian(){
echo "Installing necessary packages..."
deb_arch=$(dpkg --print-architecture)
if [ "$deb_arch" == "amd64" ]; then
wget=/usr/bin/wget
if [ ! -x "$wget" ]; then
$SUDO apt -y install wget
fi
$SUDO sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
$SUDO sh -c 'wget -O- https://dl.google.com/linux/linux_signing_key.pub |gpg --dearmor > /etc/apt/trusted.gpg.d/google.gpg'
fi
read -p 'Perform apt-get update? (y/n): ' update
if [ "${update^^}" = "Y" ]
then
$SUDO apt-get update
fi
$SUDO apt -y install chromium-chromedriver || \
$SUDO apt -y install chromium-driver || \
$SUDO apt -y install chromedriver
$SUDO apt -y install cron
PYV=`python3 -c "import sys;t='{v[0]}{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(t)";`
if [[ "$PYV" -lt "36" ]] || ! hash python3; then
if [[ "$PYV" -eq "35" ]]; then
PYTHON35=true
else
echo "This script requires Python version 3.5 or higher. Attempting to install..."
$SUDO apt-get -y install python3
fi
fi
$SUDO apt -y install chromium-browser || \
$SUDO apt -y install chromium # Update Chromium Browser or script won't work.
$SUDO apt -y install $PYTHON-pip
$SUDO apt -y install $PYTHON-pyotp
if [[ "$PYV" -gt "36" ]]; then
$SUDO apt -y install $PYTHON-selenium
else
$SUDO $PYTHON -m pip install selenium
fi
}
function deploy() {
echo "Deploying the script..."
# Remove current installation first.
if ls $INSTDIR/*noip-renew* 1> /dev/null 2>&1; then
$SUDO rm $INSTDIR/*noip-renew*
fi
$SUDO mkdir -p $LOGDIR
$SUDO chown $USER $LOGDIR
$SUDO cp noip-renew.py $INSTDIR
$SUDO cp noip-renew-skd.sh $INSTDIR
$SUDO cp noip-renew.sh $INSTEXE
$SUDO chown $USER $INSTEXE
$SUDO chown $USER $INSTDIR/noip-renew-skd.sh
$SUDO chmod 700 $INSTEXE
if [ "$PYTHON35" = true ]; then
$SUDO sed -i '2i # -*- coding: future_fstrings -*- ' $INSTDIR/noip-renew.py
fi
noip
$SUDO crontab -u $USER -l | grep -v '/noip-renew*' | $SUDO crontab -u $USER -
($SUDO crontab -u $USER -l; echo "$CRONJOB") | $SUDO crontab -u $USER -
$SUDO sed -i 's/USER=/USER='$USER'/1' $INSTDIR/noip-renew-skd.sh
echo "Installation Complete."
echo "To change noip.com account details, please run setup.sh again."
echo "Logs can be found in '$LOGDIR'"
}
function noip() {
echo "Enter your No-IP Account details...make sure you enabled 2fa authentication and saved the 2fa secret key"
read -p 'Username: ' uservar
read -sp 'Password: ' passvar
echo
read -sp '2FA Secret Key: ' totpsecret
passvar=`echo -n $passvar | base64`
echo
$SUDO sed -i 's/USERNAME=".*"/USERNAME="'$uservar'"/1' $INSTEXE
$SUDO sed -i 's/PASSWORD=".*"/PASSWORD="'$passvar'"/1' $INSTEXE
$SUDO sed -i 's/TOTP_SECRET=".*"/TOTP_SECRET="'$totpsecret'"/1' $INSTEXE
read -p 'Do you want randomized cronjob? (y/n): ' rcron
if [ "${rcron^^}" = "Y" ]
then
read -p 'Enter time interval (hours): ' tint
$SUDO sed -i '2 c Min=$(/usr/bin/shuf -i 0-59 -n 1)' $INSTDIR/noip-renew-skd.sh
$SUDO sed -i '3 c Hour=$(/usr/bin/shuf -i '$tint' -n 1)' $INSTDIR/noip-renew-skd.sh
fi
}
function installer() {
config
install
deploy
}
function uninstall() {
$SUDO sed -i '/noip-renew/d' /etc/crontab
$SUDO rm $INSTDIR/*noip-renew*
read -p 'Do you want to remove all log files? (y/n): ' clearLogs
if [ "${clearLogs^^}" = "Y" ]
then
$SUDO rm -rf $LOGDIR
$SUDO crontab -u $USER -l | grep -v '/noip-renew*' | $SUDO crontab -u $USER -
fi
}
PS3='Select an option: '
options=("Install/Repair Script" "Update noip.com account details" "Uninstall Script" "Exit setup.sh")
echo "No-IP Auto Renewal Script Setup."
select opt in "${options[@]}"
do
case $opt in
"Install/Repair Script")
installer
break
;;
"Update noip.com account details")
config
noip
echo "noip.com account settings updated."
break
;;
"Uninstall Script")
config
if ls $INSTDIR/*noip-renew* 1> /dev/null 2>&1; then
uninstall
echo "Script successfully uninstalled."
else
echo "Script is not installed."
fi
break
;;
"Exit setup.sh")
break
;;
*) echo "invalid option $REPLY";;
esac
done