autoupdate script

本文记录了一次通过脚本自动化的软件升级过程,包括从远程服务器下载MD5校验文件及新版本软件包、备份和删除旧文件等步骤,并最终确认升级成功。
shell.albert@beijing:~/study/test> ./run.sh
正在搜索升级服务器...
搜索到服务器:172.23.140.177
正在下载MD5文件...
Connected to 172.23.140.177.
220 Welcome to EAVCapture Server.
331 User name OK, need password.
230 You are logged in.
Remote system type is UNIX.
200 Command okay.
local: EAVCapture_Remote.md5 remote: EAVCapture_Remote.md5
227 Entering Passive Mode (172,23,140,177,201,196).
150 File status okay; about to open data connection.
    60      689.33 KiB/s
226 Closing data connection.
60 bytes received in 00:00 (1.47 KiB/s)
221 Quitting...
LocalMD5: 376c05132991a0adcc2f7469835d7ca6
RemoteMD5: 376c05132991a0adcc2f7469835d7c46
正在下载新版本文件
Connected to 172.23.140.177.
220 Welcome to EAVCapture Server.
331 User name OK, need password.
230 You are logged in.
Remote system type is UNIX.
200 Command okay.
local: EAVCapture_Remote.tar.bz2 remote: EAVCapture_Remote.tar.bz2
227 Entering Passive Mode (172,23,140,177,201,197).
150 File status okay; about to open data connection.
  9005        1.96 MiB/s
226 Closing data connection.
9005 bytes received in 00:00 (220.28 KiB/s)
221 Quitting...
正在备份当前版本文件
EAVCapture_Local.md5
EAVCapture_Remote.md5
EAVCapture_Remote.tar.bz2
run.sh
zserverfind.bin
zserverfind.c
正在删除旧版本文件...
run.sh
zserverfind.bin
zserverfind.c
升级成功!

PS C:\Users\Administrator> # CurlTools.psm1 >> # ============== >> # 修复版模块主文件 >> >> # 初始化模块变量 >> $script:Config = $null >> $script:CurlPath = $null >> $script:ConfigDir = $null >> $script:ConfigPath = $null >> >> # 检测操作系统 >> function Get-Platform { >> if ($PSVersionTable.PSEdition -eq "Core") { >> if ($IsWindows) { return "Windows" } >> elseif ($IsLinux) { return "Linux" } >> elseif ($IsMacOS) { return "macOS" } >> } else { >> return "Windows" >> } >> } >> >> # 获取跨平台配置目录 >> function Get-ConfigDir { >> $platform = Get-Platform >> switch ($platform) { >> "Linux" { >> if (-not (Test-Path "$env:HOME/.config")) { >> New-Item -ItemType Directory -Path "$env:HOME/.config" -Force | Out-Null >> } >> return "$env:HOME/.config/curltools" >> } >> "macOS" { >> if (-not (Test-Path "$env:HOME/Library/Application Support")) { >> New-Item -ItemType Directory -Path "$env:HOME/Library/Application Support" -Force | Out-Null >> } >> return "$env:HOME/Library/Application Support/CurlTools" >> } >> default { >> if (-not (Test-Path $env:APPDATA)) { >> New-Item -ItemType Directory -Path $env:APPDATA -Force | Out-Null >> } >> return "$env:APPDATA\CurlTools" >> } >> } >> } >> >> # 增强版路径检测 >> function Get-DefaultCurlPath { >> $platform = Get-Platform >> switch ($platform) { >> "Linux" { >> if (Test-Path "/usr/bin/curl") { return "/usr/bin" } >> return $null >> } >> "macOS" { >> if (Test-Path "/usr/local/bin/curl") { return "/usr/local/bin" } >> return $null >> } >> default { >> # 尝试自动检测Windows上的curl路径 >> $potentialPaths = @( >> "C:\Program Files\curl\bin", >> "C:\curl\bin", >> "$env:USERPROFILE\curl\bin", >> "E:\curl-8.15.0_4-win64-mingw\bin", >> "C:\Windows\System32" # 添加系统路径 >> ) >> >> foreach ($path in $potentialPaths) { >> $curlExe = Join-Path $path "curl.exe" >> if (Test-Path $curlExe -PathType Leaf) { >> return $path >> } >> } >> >> # 如果找不到,尝试在PATH中查找 >> $pathDirs = $env:Path -split ';' >> foreach ($dir in $pathDirs) { >> $curlExe = Join-Path $dir "curl.exe" >> if (Test-Path $curlExe -PathType Leaf) { >> return $dir >> } >> } >> >> return $null >> } >> } >> } >> >> # 增强初始化逻辑 >> function Initialize-ModuleConfig { >> $script:ConfigDir = Get-ConfigDir >> >> # 确保配置目录存在 >> if (-not (Test-Path $script:ConfigDir)) { >> New-Item -ItemType Directory -Path $script:ConfigDir -Force | Out-Null >> } >> >> $script:ConfigPath = Join-Path -Path $script:ConfigDir -ChildPath "config.json" >> >> # 加载或创建默认配置 >> if (Test-Path $script:ConfigPath) { >> try { >> $script:Config = Get-Content $script:ConfigPath | ConvertFrom-Json >> } catch { >> Write-Warning "配置加载失败: $_,创建新配置" >> $script:Config = $null >> } >> } >> >> if (-not $script:Config) { >> $defaultPath = Get-DefaultCurlPath >> if (-not $defaultPath) { >> throw "无法找到curl.exe,请确保curl已安装并添加到PATH" >> } >> >> $script:Config = [PSCustomObject]@{ >> CurlPath = $defaultPath >> LastUpdate = (Get-Date).ToString("o") >> AutoUpdate = $true >> } >> $script:Config | ConvertTo-Json | Set-Content $script:ConfigPath -Force >> } >> >> # 设置模块路径并验证 >> $script:CurlPath = $script:Config.CurlPath >> $curlExe = Join-Path $script:CurlPath "curl.exe" >> if (-not (Test-Path $curlExe -PathType Leaf)) { >> throw "curl.exe在路径'$script:CurlPath'中不存在" >> } >> } >> >> # 保存配置 >> function Save-ModuleConfig { >> $script:Config | ConvertTo-Json | Set-Content $script:ConfigPath -Force >> } >> >> # 在模块导入时自动初始化配置 >> try { >> Initialize-ModuleConfig >> } catch { >> Write-Error "模块初始化失败: $_" >> throw >> } >> >> # 其他函数保持不变(Get-FileHashFromUrl, Get-CurlVersion等) >> # ... [保持原有函数代码] ... >> # CurlTools.psm1 # ============== # 修复版模块主文件 # 初始化模块变量 $script:Config = $null $script:CurlPath = $null $script:ConfigDir = $null $script:ConfigPath = $null # 检测操作系统 function Get-Platform { if ($PSVersionTable.PSEdition -eq "Core") { if ($IsWindows) { return "Windows" } elseif ($IsLinux) { return "Linux" } elseif ($IsMacOS) { return "macOS" } } else { return "Windows" } } # 获取跨平台配置目录 function Get-ConfigDir { $platform = Get-Platform switch ($platform) { "Linux" { if (-not (Test-Path "$env:HOME/.config")) { New-Item -ItemType Directory -Path "$env:HOME/.config" -Force | Out-Null } return "$env:HOME/.config/curltools" } "macOS" { if (-not (Test-Path "$env:HOME/Library/Application Support")) { New-Item -ItemType Directory -Path "$env:HOME/Library/Application Support" -Force | Out-Null } return "$env:HOME/Library/Application Support/CurlTools" } default { if (-not (Test-Path $env:APPDATA)) { New-Item -ItemType Directory -Path $env:APPDATA -Force | Out-Null } return "$env:APPDATA\CurlTools" } } } # 增强版路径检测 function Get-DefaultCurlPath { $platform = Get-Platform switch ($platform) { "Linux" { if (Test-Path "/usr/bin/curl") { return "/usr/bin" } return $null } "macOS" { if (Test-Path "/usr/local/bin/curl") { return "/usr/local/bin" } return $null } default { # 尝试自动检测Windows上的curl路径 $potentialPaths = @( "C:\Program Files\curl\bin", "C:\curl\bin", "$env:USERPROFILE\curl\bin", "E:\curl-8.15.0_4-win64-mingw\bin", "C:\Windows\System32" # 添加系统路径 ) foreach ($path in $potentialPaths) { $curlExe = Join-Path $path "curl.exe" if (Test-Path $curlExe -PathType Leaf) { return $path } } # 如果找不到,尝试在PATH中查找 $pathDirs = $env:Path -split ';' foreach ($dir in $pathDirs) { $curlExe = Join-Path $dir "curl.exe" if (Test-Path $curlExe -PathType Leaf) { return $dir } } return $null } } } # 增强初始化逻辑 function Initialize-ModuleConfig { $script:ConfigDir = Get-ConfigDir # 确保配置目录存在 if (-not (Test-Path $script:ConfigDir)) { New-Item -ItemType Directory -Path $script:ConfigDir -Force | Out-Null } $script:ConfigPath = Join-Path -Path $script:ConfigDir -ChildPath "config.json" # 加载或创建默认配置 if (Test-Path $script:ConfigPath) { try { $script:Config = Get-Content $script:ConfigPath | ConvertFrom-Json } catch { Write-Warning "配置加载失败: $_,创建新配置" $script:Config = $null } } if (-not $script:Config) { $defaultPath = Get-DefaultCurlPath if (-not $defaultPath) { throw "无法找到curl.exe,请确保curl已安装并添加到PATH" } $script:Config = [PSCustomObject]@{ CurlPath = $defaultPath LastUpdate = (Get-Date).ToString("o") AutoUpdate = $true } $script:Config | ConvertTo-Json | Set-Content $script:ConfigPath -Force } # 设置模块路径并验证 $script:CurlPath = $script:Config.CurlPath $curlExe = Join-Path $script:CurlPath "curl.exe" if (-not (Test-Path $curlExe -PathType Leaf)) { throw "curl.exe在路径'$script:CurlPath'中不存在" } } # 保存配置 function Save-ModuleConfig { $script:Config | ConvertTo-Json | Set-Content $script:ConfigPath -Force } # 在模块导入时自动初始化配置 try { Initialize-ModuleConfig } catch { Write-Error "模块初始化失败: $_" throw } # 其他函数保持不变(Get-FileHashFromUrl, Get-CurlVersion等) # ... [保持原有函数代码] ... : 模块初始化失败: 无法将参数绑定到参数“Path”,因为该参数是空值。 + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException Join-Path : 无法将参数绑定到参数“Path”,因为该参数是空值。 所在位置 行:127 字符: 26 + $curlExe = Join-Path $script:CurlPath "curl.exe" + ~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Join-Path],ParentContainsErrorRecordException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCom mand PS C:\Users\Administrator>
最新发布
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值