-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebmin-ip-update.ps1
293 lines (233 loc) · 9.97 KB
/
webmin-ip-update.ps1
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Script Name: webmin-ip-update.ps1
#
# Description: Script which automatically changes the allowed IP on Webmin, depending on the dynamic IP assigned to the machine you are connecting from.
#
# Author: Unalignedcoder
# URL: https://github.com/unalignedcoder/webmin-ip-update/
# Copyright 2023 Unalignedcoder
# All rights reserved.
#
# 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.
#============== Customize the following variables to your needs
# Path to plink executable
$plink = "PLINK.EXE"
# Array of IP services to query
# thanks to https://www.scriptinglibrary.com/languages/powershell/how-to-get-your-external-ip-with-powershell-core-using-a-restapi/
$ipServices = @("https://icanhazip.com", "https://api.ipify.org", "https://ipinfo.io/json | select-object -ExpandProperty ip", "https://jsyk.it/ip")
# Define the file path for miniserv.conf
$miniservConfPath = "/etc/webmin/miniserv.conf"
# SSH variables
$sshHost = "<ip-number or server hostname>"
$sshUser = "<username with write privileges to miniserv.conf>"
$sshPort = "<port number>" # Usually 22, custom number is recommended
$hostKey = "<Public Host Key Fingerprint in the key-type:host-key format>" # Probably necessary only the first time the script is ran
# File to store the last known IP
$ipStore = "$PSScriptRoot/.last_known_ip.txt"
# Restart Webmin? May not work if set to false
$restartWebmin = $true
# Should multiple IPs be allowed?
$multipleIPs = $false
# Create log file? For debugging purposes
$logFile = $true
#should the log file be printed in reverse order?
$logReverse = $true
# Define a log file path
$logFilePath = "$PSScriptRoot\script.log"
#============== Functions
# Function to log messages to a file
function Log-Message {
param (
[string]$Message
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "$timestamp - $Message"
# do only if Logging is enabled
if ($logFile) {
#check if printing in reverse or not
if ($logReverse) {
# Read the existing content of the log file
$existingContent = Get-Content -Path $logFilePath -Raw
# Prepend the new log entry to the existing content
$updatedContent = "$logEntry`n$existingContent"
# Write the updated content back to the log file
$updatedContent | Set-Content -Path $logFilePath -Encoding UTF8
} else {
$logEntry | Out-File -Append -FilePath $logFilePath -Encoding UTF8
}
}
}
# Query IP services to get the external IP address
function Get-ExternalIP {
foreach ($ipService in $ipServices) {
try {
$externalIP = (Invoke-WebRequest -Uri $ipService -ErrorAction Stop).Content.Trim()
if ($externalIP) {
Log-Message -Message "Your IP appears to be $externalIP"
return $externalIP
}
} catch {
$errorMessage = "Failed to retrieve external IP from $ipService. Error: $_"
Log-Message -Message $errorMessage
}
}
# Write-Host "Failed to retrieve external IP"
Log-Message -Message "Failed to retrieve external IP from any IP service. Exiting."
throw "Failed to retrieve external IP"
}
# unneeded for IP services, required for DNS requests
# Check if the IP contains both v4 and v6 parts, extract IPv4
<#if($externalIP -match '\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}(?:\s*:\S*)?') {
$ipParts = $externalIP.Split(':')
$ipv4Part = $ipParts[-1].TrimStart('.')
$externalIP = $ipv4Part
} #>
# Display BurntToast Notifications
function Show-BurntToastNotification {
param(
[string]$Text,
[string]$AppLogo
)
# Install the BurntToast module if not already installed
if (-not (Get-Module -Name BurntToast -ListAvailable )) {
Install-Module -Name BurntToast -Scope CurrentUser
}
# for when the above is commented out, we log a message.
if (Get-Module -Name BurntToast -ListAvailable) {
New-BurntToastNotification -Text $Text -AppLogo $AppLogo
} else {
Log-Message -Message "BurntToast module is not installed. Cannot display system notifications."
}
}
#============== SSH Agents checks (it's either Pageant or OpenSSH)
# Check if Pageant is running
function check-SSH {
$processPageant = Get-Process -Name "Pageant" -ErrorAction SilentlyContinue
if ($processPageant -ne $null) {
# Pageant is running, continue with the script
Log-Message -Message "Pageant is running."
}
elseif ($env:SSH_AUTH_SOCK -ne $null) {
# OpenSSH agent is running
# Check if SSH keys are loaded in OpenSSH agent
$sshKeysLoaded = $null
try {
$sshKeysLoaded = ssh-add -l 2>$null
} catch {
# Error occurred, likely due to no keys loaded
$sshKeysLoaded = $null
}
if ($sshKeysLoaded -eq $null) {
# No keys loaded, provide a warning and exit
$errorMessage = "OpenSSH agent is running, but no SSH keys are loaded."
Log-Message -Message "Warning: $errorMessage"
New-BurntToastNotification -Text "Warning: $errorMessage" -AppLogo "error.png"
exit 1 # You can choose an appropriate exit code
} else {
# SSH keys are loaded in OpenSSH agent
Log-Message -Message "SSH keys are loaded in OpenSSH agent."
}
}
else {
# Neither Pageant nor OpenSSH agent is running, exit with a warning
$errorMessage = "Could not find any SSH Agent running. Please start an SSH agent and load your SSH keys before running this script."
Log-Message -Message "Warning: $errorMessage"
New-BurntToastNotification -Text "Warning: $errorMessage" -AppLogo "error.png"
exit 1 # You can choose an appropriate exit code
}
}
#============== Execution
try {
#Start log session
Log-Message -Message "===== New Log Session ====="
Log-Message -Message "Script: PowerShell"
#check SSH agent
check-SSH
# Call the Get-ExternalIP function to retrieve the external IP
$externalIP = Get-ExternalIP
# Log the current IP
Log-Message -Message "Current IP: $externalIP"
# Read the old IP from the store file and Log it to console
$oldIP = ""
if (Test-Path -Path $ipStore) {
$oldIP = Get-Content -Path $ipStore
# Write-Host "Last logged IP: $oldIP"
Log-Message -Message "Last logged IP: $oldIP"
}
# Use Plink to check miniserv.conf content
$miniservConfContent = Invoke-Expression -Command "$plink -ssh $sshUser@$sshHost -hostkey $hostKey -batch -P $sshPort cat $miniservConfPath"
# Check if preserving multiple allowed IPs is necessary
if ($multipleIPs) {
# Regular expression to match the 'allow=' line and its IP addresses
$regex = 'allow=([^\n]*)'
# Get the current 'allow=' line from miniserv.conf
$currentAllowLine = ($miniservConfContent | Select-String -Pattern $regex).Matches.Groups[1].Value
# Split the current 'allow=' line into an array of IP addresses/hostnames
$allowIPs = $currentAllowLine.Split(" ", [System.StringSplitOptions]::RemoveEmptyEntries)
# Check if the old IP exists in the array of IP addresses
$oldIPIndex = $allowIPs.IndexOf($oldIP)
# Check if the old IP exists and is different from the new IP
if ($oldIPIndex -ne -1 -and $oldIP -ne $externalIP) {
$allowIPs[$oldIPIndex] = $externalIP
}
elseif ($oldIP -eq $externalIP) {
# If the old IP is the same as the new IP, exit the script
Log-Message -Message "$externalIP is already allowed in Webmin. Nothing to do."
# Pause execution to keep the window open (debug feature)
# Read-Host "Press Enter to exit..."
exit
}
else {
# If the old IP doesn't exist, add the new IP to the array
$allowIPs += $externalIP
}
# Reconstruct the 'allow=' line with the updated IP addresses
$updatedAllowLine = "allow=" + ($allowIPs -join " ")
} else {
$updatedAllowLine = "allow=$externalIP"
}
# Use Plink to update miniserv.conf
$sshCommand = "sed -i 's/^allow=.*/$updatedAllowLine/' $miniservConfPath"
Invoke-Expression -Command "$plink -ssh $sshUser@$sshHost -hostkey $hostKey -batch -P $sshPort ""$sshCommand""" | Out-Null
# Show notification & log
Show-BurntToastNotification -Text "IP address updated successfully.`nNew IP: $externalIP" -AppLogo "ip.png"
Log-Message -Message "IP address updated successfully."
# Restart Webmin if so specified
if ($restartWebmin) {
Invoke-Expression -Command "$plink -ssh $sshUser@$sshHost -hostkey $hostKey -batch -P $sshPort ""systemctl restart webmin"""
# Show notification & Log
Show-BurntToastNotification -Text "Webmin restarted." -AppLogo "webmin.png"
Log-Message -Message "Webmin restarted."
}
# Update the IP store file with the current IP
$externalIP | Set-Content -Path $ipStore
# Write-Host "Most recent IP added to store file."
Log-Message -Message "Most recent IP added to store file."
} catch {
# Handle errors
$errorMessage = $_.Exception.Message
if ($errorMessage == "Cannot index into a null array") {
$errorMessage = "Cannot login. Your SSH keys are probably not loaded."
}
# Notification when there's an error
New-BurntToastNotification -Text "$errorMessage" -AppLogo "error.png"
# Write-Host "Error: An error occurred: $errorMessage"
Log-Message -Message "Error: $errorMessage"
}
# Pause execution to keep the window open (debug feature)
# Read-Host "Press Enter to exit..."