Compare commits

14 Commits

Author SHA1 Message Date
531b39db5d Change text 2025-01-22 11:18:57 +01:00
33e1ef965b Update README.md 2025-01-22 11:16:08 +01:00
f3606631a0 Check status 2025-01-22 11:10:52 +01:00
84ed566c55 Fix NSSM path 2025-01-22 11:01:56 +01:00
c4c5f2841c nssm status 2025-01-20 14:32:35 +01:00
0ecaefeb10 Create directory before nssm download 2025-01-20 10:49:29 +01:00
43a65926fe 64 bit only 2025-01-19 18:23:09 +01:00
24a6e7678e add finishing message 2025-01-19 18:13:04 +01:00
ae01d517d8 redirect winget output 2025-01-19 18:05:31 +01:00
581e856e50 --accept-source-agreements --accept-package-agreements 2025-01-19 18:02:49 +01:00
5e062cc429 Remove piping of output 2025-01-19 17:59:55 +01:00
0c93030488 Refresh environment 2025-01-19 17:56:58 +01:00
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
6 changed files with 211 additions and 21 deletions

2
.gitignore vendored Normal file
View File

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

View File

@@ -1,6 +1,6 @@
# BeszelWin # BeszelWin
A collection of four scripts to build, install, update or uninstall the [Beszel](https://beszel.dev) agent 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.
## (1) Building the executable ## (1) Building the executable
@@ -22,6 +22,23 @@ Open a `cmd.exe` shell, `cd` into the script directory (where you extracted the
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. 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.
```
C:\Users\User\Downloads\install-beszel-service>build_agent.cmd
This script downloads the Beszel repository and builds the Beszel agent for Windows (64 Bit only).
If not yet installed, Git and Go will be installed via Winget.
Winget executable found
Installing Go with Winget
Installing Git with Winget
Refreshing environment variables from registry for cmd.exe. Please wait...Finished..
Cloning Beszel repository
Building agent
Build successful
Beszel agent.exe was built and copied, you can proceed with 'install-beszel-service.cmd'.
```
## (2) Installing the service ## (2) Installing the service
### Prerequisites ### Prerequisites
@@ -54,6 +71,42 @@ The provided `agent.exe` was compiled on January, 17th 2025 by me and should be
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.
```
C:\Users\User\Downloads\install-beszel-service>install-beszel-service.cmd ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJwRj3l7U9QUJM2K8p2mkH8KDJmhpMwejuPci4LtGxEz
This script installs the Beszel agent for Windows.
If not yet installed, NSSM will be installed via Winget.
Checking adminstrative rights
Administrative rights check OK
Creating directory C:\Program Files\BeszelAgent
Downloading NSSM
Extracting ZIP file
Copying nssm.exe to C:\Program Files\BeszelAgent
Deleting temporary files and directories
Copying agent.exe to C:\Program Files\BeszelAgent
Adding firewall rule
Installing service BeszelAgent
Setting environment for BeszelAgent
Starting BeszelAgent
BeszelAgent: START: The operation completed successfully.
Display service status
NSSM dump:
^"^\^"C:^\Program Files^\BeszelAgent^\nssm.exe^\^"^" install BeszelAgent "C:\Program Files\BeszelAgent\agent.exe"
^"^\^"C:^\Program Files^\BeszelAgent^\nssm.exe^\^"^" set BeszelAgent AppDirectory "C:\Program Files\BeszelAgent"
^"^\^"C:^\Program Files^\BeszelAgent^\nssm.exe^\^"^" set BeszelAgent AppExit Default Restart
^"^\^"C:^\Program Files^\BeszelAgent^\nssm.exe^\^"^" set BeszelAgent AppEnvironmentExtra ":KEY=ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJwRj3l7U9QUJM2K8p2mkH8KDJmhpMwejuPci4LtGxEz"
^"^\^"C:^\Program Files^\BeszelAgent^\nssm.exe^\^"^" set BeszelAgent DisplayName BeszelAgent
^"^\^"C:^\Program Files^\BeszelAgent^\nssm.exe^\^"^" set BeszelAgent ObjectName LocalSystem
^"^\^"C:^\Program Files^\BeszelAgent^\nssm.exe^\^"^" set BeszelAgent Start SERVICE_AUTO_START
^"^\^"C:^\Program Files^\BeszelAgent^\nssm.exe^\^"^" set BeszelAgent Type SERVICE_WIN32_OWN_PROCESS
Wait 5 seconds, check service status
SERVICE_RUNNING
```
## (3) Updating the service ## (3) Updating the service
### Prerequisites ### Prerequisites

130
RefreshEnv.cmd Normal file
View File

@@ -0,0 +1,130 @@
:: 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 .

View File

@@ -5,6 +5,7 @@ echo If not yet installed, Git and Go will be installed via Winget.
echo. echo.
call env.cmd call env.cmd
set REFRESHENV=0
where winget.exe >nul 2>&1 where winget.exe >nul 2>&1
if %errorlevel% NEQ 0 ( if %errorlevel% NEQ 0 (
@@ -17,7 +18,8 @@ if %errorlevel% NEQ 0 (
where go.exe >nul 2>&1 where go.exe >nul 2>&1
if %errorlevel% NEQ 0 ( if %errorlevel% NEQ 0 (
echo Installing Go with Winget echo Installing Go with Winget
winget install GoLang.Go >nul 2>&1 winget install GoLang.Go --accept-source-agreements --accept-package-agreements >nul 2>&1
set REFRESHENV=1
) else ( ) else (
echo Go executable found echo Go executable found
) )
@@ -25,11 +27,14 @@ if %errorlevel% NEQ 0 (
where git.exe >nul 2>&1 where git.exe >nul 2>&1
if %errorlevel% NEQ 0 ( if %errorlevel% NEQ 0 (
echo Installing Git with Winget echo Installing Git with Winget
winget install Git.Git >nul 2>&1 winget install Git.Git --accept-source-agreements --accept-package-agreements >nul 2>&1
set REFRESHENV=1
) else ( ) else (
echo Git executable found echo Git executable found
) )
if %REFRESHENV% EQU 1 call .\RefreshEnv.cmd
echo Cloning Beszel repository echo Cloning Beszel repository
git clone https://github.com/henrygd/beszel.git >nul 2>&1 git clone https://github.com/henrygd/beszel.git >nul 2>&1
if %errorlevel% NEQ 0 ( if %errorlevel% NEQ 0 (
@@ -44,8 +49,10 @@ go build -ldflags "-w -s" . >nul 2>&1
if %errorlevel% NEQ 0 ( if %errorlevel% NEQ 0 (
echo Something went wrong echo Something went wrong
) else ( ) else (
echo Build successful: echo Build successful
dir agent.exe cd /D "%~dp0"
copy /y .\beszel\beszel\cmd\agent\agent.exe . >nul 2>&1
echo Beszel agent.exe was built and copied, you can proceed with 'install-beszel-service.cmd'.
) )
:end :end

View File

@@ -21,30 +21,26 @@ set BESZELKEYPREFIX=%1
set BESZELKEY=%2 set BESZELKEY=%2
call env.cmd call env.cmd
echo Creating directory %DESTINATIONDIR%
md "%DESTINATIONDIR%" >nul 2>&1
where nssm.exe >nul 2>&1 where nssm.exe >nul 2>&1
if %errorlevel% NEQ 0 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/ci/nssm-2.24-101-g897c7ad.zip >nul 2>&1
echo Extracting ZIP file echo Extracting ZIP file
tar.exe -xf %TEMP%\nssm-2.24.zip >nul 2>&1 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 ( copy .\nssm-2.24-101-g897c7ad\win64\nssm.exe "%DESTINATIONDIR%\" >nul 2>&1
copy .\nssm-2.24\win64\nssm.exe "%DESTINATIONDIR%\" >nul 2>&1
) else (
copy .\nssm-2.24\win32\nssm.exe "%DESTINATIONDIR%\" >nul 2>&1
)
set NSSMEXE="%DESTINATIONDIR%\nssm.exe" set NSSMEXE="%DESTINATIONDIR%\nssm.exe"
echo Deleting temporary files and directories echo Deleting temporary files and directories
del "%TEMP%\nssm-2.24.zip" >nul 2>&1 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 directory %DESTINATIONDIR%
md "%DESTINATIONDIR%" >nul 2>&1
echo Copying agent.exe to %DESTINATIONDIR% echo Copying agent.exe to %DESTINATIONDIR%
copy .\agent.exe "%DESTINATIONDIR%\" >nul 2>&1 copy .\agent.exe "%DESTINATIONDIR%\" >nul 2>&1
@@ -66,14 +62,16 @@ if %errorlevel% NEQ 0 (
) )
echo Starting %SERVICENAME% echo Starting %SERVICENAME%
%NSSMEXE% start %SERVICENAME% >nul 2>&1 %NSSMEXE% start %SERVICENAME%
if %errorlevel% NEQ 0 ( if %errorlevel% NEQ 0 (
echo Failed to start service. echo Check output above.
goto end
) )
echo Dumping service state echo NSSM dump:
%NSSMEXE% dump %SERVICENAME% %NSSMEXE% dump %SERVICENAME%
echo Wait 5 seconds, then check service status
ping 127.0.0.1 -n 5 >nul 2>&1
%NSSMEXE% status %SERVICENAME%
goto end goto end
:help :help

View File

@@ -27,7 +27,7 @@ if %errorlevel% NEQ 0 (
where go.exe >nul 2>&1 where go.exe >nul 2>&1
if %errorlevel% NEQ 0 ( if %errorlevel% NEQ 0 (
echo Installing Go with Winget echo Installing Go with Winget
winget install GoLang.Go >nul 2>&1 winget install GoLang.Go
) else ( ) else (
echo Go executable found echo Go executable found
) )
@@ -35,7 +35,7 @@ if %errorlevel% NEQ 0 (
where git.exe >nul 2>&1 where git.exe >nul 2>&1
if %errorlevel% NEQ 0 ( if %errorlevel% NEQ 0 (
echo Installing Git with Winget echo Installing Git with Winget
winget install Git.Git >nul 2>&1 winget install Git.Git
) else ( ) else (
echo Git executable found echo Git executable found
) )