fix: 优化Web-AMR项目部署脚本,简化密码输入逻辑,移除对sshpass和expect工具的依赖,增强用户提示信息

This commit is contained in:
xudan 2025-07-07 16:46:51 +08:00
parent 0285b59927
commit d6aabaf6ed

View File

@ -1,4 +1,5 @@
@echo off
setlocal enabledelayedexpansion
chcp 65001 > nul
title Web-AMR项目部署脚本
@ -123,24 +124,6 @@ if %errorlevel% neq 0 (
exit /b 1
)
:: 检查是否有sshpass或可替代工具
where sshpass >nul 2>&1
if %errorlevel% neq 0 (
where expect >nul 2>&1
if %errorlevel% neq 0 (
echo.
echo [提示] 为实现密码自动输入,建议安装以下工具之一:
echo 1. sshpass - 用于SSH密码自动输入
echo 安装方法在Git Bash中执行 pacman -S sshpass
echo 2. expect - 用于自动化交互脚本
echo 可通过Cygwin或MSYS2安装
echo.
echo [当前方案] 脚本会将密码复制到剪贴板提示输入密码时按Ctrl+V或右键粘贴
echo.
pause
)
)
:: 创建临时压缩包
echo.
echo [信息] 正在创建临时压缩包...
@ -222,59 +205,32 @@ echo [部署] 开始部署到 !current_name! (!current_host!)...
echo 目标路径:!current_path!
echo 用户名:!current_user!
:: 检查是否有sshpass工具
where sshpass >nul 2>&1
if !errorlevel! equ 0 (
:: 有sshpass工具使用自动密码输入
set scp_cmd=sshpass -p "!current_pass!" scp -o StrictHostKeyChecking=no
set ssh_cmd=sshpass -p "!current_pass!" ssh -o StrictHostKeyChecking=no
echo [信息] 使用sshpass自动输入密码
) else (
:: 没有sshpass工具检查是否为Windows环境
if exist "%windir%\System32\clip.exe" (
:: Windows环境将密码复制到剪贴板
echo !current_pass!| clip
echo [信息] 密码已复制到剪贴板,可直接粘贴
echo [密码] !current_pass!
echo [提示] 如果提示输入密码可以按Ctrl+V或右键粘贴或手动输入上述密码
) else (
echo [提示] 如果提示输入密码,请输入:!current_pass!
)
:: 根据认证方式设置命令前缀
if "!current_use_key!"=="true" (
set scp_cmd=scp -o StrictHostKeyChecking=no
set ssh_cmd=ssh -o StrictHostKeyChecking=no
:: 尝试使用expect脚本如果可用
where expect >nul 2>&1
echo [信息] 使用SSH密钥认证免密登录
) else (
:: 检查是否有sshpass工具
where sshpass >nul 2>&1
if !errorlevel! equ 0 (
echo [信息] 检测到expect工具尝试自动输入密码
:: 创建临时expect脚本
echo set timeout 30 > temp_expect.exp
echo spawn scp -o StrictHostKeyChecking=no dist_temp.zip !current_user!@!current_host!:/tmp/ >> temp_expect.exp
echo expect "password:" >> temp_expect.exp
echo send "!current_pass!\r" >> temp_expect.exp
echo expect eof >> temp_expect.exp
set scp_cmd=expect temp_expect.exp
set use_expect=true
set scp_cmd=sshpass -p "!current_pass!" scp -o StrictHostKeyChecking=no
set ssh_cmd=sshpass -p "!current_pass!" ssh -o StrictHostKeyChecking=no
echo [信息] 使用sshpass自动输入密码
) else (
set scp_cmd=scp -o StrictHostKeyChecking=no
set ssh_cmd=ssh -o StrictHostKeyChecking=no
echo [提示] 如果提示输入密码,请输入:!current_pass!
echo [建议] 安装sshpass工具可以实现自动输入密码
)
)
:: 上传文件到远程服务器
echo [信息] 正在上传文件...
if defined use_expect (
:: 使用expect脚本
!scp_cmd!
if exist temp_expect.exp del temp_expect.exp
) else (
:: 使用普通scp命令
!scp_cmd! dist_temp.zip !current_user!@!current_host!:/tmp/
)
!scp_cmd! dist_temp.zip !current_user!@!current_host!:/tmp/
if !errorlevel! neq 0 (
echo [错误] 文件上传到 !current_name! 失败
echo [提示] 请检查网络连接和SSH配置
if defined current_pass (
echo [密码提示] 服务器密码: !current_pass!
)
exit /b 1
)
@ -282,23 +238,12 @@ echo [成功] 文件上传到 !current_name! 完成
:: 在远程服务器上解压并部署
echo [信息] 正在远程服务器上解压部署...
if defined use_expect (
:: 为SSH创建expect脚本
echo set timeout 60 > temp_ssh_expect.exp
echo spawn ssh -o StrictHostKeyChecking=no !current_user!@!current_host! "mkdir -p !current_path! && echo '[步骤1] 正在清空目标目录...' && cd !current_path! && find . -mindepth 1 -delete 2>/dev/null \|\| rm -rf ./* ./.[^.]* 2>/dev/null \|\| true && echo '[步骤2] 目录清理完成,开始解压文件...' && unzip -q -o /tmp/dist_temp.zip && echo '[步骤3] 文件解压完成,清理临时文件...' && rm /tmp/dist_temp.zip && echo '[步骤4] 验证部署结果...' && \[ -f index.html \] && echo '[成功] Web-AMR项目部署完成' && echo '部署文件列表:' && ls -la !current_path! \|\| echo '[警告] 部署可能存在问题,请检查'" >> temp_ssh_expect.exp
echo expect "password:" >> temp_ssh_expect.exp
echo send "!current_pass!\r" >> temp_ssh_expect.exp
echo expect eof >> temp_ssh_expect.exp
expect temp_ssh_expect.exp
if exist temp_ssh_expect.exp del temp_ssh_expect.exp
) else (
!ssh_cmd! !current_user!@!current_host! "mkdir -p !current_path! && echo '[步骤1] 正在清空目标目录...' && cd !current_path! && find . -mindepth 1 -delete 2>/dev/null || rm -rf ./* ./.[^.]* 2>/dev/null || true && echo '[步骤2] 目录清理完成,开始解压文件...' && unzip -q -o /tmp/dist_temp.zip && echo '[步骤3] 文件解压完成,清理临时文件...' && rm /tmp/dist_temp.zip && echo '[步骤4] 验证部署结果...' && [ -f index.html ] && echo '[成功] Web-AMR项目部署完成' && echo '部署文件列表:' && ls -la !current_path! || echo '[警告] 部署可能存在问题,请检查'"
)
!ssh_cmd! !current_user!@!current_host! "mkdir -p !current_path! && rm -r !current_path!/* 2>/dev/null || true && cd !current_path! && unzip -o /tmp/dist_temp.zip && rm /tmp/dist_temp.zip && echo '部署完成,文件列表:' && ls -la !current_path! || echo '文件列表显示失败,但部署成功'"
if !errorlevel! neq 0 (
echo [错误] !current_name! 远程部署失败
exit /b 1
)
echo [成功] !current_name! Web-AMR项目部署完成!
echo [成功] !current_name! 部署完成!
exit /b 0