Compare commits

17 Commits

Author SHA1 Message Date
25ed1ecf9b Update README.md 2025-01-29 09:11:16 +01:00
530e59e3e1 Remove clones repository, add some instructions 2025-01-28 10:20:04 +01:00
a156ae0c08 Update links 2025-01-22 11:22:33 +01:00
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 222 additions and 28 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/*.pdf
/*.zip
*.exe

View File

@@ -1,6 +1,6 @@
# 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/) and a [video](https://youtu.be/O_9wT-5LoHM) on the *Tailscale* channel by Alex Kretschmar.
## (1) Building the executable
@@ -12,7 +12,7 @@ A collection of four scripts to build, install, update or uninstall the [Beszel]
### Compiling
Download and extract [install-beszel-service.zip](https://gitea.elpatron.me/elpatron/Install-Beszel-Agent/releases/download/1.0/install-beszel-service.zip).
Download and extract [install-beszel-service.zip](https://gitea.elpatron.me/elpatron/Install-Beszel-Agent/releases/latest).
Open a `cmd.exe` shell, `cd` into the script directory (where you extracted the archive) and start the build-script:
@@ -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.
```
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
### Prerequisites
@@ -30,8 +47,6 @@ The script will install *Git* and *Go* (if not yet installed) with *Winget*, clo
### 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.
@@ -54,6 +69,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.
```
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
### Prerequisites
@@ -62,8 +113,6 @@ For more details, see [*Using Beszel to monitor Windows*](https://blog.ktz.me/us
### 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
@@ -82,8 +131,6 @@ The update will make a fresh clone of the Beszel GitHub repository, build the ex
### 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

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.
call env.cmd
set REFRESHENV=0
where winget.exe >nul 2>&1
if %errorlevel% NEQ 0 (
@@ -17,7 +18,8 @@ if %errorlevel% NEQ 0 (
where go.exe >nul 2>&1
if %errorlevel% NEQ 0 (
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 (
echo Go executable found
)
@@ -25,11 +27,14 @@ if %errorlevel% NEQ 0 (
where git.exe >nul 2>&1
if %errorlevel% NEQ 0 (
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 (
echo Git executable found
)
if %REFRESHENV% EQU 1 call .\RefreshEnv.cmd
echo Cloning Beszel repository
git clone https://github.com/henrygd/beszel.git >nul 2>&1
if %errorlevel% NEQ 0 (
@@ -44,8 +49,19 @@ go build -ldflags "-w -s" . >nul 2>&1
if %errorlevel% NEQ 0 (
echo Something went wrong
) else (
echo Build successful:
dir agent.exe
echo Build was successful
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'.
rd /s/q .\beszel >nul 2>&1
echo Cloned Beszel repository was deleted as we don´t need it anymore.
echo.
echo You can go ahead with .\install-beszel-service.cmd to install the service.
echo .
echo If Git ond/or Go was installed from this script and you don´t want to keep them, use
echo winget uninstall GoLang.Go
echo winget uninstall Git.Git
echo to remove the corresponding packages.
)
:end
:end

View File

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

View File

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