Compare commits

15 Commits

Author SHA1 Message Date
7b871edada Merge branch 'main' of https://gitea.elpatron.me/elpatron/Install-Beszel-Agent 2025-01-19 16:14:13 +01:00
58fb7067eb Merge branch 'main' of https://gitea.elpatron.me/elpatron/Install-Beszel-Agent 2025-01-19 16:14:06 +01:00
2ae4d963ea Merge branch 'main' of https://gitea.elpatron.me/elpatron/Install-Beszel-Agent 2025-01-19 16:12:17 +01:00
09ce390537 Update README.md 2025-01-19 16:12:04 +01:00
edc1df0bbd Update README.md 2025-01-19 16:10:22 +01:00
9706917679 Add missing scripts 2025-01-19 16:07:46 +01:00
f3d3a8472e Add env.cmd 2025-01-19 16:07:18 +01:00
39c458f321 Update README.md 2025-01-19 16:06:48 +01:00
683095b6d7 Move variables to env.cmd 2025-01-19 15:53:11 +01:00
281b3ce27d Add update_agent.cmd 2025-01-19 15:43:51 +01:00
3b730c162b Add build script 2025-01-19 14:37:37 +01:00
39069fe26d Typo, change text 2025-01-19 13:41:11 +01:00
9e6ba0da51 Redirect tar output 2025-01-19 13:39:34 +01:00
f5808f4461 Exception handling 2025-01-19 13:36:23 +01:00
ab8abaee85 Update README.md 2025-01-19 13:27:17 +01:00
9 changed files with 308 additions and 151 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/*.pdf
/*.zip

View File

@@ -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, inspired by [*Using Beszel to monitor Windows*](https://blog.ktz.me/using-beszel-to-monitor-windows/) by Alex Kretschmar.
## 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> .\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, 17th 2025 by me and should be exchanged by a self compiled or official executable, see *(1) Building the executable*.
The provided `agent.exe` was compiled on January, 18th 2025 by me and should be exchanged by a self compiled or official executable. For more details, see [*Using Beszel to monitor Windows*](https://blog.ktz.me/using-beszel-to-monitor-windows/) by Alex Kretschmar.
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`.

View File

@@ -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 .

BIN
agent.exe

Binary file not shown.

51
build_agent.cmd Normal file
View 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
View 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

View File

@@ -1,24 +1,35 @@
@echo off @echo off
cd /D "%~dp0" 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 !%1==! goto help
if !%2==! goto help if !%2==! goto help
set BESZELKEYPREFIX=%1 set BESZELKEYPREFIX=%1
set BESZELKEY=%2 set BESZELKEY=%2
set SERVICENAME=BeszelAgent call env.cmd
set DESTINATIONDIR=%ProgramFiles%\%SERVICENAME%
set AGENTEXE=%DESTINATIONDIR%\agent.exe
set NSSMEXE=nssm.exe
where nssm.exe >nul 2>&1 where nssm.exe >nul 2>&1
if %errorlevel%==1 goto installnssm if %errorlevel% NEQ 0 goto installnssm
goto proceed goto proceed
:installnssm :installnssm
echo Downloading NSSM echo Downloading NSSM
curl.exe --output "%TEMP%\nssm-2.24.zip" --url https://nssm.cc/release/nssm-2.24.zip >nul 2>&1 curl.exe --output "%TEMP%\nssm-2.24.zip" --url https://nssm.cc/release/nssm-2.24.zip >nul 2>&1
echo Extracting ZIP file 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% echo Copying nssm.exe to %DESTINATIONDIR%
if %PROCESSOR_ARCHITECTURE%==AMD64 ( if %PROCESSOR_ARCHITECTURE%==AMD64 (
copy .\nssm-2.24\win64\nssm.exe "%DESTINATIONDIR%\" >nul 2>&1 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 rd /s/q .\nssm-2.24 >nul 2>&1
:proceed :proceed
echo Creating deirectory %DESTINATIONDIR% echo Creating directory %DESTINATIONDIR%
md "%DESTINATIONDIR%" >nul 2>&1 md "%DESTINATIONDIR%" >nul 2>&1
echo Copying agent.exe echo Copying agent.exe to %DESTINATIONDIR%
copy .\agent.exe "%DESTINATIONDIR%\" >nul 2>&1 copy .\agent.exe "%DESTINATIONDIR%\" >nul 2>&1
echo Adding firewall rule echo Adding firewall rule
netsh advfirewall firewall add rule name="%SERVICENAME%" dir=in action=allow program="%AGENTEXE%" enable=yes >nul 2>&1 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 %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 %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 %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% %NSSMEXE% dump %SERVICENAME%
goto end goto end
:help :help
echo You have to provide a Beszel ssh key, e.g. 'install-beszel-service.cmd ssh-ed25519 AAAAC3L3lIpn94iu2TIpdmneXsEC1TDvrg4HCNoM1aiC683WMZfGOVoatrJSkysaQB9K 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. echo The key must not be enclosed in "" or '', just copy and paste it as it is.
:end :end

29
uninstall_agent.cmd Normal file
View 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
View 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