Compare commits
13 Commits
1.0
...
2ae4d963ea
Author | SHA1 | Date | |
---|---|---|---|
2ae4d963ea | |||
09ce390537 | |||
edc1df0bbd | |||
9706917679 | |||
f3d3a8472e | |||
39c458f321 | |||
683095b6d7 | |||
281b3ce27d | |||
3b730c162b | |||
39069fe26d | |||
9e6ba0da51 | |||
f5808f4461 | |||
ab8abaee85 |
94
README.md
94
README.md
@@ -1,21 +1,95 @@
|
||||
# Install Beszel Agent As Service Under Windows
|
||||
# BeszelWin
|
||||
|
||||
The Windows *cmd* file `install-beszel-service.cmd` installs the [Beszel](https://beszel.dev) agent as a service under Microsoft Windows.
|
||||
A collection of four scripts to build, install, update or uninstall the [Beszel](https://beszel.dev) agent under Microsoft Windows.
|
||||
|
||||
## Usage
|
||||
## (1) Building the executable
|
||||
|
||||
Open a `cmd.exe` shell with administrative rights.
|
||||
### Prerequisites
|
||||
|
||||
- Microsoft Windows 10 or later **Client** OS (64 bit)
|
||||
|
||||
*Building under Windows Server OS should be working, but you have to install Go and Git manually as Winget is not available for Windows server operating systems.*
|
||||
|
||||
### Compiling
|
||||
|
||||
Download and extract [install-beszel-service.zip](https://gitea.elpatron.me/elpatron/Install-Beszel-Agent/releases/download/1.0/install-beszel-service.zip).
|
||||
|
||||
Open a `cmd.exe` shell, `cd` into the script directory (where you extracted the archive) and start the build-script:
|
||||
|
||||
```
|
||||
.\build_agent.cmd
|
||||
```
|
||||
|
||||
The script will install *Git* and *Go* (if not yet installed) with *Winget*, clone the *Beszel* repository and build a Windows 64 bit `agent.exe` binary.
|
||||
|
||||
## (2) Installing the service
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Microsoft Windows 10 or later - *Server OS should work, too*
|
||||
|
||||
### Installation
|
||||
|
||||
Download and extract [install-beszel-service.zip](https://gitea.elpatron.me/elpatron/Install-Beszel-Agent/releases/download/1.0/install-beszel-service.zip) if you haven´t done this in before.
|
||||
|
||||
Open a `cmd.exe` shell with **administrative rights** and `cd` into the script directory (where you extracted the archive).
|
||||
|
||||
If you have built the agent by yourself: Copy your `agent.exe` binary to the directory where you extracted the scripts. Overwrite `agent.exe` from the archive.
|
||||
|
||||
Example:
|
||||
|
||||
```
|
||||
copy /y .\beszel\beszel\cmd\agent\agent.exe .
|
||||
```
|
||||
|
||||
Start the installation script provided with your Beszel Ssh key:
|
||||
|
||||
````
|
||||
git clone https://gitea.elpatron.me/elpatron/Install-Beszel-Agent
|
||||
cd Install-Beszel-Agent
|
||||
.\install-beszel-service.cmd <your Beszel ssh key>
|
||||
````
|
||||
|
||||
## Explanation
|
||||
The script copies `agent.exe` into the directory `%ProgramFiles%\BeszelAgent` and takes usage of *[NSSM - the Non-Sucking Service Manager](https://nssm.cc/)* to install the service. If *NSSM* is not yet installed on your system, it will be downloaded and installed to the Beszel agent directory.
|
||||
|
||||
The script copies `agent.exe` into the directory `%ProgramFiles%\BeszelAgent` and takes usage of *[NSSM - the Non-Sucking Service Manager](https://nssm.cc/)* to install the service. *NSSM* is being installed by *[Winget](https://github.com/microsoft/winget-cli)*.
|
||||
|
||||
The provided `agent.exe` was compiled on January, 18th 2025 by me and should be exchanged by a self compiled or official executable.
|
||||
The provided `agent.exe` was compiled on January, 17th 2025 by me and should be exchanged by a self compiled or official executable, see *(1) Building the executable*.
|
||||
|
||||
For more details, see [*Using Beszel to monitor Windows*](https://blog.ktz.me/using-beszel-to-monitor-windows/) by Alex Kretschmar.
|
||||
|
||||
## (3) Updating the service
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- To update the Beszel agent, it has to be installed using the install script.
|
||||
|
||||
### Updating
|
||||
|
||||
Download and extract [install-beszel-service.zip](https://gitea.elpatron.me/elpatron/Install-Beszel-Agent/releases/download/1.0/install-beszel-service.zip) if you haven´t done this before.
|
||||
|
||||
Open a `cmd.exe` shell with **administrative rights** and `cd` into the script directory (where you extracted the archive).
|
||||
|
||||
Start the update by launching
|
||||
|
||||
```
|
||||
.\update_agent.cmd
|
||||
```
|
||||
|
||||
The update will make a fresh clone of the Beszel GitHub repository, build the executable, stop the agent service, copy the new `agent.exe` to `%ProgramFiles%\BeszelAgent` and start the service.
|
||||
|
||||
## (4) Uninstall the service
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- To uninstall the Beszel agent, it has to be installed using the install script.
|
||||
|
||||
### Uninstalling
|
||||
|
||||
Download and extract [install-beszel-service.zip](https://gitea.elpatron.me/elpatron/Install-Beszel-Agent/releases/download/1.0/install-beszel-service.zip) if you haven´t done this before.
|
||||
|
||||
Open a `cmd.exe` shell with **administrative rights**.
|
||||
|
||||
Start the update by launching
|
||||
|
||||
```
|
||||
.\uninstall_agent.cmd
|
||||
```
|
||||
|
||||
The script will stop and remove the Beszel service and delete the installation directory `%ProgramFiles%\BeszelAgent`.
|
130
RefreshEnv.cmd
130
RefreshEnv.cmd
@@ -1,130 +0,0 @@
|
||||
:: Code generously provided by @beatcracker: https://github.com/beatcracker/detect-batch-subshell
|
||||
@echo off
|
||||
|
||||
setlocal EnableDelayedExpansion
|
||||
|
||||
:: Dequote path to command processor and this script path
|
||||
set ScriptPath=%~0
|
||||
set CmdPath=%COMSPEC:"=%
|
||||
|
||||
:: Get command processor filename and filename with extension
|
||||
for %%c in (!CmdPath!) do (
|
||||
set CmdExeName=%%~nxc
|
||||
set CmdName=%%~nc
|
||||
)
|
||||
|
||||
:: Get this process' PID
|
||||
:: Adapted from: http://www.dostips.com/forum/viewtopic.php?p=22675#p22675
|
||||
set "uid="
|
||||
for /l %%i in (1 1 128) do (
|
||||
set /a "bit=!random!&1"
|
||||
set "uid=!uid!!bit!"
|
||||
)
|
||||
|
||||
for /f "tokens=2 delims==" %%i in (
|
||||
'wmic Process WHERE "Name='!CmdExeName!' AND CommandLine LIKE '%%!uid!%%'" GET ParentProcessID /value'
|
||||
) do (
|
||||
rem Get commandline of parent
|
||||
for /f "tokens=1,2,*" %%j in (
|
||||
'wmic Process WHERE "Handle='%%i'" GET CommandLine /value'
|
||||
) do (
|
||||
|
||||
rem Strip extra CR's from wmic output
|
||||
rem http://www.dostips.com/forum/viewtopic.php?t=4266
|
||||
for /f "delims=" %%x in ("%%l") do (
|
||||
rem Dequote path to batch file, if any (3rd argument)
|
||||
set ParentScriptPath=%%x
|
||||
set ParentScriptPath=!ParentScriptPath:"=!
|
||||
)
|
||||
|
||||
rem Get parent process path
|
||||
for /f "tokens=2 delims==" %%y in ("%%j") do (
|
||||
rem Dequote parent path
|
||||
set ParentPath=%%y
|
||||
set ParentPath=!ParentPath:"=!
|
||||
|
||||
rem Handle different invocations: C:\Windows\system32\cmd.exe , cmd.exe , cmd
|
||||
for %%p in (!CmdPath! !CmdExeName! !CmdName!) do (
|
||||
if !ParentPath!==%%p set IsCmdParent=1
|
||||
)
|
||||
|
||||
rem Check if we're running in cmd.exe with /c switch and this script path as argument
|
||||
if !IsCmdParent!==1 if %%k==/c if "!ParentScriptPath!"=="%ScriptPath%" set IsExternal=1
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
if !IsExternal!==1 (
|
||||
echo %~nx0 does not work when run from this process. If you're in PowerShell, please 'Import-Module $env:ChocolateyInstall\helpers\chocolateyProfile.psm1' and try again.
|
||||
exit 1
|
||||
)
|
||||
|
||||
endlocal
|
||||
:: End code from @beatcracker
|
||||
@echo off
|
||||
::
|
||||
:: RefreshEnv.cmd
|
||||
::
|
||||
:: Batch file to read environment variables from registry and
|
||||
:: set session variables to these values.
|
||||
::
|
||||
:: With this batch file, there should be no need to reload command
|
||||
:: environment every time you want environment changes to propagate
|
||||
|
||||
::echo "RefreshEnv.cmd only works from cmd.exe, please install the Chocolatey Profile to take advantage of refreshenv from PowerShell"
|
||||
echo | set /p dummy="Refreshing environment variables from registry for cmd.exe. Please wait..."
|
||||
|
||||
goto main
|
||||
|
||||
:: Set one environment variable from registry key
|
||||
:SetFromReg
|
||||
"%WinDir%\System32\Reg" QUERY "%~1" /v "%~2" > "%TEMP%\_envset.tmp" 2>NUL
|
||||
for /f "usebackq skip=2 tokens=2,*" %%A IN ("%TEMP%\_envset.tmp") do (
|
||||
echo/set "%~3=%%B"
|
||||
)
|
||||
goto :EOF
|
||||
|
||||
:: Get a list of environment variables from registry
|
||||
:GetRegEnv
|
||||
"%WinDir%\System32\Reg" QUERY "%~1" > "%TEMP%\_envget.tmp"
|
||||
for /f "usebackq skip=2" %%A IN ("%TEMP%\_envget.tmp") do (
|
||||
if /I not "%%~A"=="Path" (
|
||||
call :SetFromReg "%~1" "%%~A" "%%~A"
|
||||
)
|
||||
)
|
||||
goto :EOF
|
||||
|
||||
:main
|
||||
echo/@echo off >"%TEMP%\_env.cmd"
|
||||
|
||||
:: Slowly generating final file
|
||||
call :GetRegEnv "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" >> "%TEMP%\_env.cmd"
|
||||
call :GetRegEnv "HKCU\Environment">>"%TEMP%\_env.cmd" >> "%TEMP%\_env.cmd"
|
||||
|
||||
:: Special handling for PATH - mix both User and System
|
||||
call :SetFromReg "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" Path Path_HKLM >> "%TEMP%\_env.cmd"
|
||||
call :SetFromReg "HKCU\Environment" Path Path_HKCU >> "%TEMP%\_env.cmd"
|
||||
|
||||
:: Caution: do not insert space-chars before >> redirection sign
|
||||
echo/set "Path=%%Path_HKLM%%;%%Path_HKCU%%" >> "%TEMP%\_env.cmd"
|
||||
|
||||
:: Cleanup
|
||||
del /f /q "%TEMP%\_envset.tmp" 2>nul
|
||||
del /f /q "%TEMP%\_envget.tmp" 2>nul
|
||||
|
||||
:: capture user / architecture
|
||||
SET "OriginalUserName=%USERNAME%"
|
||||
SET "OriginalArchitecture=%PROCESSOR_ARCHITECTURE%"
|
||||
|
||||
:: Set these variables
|
||||
call "%TEMP%\_env.cmd"
|
||||
|
||||
:: Cleanup
|
||||
del /f /q "%TEMP%\_env.cmd" 2>nul
|
||||
|
||||
:: reset user / architecture
|
||||
SET "USERNAME=%OriginalUserName%"
|
||||
SET "PROCESSOR_ARCHITECTURE=%OriginalArchitecture%"
|
||||
|
||||
echo | set /p dummy="Finished."
|
||||
echo .
|
51
build_agent.cmd
Normal file
51
build_agent.cmd
Normal file
@@ -0,0 +1,51 @@
|
||||
@echo off
|
||||
cd /D "%~dp0"
|
||||
echo This script downloads the Beszel repository and builds the Beszel agent for Windows (64 Bit only).
|
||||
echo If not yet installed, Git and Go will be installed via Winget.
|
||||
echo.
|
||||
|
||||
call env.cmd
|
||||
|
||||
where winget.exe >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Winget not found, install Winget or use a compatible OS: Win 10 Client OS or later
|
||||
goto end
|
||||
) else (
|
||||
echo Winget executable found
|
||||
)
|
||||
|
||||
where go.exe >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Installing Go with Winget
|
||||
winget install GoLang.Go >nul 2>&1
|
||||
) else (
|
||||
echo Go executable found
|
||||
)
|
||||
|
||||
where git.exe >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Installing Git with Winget
|
||||
winget install Git.Git >nul 2>&1
|
||||
) else (
|
||||
echo Git executable found
|
||||
)
|
||||
|
||||
echo Cloning Beszel repository
|
||||
git clone https://github.com/henrygd/beszel.git >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Cloning the Beszel repository failed.
|
||||
goto end
|
||||
)
|
||||
|
||||
cd .\beszel\beszel\cmd\agent
|
||||
|
||||
echo Building agent
|
||||
go build -ldflags "-w -s" . >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Something went wrong
|
||||
) else (
|
||||
echo Build successful:
|
||||
dir agent.exe
|
||||
)
|
||||
|
||||
:end
|
8
env.cmd
Normal file
8
env.cmd
Normal file
@@ -0,0 +1,8 @@
|
||||
@echo off
|
||||
set SERVICENAME=BeszelAgent
|
||||
set DESTINATIONDIR=%ProgramFiles%\%SERVICENAME%
|
||||
set AGENTEXE=%DESTINATIONDIR%\agent.exe
|
||||
set NSSMEXE=nssm.exe
|
||||
set GOOS=windows
|
||||
set GOARCH=amd64
|
||||
set CGO_ENABLED=0
|
@@ -1,24 +1,35 @@
|
||||
@echo off
|
||||
cd /D "%~dp0"
|
||||
echo This script installs the Beszel agent for Windows.
|
||||
echo If not yet installed, NSSM will be installed via Winget.
|
||||
echo.
|
||||
|
||||
echo Checking adminstrative rights
|
||||
rem from https://stackoverflow.com/questions/7985755/how-to-detect-if-cmd-is-running-as-administrator-has-elevated-privileges
|
||||
net session >nul 2>&1
|
||||
if %errorlevel% equ 0 (
|
||||
echo Administrative rights check OK
|
||||
) else (
|
||||
echo You are NOT Administrator. Start the script from an administrative cmd.exe shell...
|
||||
goto end
|
||||
)
|
||||
|
||||
if !%1==! goto help
|
||||
if !%2==! goto help
|
||||
|
||||
set BESZELKEYPREFIX=%1
|
||||
set BESZELKEY=%2
|
||||
set SERVICENAME=BeszelAgent
|
||||
set DESTINATIONDIR=%ProgramFiles%\%SERVICENAME%
|
||||
set AGENTEXE=%DESTINATIONDIR%\agent.exe
|
||||
set NSSMEXE=nssm.exe
|
||||
call env.cmd
|
||||
|
||||
where nssm.exe >nul 2>&1
|
||||
if %errorlevel%==1 goto installnssm
|
||||
if %errorlevel% NEQ 0 goto installnssm
|
||||
goto proceed
|
||||
|
||||
:installnssm
|
||||
echo Downloading NSSM
|
||||
curl.exe --output "%TEMP%\nssm-2.24.zip" --url https://nssm.cc/release/nssm-2.24.zip >nul 2>&1
|
||||
echo Extracting ZIP file
|
||||
tar.exe -xf %TEMP%\nssm-2.24.zip
|
||||
tar.exe -xf %TEMP%\nssm-2.24.zip >nul 2>&1
|
||||
echo Copying nssm.exe to %DESTINATIONDIR%
|
||||
if %PROCESSOR_ARCHITECTURE%==AMD64 (
|
||||
copy .\nssm-2.24\win64\nssm.exe "%DESTINATIONDIR%\" >nul 2>&1
|
||||
@@ -31,27 +42,42 @@ del "%TEMP%\nssm-2.24.zip" >nul 2>&1
|
||||
rd /s/q .\nssm-2.24 >nul 2>&1
|
||||
|
||||
:proceed
|
||||
echo Creating deirectory %DESTINATIONDIR%
|
||||
echo Creating directory %DESTINATIONDIR%
|
||||
md "%DESTINATIONDIR%" >nul 2>&1
|
||||
|
||||
echo Copying agent.exe
|
||||
echo Copying agent.exe to %DESTINATIONDIR%
|
||||
copy .\agent.exe "%DESTINATIONDIR%\" >nul 2>&1
|
||||
|
||||
echo Adding firewall rule
|
||||
netsh advfirewall firewall add rule name="%SERVICENAME%" dir=in action=allow program="%AGENTEXE%" enable=yes >nul 2>&1
|
||||
|
||||
echo Installing service
|
||||
echo Installing service %SERVICENAME%
|
||||
%NSSMEXE% install %SERVICENAME% "%AGENTEXE%" >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Failed to install the service.
|
||||
goto end
|
||||
)
|
||||
|
||||
echo Setting environment for %SERVICENAME%
|
||||
%NSSMEXE% set %SERVICENAME% AppEnvironmentExtra "KEY=%BESZELKEYPREFIX% %BESZELKEY%" >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Failed to set the service environment.
|
||||
goto end
|
||||
)
|
||||
|
||||
echo Setting environment for service
|
||||
echo Starting %SERVICENAME%
|
||||
%NSSMEXE% start %SERVICENAME% >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Failed to start service.
|
||||
goto end
|
||||
)
|
||||
|
||||
echo Service installed, status:
|
||||
echo Dumping service state
|
||||
%NSSMEXE% dump %SERVICENAME%
|
||||
goto end
|
||||
|
||||
:help
|
||||
echo You have to provide a Beszel ssh key, e.g. 'install-beszel-service.cmd ssh-ed25519 AAAAC3L3lIpn94iu2TIpdmneXsEC1TDvrg4HCNoM1aiC683WMZfGOVoatrJSkysaQB9K
|
||||
echo The key must not be enclosed in "" or '', just copy and paste it as it is.
|
||||
|
||||
:end
|
29
uninstall_agent.cmd
Normal file
29
uninstall_agent.cmd
Normal file
@@ -0,0 +1,29 @@
|
||||
@echo off
|
||||
setlocal
|
||||
cd /D "%~dp0"
|
||||
echo This script uninstalls the Beszel agent
|
||||
echo.
|
||||
|
||||
call env.cmd
|
||||
|
||||
echo Checking adminstrative rights
|
||||
rem from https://stackoverflow.com/questions/7985755/how-to-detect-if-cmd-is-running-as-administrator-has-elevated-privileges
|
||||
net session >nul 2>&1
|
||||
if %errorlevel% equ 0 (
|
||||
echo Administrative rights check OK
|
||||
) else (
|
||||
echo You are NOT Administrator. Start the script from an administrative cmd.exe shell...
|
||||
goto end
|
||||
)
|
||||
|
||||
echo Stopping Beszel agent service
|
||||
%NSSMEXE% stop %SERVICENAME% >nul 2>&1
|
||||
|
||||
echo Removing Beszel agent service
|
||||
%NSSMEXE% remove %SERVICENAME% >nul 2>&1
|
||||
|
||||
echo Deleting installation directory
|
||||
rd /q/s %DESTINATIONDIR% >nul 2>&1
|
||||
|
||||
:end
|
||||
ech Done.
|
97
update_agent.cmd
Normal file
97
update_agent.cmd
Normal file
@@ -0,0 +1,97 @@
|
||||
@echo off
|
||||
setlocal
|
||||
cd /D "%~dp0"
|
||||
echo This script updates the Beszel agent to the latest version from the Github repository
|
||||
echo.
|
||||
|
||||
echo Checking adminstrative rights
|
||||
rem from https://stackoverflow.com/questions/7985755/how-to-detect-if-cmd-is-running-as-administrator-has-elevated-privileges
|
||||
net session >nul 2>&1
|
||||
if %errorlevel% equ 0 (
|
||||
echo Administrative rights check OK
|
||||
) else (
|
||||
echo You are NOT Administrator. Start the script from an administrative cmd.exe shell...
|
||||
goto end
|
||||
)
|
||||
|
||||
call env.cmd
|
||||
|
||||
where winget.exe >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Winget not found, install Winget or use a compatible OS: Win 10 Client OS or later
|
||||
goto end
|
||||
) else (
|
||||
echo Winget executable found
|
||||
)
|
||||
|
||||
where go.exe >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Installing Go with Winget
|
||||
winget install GoLang.Go >nul 2>&1
|
||||
) else (
|
||||
echo Go executable found
|
||||
)
|
||||
|
||||
where git.exe >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Installing Git with Winget
|
||||
winget install Git.Git >nul 2>&1
|
||||
) else (
|
||||
echo Git executable found
|
||||
)
|
||||
|
||||
if exist .\beszel (
|
||||
echo Subdirectory beszel already exists, it has to be deleted.
|
||||
echo - Hit Crtl+c to exit if you don't want the directory to be deleted.
|
||||
echo - Hit any other key to continue
|
||||
pause >nul 2>&1
|
||||
echo Deleting beszel subdirectory
|
||||
rd /q/s .\beszel >nul 2>&1
|
||||
)
|
||||
|
||||
:proceed
|
||||
echo Cloning Beszel repository from Github
|
||||
git clone https://github.com/henrygd/beszel.git >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Cloning repository failed.
|
||||
goto end
|
||||
)
|
||||
|
||||
cd .\beszel\beszel\cmd\agent
|
||||
|
||||
echo Building agent
|
||||
set GOOS=windows
|
||||
set GOARCH=amd64
|
||||
set CGO_ENABLED=0
|
||||
go build -ldflags "-w -s" . >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Something went wrong
|
||||
goto end
|
||||
) else (
|
||||
echo Build successful
|
||||
)
|
||||
|
||||
cd /D "%~dp0"
|
||||
|
||||
echo Stopping Beszel agent service
|
||||
net stop %SERVICENAME% >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Failed to stop Beszel agent service
|
||||
goto end
|
||||
)
|
||||
|
||||
echo Copying new agent
|
||||
copy .\beszel\beszel\cmd\agent\agent.exe %AGENTEXE% >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Failed to copy agent.exe
|
||||
)
|
||||
|
||||
echo Starting Beszel agent service
|
||||
net start %SERVICENAME% >nul 2>&1
|
||||
if %errorlevel% NEQ 0 (
|
||||
echo Failed to start Beszel agent service
|
||||
goto end
|
||||
)
|
||||
|
||||
:end
|
||||
endlocal
|
Reference in New Issue
Block a user