-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoip.sh
More file actions
187 lines (156 loc) · 5.32 KB
/
autoip.sh
File metadata and controls
187 lines (156 loc) · 5.32 KB
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
#!/usr/bin/env bash
# Color and symbol configuration for logs
setup_colors() {
greenColor="\e[0;32m\033[1m"
redColor="\e[0;31m\033[1m"
blueColor="\e[0;34m\033[1m"
yellowColor="\e[0;33m\033[1m"
purpleColor="\e[0;35m\033[1m"
turquoiseColor="\e[0;36m\033[1m"
grayColor="\e[0;37m\033[1m"
endColor="\033[0m\e[0m"
plus="${greenColor}[+]${endColor}"
error="${redColor}[x]${endColor}"
warning="${yellowColor}[!]${endColor}"
question="${turquoiseColor}[?]${endColor}"
dash="${purpleColor}-${endColor}"
double_dash="${turquoiseColor}--${endColor}"
open_bracket="${purpleColor}[${endColor}"
close_bracket="${purpleColor}]${endColor}"
double_dot="${turquoiseColor}:${endColor}"
open_angle="${purpleColor}<${endColor}"
close_angle="${purpleColor}>${endColor}"
double_colon="${turquoiseColor}::${endColor}"
}
setup_colors
# Function to handle errors and events
log_message() {
local log_file="/var/log/autoip/autoip.log"
local level=$1
local message=$2
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
# Save the message in the log file
echo "${timestamp} ${level} ${message}" >> "$log_file"
# Show the message on standard output
case "$level" in
ERROR)
echo -e "${error} ${message}"
;;
WARNING)
echo -e "${warning} ${message}"
;;
INFO)
echo -e "${plus} ${message}"
;;
*)
echo -e "${plus} ${message}"
;;
esac
}
# Function to validate if the content is an IP
validate_ip() {
local ip="$1"
[[ "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]
}
# Function to update the IP in NO-IP
update_noip() {
local current_ip_file="/tmp/autoip"
if [[ -s "$current_ip_file" ]]; then
local current_ip=$(cat "$current_ip_file")
else
log_message "WARNING" "Current IP not found or the file is empty."
return 1
fi
if validate_ip "$current_ip"; then
response=$(curl -s -X GET "http://dynupdate.no-ip.com/nic/update?hostname=${hostname}&myip=${current_ip}" -u "${user}:${password}" -H "User-Agent: ${user_agent}")
# List of possible responses with their log levels and associated messages
declare -A responses=(
["good"]="INFO: DNS hostname updated successfully: ${response}"
["nochg"]="INFO: The IP is current, no update performed: ${response}"
["nohost"]="ERROR: The hostname does not exist under the specified account."
["badauth"]="ERROR: Invalid username and password combination."
["badagent"]="ERROR: Client disabled. No further updates should be made without user intervention."
["!donator"]="ERROR: Attempted to use a feature not available to the user."
["abuse"]="ERROR: Username blocked due to abuse. Updates halted."
["911"]="ERROR: Fatal error at No-IP. Retry no sooner than 30 minutes."
)
# Flags to find a match
found=false
# Search the list of possible responses
for key in "${!responses[@]}"; do
if echo "$response" | grep -q "^$key"; then
# Extract level and message from the array value
IFS=':' read -r level message <<< "${responses[$key]}"
log_message "$level" "$message"
found=true
break
fi
done
# Message for unexpected responses
if [[ "$found" == false ]]; then
log_message "WARNING" "Unexpected response from the server: ${response}"
fi
else
log_message "WARNING" "The IP ${current_ip} is invalid."
return 1
fi
}
# Function to read configuration from a TOML file
read_configuration() {
local config_file="$1"
local tq_dir="/usr/local/bin/tq"
hostname=$($tq_dir -f "$config_file" noip.hostname)
user=$($tq_dir -f "$config_file" noip.user)
password=$($tq_dir -f "$config_file" noip.password)
user_agent=$($tq_dir -f "$config_file" noip.user_agent)
log_message "INFO" "Configuration loaded from ${config_file}."
}
config_file="/usr/local/etc/autoip/config.toml"
read_configuration "$config_file"
# Function to get the IP and update if it has changed
get_ip() {
#local no_station="test" # testing
#if [[ -z "$no_station" ]]; then
#log_message "WARNING" "Could not obtain the station number."
#fi
servers=(
#"localhost:5000/?station=${no_station}" # testing
"api.ipify.org"
"icanhazip.com"
"ifconfig.me"
"wtfismyip.com/text"
)
local current_ip_file="/tmp/autoip"
local temp_ip_file="/tmp/autoip-temp"
local old_ip_file="/tmp/autoip-old"
local ip
for server in "${servers[@]}"; do
ip=$(curl -sL "$server" | grep -Eo '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)')
if [[ -n "$ip" ]]; then
echo "$ip" > "$temp_ip_file"
log_message "INFO" "IP obtained from ${server}: ${ip}"
break
else
log_message "ERROR" "Could not obtain a valid IP from ${server}."
fi
done
if [[ -s "$temp_ip_file" ]]; then
local new_ip=$(cat "$temp_ip_file")
local current_ip=""
if [[ -s "$current_ip_file" ]]; then
current_ip=$(cat "$current_ip_file")
fi
if validate_ip "$new_ip" && [[ "$new_ip" != "$current_ip" ]]; then
if validate_ip "$current_ip"; then
echo "$current_ip" > "$old_ip_file"
fi
echo "$new_ip" > "$current_ip_file"
log_message "INFO" "IP updated: ${new_ip}"
update_noip
else
log_message "INFO" "The IP has not changed."
fi
rm -f "$temp_ip_file"
fi
}
get_ip