完美解决dotnet-install.sh下载出错

问题现象

Linux正常通过dotnet-install.sh安装dotnet(以dotnet-runtime为例)时,会打印以下信息:

dotnet-install: Note that the intended use of this script is for Continuous Integration (CI) scenarios, where:
dotnet-install: - The SDK needs to be installed without user interaction and without admin rights.
dotnet-install: - The SDK installation doesn't need to persist across multiple CI runs.
dotnet-install: To set up a development environment or to run apps, use installers rather than this script. Visit https://dotnet.microsoft.com/download to get the installer.

dotnet-install: Attempting to download using aka.ms link https://dotnetcli.azureedge.net/dotnet/Runtime/6.0.13/dotnet-runtime-6.0.13-linux-x64.tar.gz

如果显示:

curl: (56) OpenSSL SSL_read: Connection reset by peer, errno 104
dotnet-install: Download attempt #1 has failed: 200 Unable to download https://dotnetcli.azureedge.net/dotnet/Runtime/6.0.13/dotnet-runtime-6.0.13-linux-x64.tar.gz.

说明下载指定文件失败,毕竟是国外源,再加上伟大的长城,也就见怪不怪了。

分析问题

首先搞清楚是本机还是源服务器出了问题。尝试在其他环境下挂魔法下载,如果可以下载,说明只是本地服务器单方面无法连接,源服务器正常。

解决方案

给服务器挂代理肯定是可行的,但是操作比较繁琐,本文通过在Bash中搜索替换下载链接的方式解决。

在脚本中搜索语句Attempting to download using,跳转到1434行(可能会不太一样),显示如下:

say "Attempting to download using $link_type link $download_link"

这是控制台输出固定的语句,简单分析这句话,包含了$link_type$download_link两个变量,其中:

  • $link_type:使用的下载源,不必关注
  • $download_link:使用的下载链接,就是这个链接导致了下载失败

接下来就替换为能正常访问的链接。

从该行开始向上找,找到最近的$download_link赋值语句,就在不远的1429行,显示如下:

download_link="${download_links[$link_index]}"

看起来很复杂,等号后的内容又嵌套了其他变量,但只需把引号内替换为能正常访问的链接就行了。

为了方便,就把源文件(dotnet-runtime-6.0.13-linux-x64.tar.gz)下载后再上传到蓝奏云,下载直链为https://i41.lanzoug.com/0119150098126364bb/2023/01/19/758b5c9030a455df9d69df84075f1b01.gz?st=ZrY_80DQAuRaT-pnxI8kTQ&e=1674117127&b=BTMMYwl9B2xVZVNxUSxTJQkpWmgDdlE_bBjgBaQIoV2MAfFpqVHoFMVQyVC8EOFQ5U2hddAR9Bi0CKQ02UmEDLwUjDG0JewcsVWdTfw_c_c&fi=98126364&pid=223-90-112-230&up=2&mp=0&co=1

替换后原语句就变为:

download_link="https://i41.lanzoug.com/0119150098126364bb/2023/01/19/758b5c9030a455df9d69df84075f1b01.gz?st=ZrY_80DQAuRaT-pnxI8kTQ&e=1674117127&b=BTMMYwl9B2xVZVNxUSxTJQkpWmgDdlE_bBjgBaQIoV2MAfFpqVHoFMVQyVC8EOFQ5U2hddAR9Bi0CKQ02UmEDLwUjDG0JewcsVWdTfw_c_c&fi=98126364&pid=223-90-112-230&up=2&mp=0&co=1"

保存后重新运行脚本,如果显示:

dotnet-install: Attempting to download using aka.ms link https://i41.lanzoug.com/0119150098126364bb/2023/01/19/758b5c9030a455df9d69df84075f1b01.gz?st=ZrY_80DQAuRaT-pnxI8kTQ&e=1674117127&b=BTMMYwl9B2xVZVNxUSxTJQkpWmgDdlE_bBjgBaQIoV2MAfFpqVHoFMVQyVC8EOFQ5U2hddAR9Bi0CKQ02UmEDLwUjDG0JewcsVWdTfw_c_c&fi=98126364&pid=223-90-112-230&up=2&mp=0&co=1

说明下载链接已重定向,可以进行后续安装了。

修改记录

日期 描述
2023.1.19 初版
2023.12.30 内容完善
posted @ 2023-01-19 18:08  NiGhT_Ray  阅读(2197)  评论(1编辑  收藏  举报