feat: 增加sshpass和expect工具的检查与使用,优化SSH密码输入流程

This commit is contained in:
xudan 2025-06-30 15:01:27 +08:00
parent 78c38add80
commit 665acc7e7f

View File

@ -123,6 +123,24 @@ 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 [信息] 正在创建临时压缩包...
@ -204,32 +222,59 @@ echo [部署] 开始部署到 !current_name! (!current_host!)...
echo 目标路径:!current_path!
echo 用户名:!current_user!
:: 根据认证方式设置命令前缀
if "!current_use_key!"=="true" (
:: 检查是否有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!
)
set scp_cmd=scp -o StrictHostKeyChecking=no
set ssh_cmd=ssh -o StrictHostKeyChecking=no
echo [信息] 使用SSH密钥认证免密登录
) else (
:: 检查是否有sshpass工具
where sshpass >nul 2>&1
:: 尝试使用expect脚本如果可用
where expect >nul 2>&1
if !errorlevel! equ 0 (
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 [信息] 检测到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
)
)
:: 上传文件到远程服务器
echo [信息] 正在上传文件...
!scp_cmd! dist_temp.zip !current_user!@!current_host!:/tmp/
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/
)
if !errorlevel! neq 0 (
echo [错误] 文件上传到 !current_name! 失败
echo [提示] 请检查网络连接和SSH配置
if defined current_pass (
echo [密码提示] 服务器密码: !current_pass!
)
exit /b 1
)
@ -237,7 +282,18 @@ echo [成功] 文件上传到 !current_name! 完成
:: 在远程服务器上解压并部署
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 'Web-AMR项目部署完成文件列表' && ls -la !current_path! || echo '文件列表显示失败,但部署成功'"
if defined use_expect (
:: 为SSH创建expect脚本
echo set timeout 30 > temp_ssh_expect.exp
echo spawn ssh -o StrictHostKeyChecking=no !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 'Web-AMR项目部署完成文件列表' && 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! && rm -r !current_path!/* 2>/dev/null || true && cd !current_path! && unzip -o /tmp/dist_temp.zip && rm /tmp/dist_temp.zip && echo 'Web-AMR项目部署完成文件列表' && ls -la !current_path! || echo '文件列表显示失败,但部署成功'"
)
if !errorlevel! neq 0 (
echo [错误] !current_name! 远程部署失败