windows-openssh-to-msys.bat 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. @echo off
  2. ::
  3. :: This script will:
  4. ::
  5. :: 1. install the windows OpenSSH server (either via dsim or download it)
  6. :: 2. activate the windows OpenSSH service
  7. :: 3. open OpenSSH TCP port at windows firewall
  8. :: 4. create a small batch file to start an MSYS session
  9. :: 5. Set the default OpenSSH startup script to start the MSYS session
  10. ::
  11. :: Problems:
  12. :: On older windows versions, terminal emulation is broken.
  13. :: So, on windows 10 or windows server before 2019, the ssh session
  14. :: will not have proper terminal emulation and will be not be able to
  15. :: be used for editing files.
  16. :: For more info check:
  17. :: https://github.com/PowerShell/Win32-OpenSSH/issues/1260
  18. ::
  19. :: Check if OpenSSH Server is already installed
  20. sc query sshd >nul 2>&1
  21. if %errorlevel% neq 0 (
  22. echo "OpenSSH Server not found. Attempting to install via dism..."
  23. goto :install_openssh_dism
  24. ) else (
  25. echo "OpenSSH Server is already installed."
  26. goto :configure_openssh
  27. )
  28. :: Install OpenSSH using dism
  29. :install_openssh_dism
  30. dism /online /Enable-Feature /FeatureName:OpenSSH-Client /All >nul 2>&1
  31. dism /online /Enable-Feature /FeatureName:OpenSSH-Server /All >nul 2>&1
  32. :: Check if dism succeeded in installing OpenSSH
  33. sc query sshd >nul 2>&1
  34. if %errorlevel% neq 0 (
  35. echo "OpenSSH installation via dism failed or is unavailable."
  36. goto :install_openssh_manual
  37. ) else (
  38. echo "OpenSSH installed successfully using dism."
  39. goto :configure_openssh
  40. )
  41. :: Function to Install OpenSSH manually if dism fails
  42. :install_openssh_manual
  43. echo "Installing OpenSSH manually..."
  44. :: Download the latest OpenSSH release
  45. set DOWNLOAD_URL=https://github.com/PowerShell/Win32-OpenSSH/releases/download/v9.5.0.0p1-Beta/OpenSSH-Win64.zip
  46. set DOWNLOAD_FILE=%temp%\OpenSSH-Win64.zip
  47. set INSTALL_DIR=C:\Program Files\OpenSSH-Win64
  48. :: Create the installation directory if it doesn't exist
  49. if not exist "%INSTALL_DIR%" mkdir "%INSTALL_DIR%"
  50. :: Attempt to download OpenSSH using Invoke-WebRequest and TLS configuration
  51. powershell -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; try { Invoke-WebRequest -Uri '%DOWNLOAD_URL%' -OutFile '%DOWNLOAD_FILE%' -UseBasicParsing; exit 0 } catch { exit 1 }"
  52. if %errorlevel% neq 0 (
  53. echo "Invoke-WebRequest download failed. Attempting to download using curl..."
  54. curl -L -o "%DOWNLOAD_FILE%" "%DOWNLOAD_URL%"
  55. if %errorlevel% neq 0 (
  56. echo "Failed to download OpenSSH using curl. Exiting..."
  57. exit /b 1
  58. )
  59. )
  60. :: Unzip directly to INSTALL_DIR (flatten the folder structure)
  61. powershell -Command "Expand-Archive -Path '%DOWNLOAD_FILE%' -DestinationPath '%INSTALL_DIR%' -Force"
  62. if %errorlevel% neq 0 (
  63. echo "Failed to unzip OpenSSH package."
  64. exit /b 1
  65. )
  66. :: Move inner contents to INSTALL_DIR if nested OpenSSH-Win64 folder exists
  67. if exist "%INSTALL_DIR%\OpenSSH-Win64" (
  68. xcopy "%INSTALL_DIR%\OpenSSH-Win64\*" "%INSTALL_DIR%\" /s /e /y
  69. rmdir "%INSTALL_DIR%\OpenSSH-Win64" /s /q
  70. )
  71. :: Add the OpenSSH binaries to the system PATH
  72. setx /M PATH "%INSTALL_DIR%;%PATH%"
  73. :: Register OpenSSH utilities as services using PowerShell
  74. powershell -ExecutionPolicy Bypass -Command "& '%INSTALL_DIR%\install-sshd.ps1'"
  75. :: Verify if manual installation succeeded
  76. sc query sshd >nul 2>&1
  77. if %errorlevel% neq 0 (
  78. echo "Manual OpenSSH installation failed. Exiting..."
  79. exit /b 1
  80. ) else (
  81. echo "OpenSSH installed successfully manually."
  82. goto :configure_openssh
  83. )
  84. :configure_openssh
  85. :: Ensure OpenSSH Server service is set to start automatically and start the service
  86. sc config sshd start= auto
  87. net start sshd
  88. :: Create msys2.bat file with specific content
  89. set MSYS2_PATH=C:\msys64
  90. if not exist "%MSYS2_PATH%" (
  91. echo "Error: %MSYS2_PATH% does not exist."
  92. exit /b 1
  93. )
  94. echo @%MSYS2_PATH%\msys2_shell.cmd -defterm -here -no-start -msys > %MSYS2_PATH%\msys2.bat
  95. :: Run PowerShell command to set default shell
  96. powershell -Command "New-ItemProperty -Path 'HKLM:\SOFTWARE\OpenSSH' -Name 'DefaultShell' -Value '%MSYS2_PATH%\msys2.bat' -PropertyType String -Force"
  97. :: Open the Windows Firewall for sshd (using PowerShell)
  98. powershell -Command "New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd) Incoming' -Description 'Allow incoming SSH traffic via OpenSSH server' -Enabled True -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow"
  99. echo "OpenSSH has been successfully configured with MSYS2 as the default shell, and the firewall has been opened for sshd."
  100. pause