通过脚本检查 yum 连接性?

通过脚本检查 yum 连接性?

当 yum repos 访问出现问题时(例如,如果未配置所需的代理),运行“yum update”之类的命令将等待很长时间并尝试大量不同的镜像。有时这很好,但有时我宁愿快速检查 yum repos 访问是否正常。

有没有办法让 yum 快速检查其连接性并提供一个状态代码来指示是否可以访问远程存储库?

答案1

这是实现此目的的一种方法,其关键是 yum repolist 命令报告的“*Repo-baseurl:”:

# curl -s --dump-header - `yum repolist rhcd -v | grep Repo-baseurl | awk  '{print $2}'` -o  /dev/null

HTTP/1.1 200 OK
Date: Fri, 17 May 2013 09:58:30 GMT
Server: Apache/2.2.3 (CentOS)
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html;charset=ISO-8859-1

具体细节:

 yum repolist rhcd -v
Loading "fastestmirror" plugin
Config time: 0.104
Yum Version: 3.2.22
Loading mirror speeds from cached hostfile
Repo-id     : rhcd
Repo-name   : rhcd
Repo-status : enabled:
Repo-updated: Mon Nov  1 14:37:19 2010
Repo-pkgs   : 2,599
Repo-size   : 3.7 G
Repo-baseurl: http://lochost:81/core_build/il31/centos/5Server/i386/CentOS/

使用 grep 提取 baseurl 并通过管道传输到 awk 获取 url。

使用 curl 的 dump header 选项查看 http 状态:

HTTP/1.1 200 OK
Date: Fri, 17 May 2013 09:58:30 GMT
Server: Apache/2.2.3 (CentOS)
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html;charset=ISO-8859-1

当然,yum 是一个非常好的 Python 程序,所以我想你也可以将它组合成一个 Python 实用程序,导入 yum 的相关部分。

如果没有 reponame,yum repolist 将列出所有 yum 存储库。然后您可以循环处理它们。

答案2

对我来说,以下命令有效:

yum repolist -v | grep Repo-baseurl | awk  '{print $3}'

而不是这个:

yum repolist rhcd -v | grep Repo-baseurl | awk  '{print $2}'

对于多个 yum-repos,您可以执行以下操作:

for repourl in $(yum repolist -v | grep Repo-baseurl | awk  '{print $3}') ; do echo $repourl; curl -s --dump-header - $repourl -o  /dev/null; done

将导致:

http://repo-url-1/
HTTP/1.1 200 OK
Date: Sat, 31 Aug 2019 08:32:35 GMT
Server: Apache/2.2.15 (CentOS)
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html;charset=UTF-8

http://repo-url-2/
HTTP/1.1 200 OK
Date: Sat, 31 Aug 2019 08:32:36 GMT
Server: Apache/2.2.15 (CentOS)
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html;charset=UTF-8

答案3

我有同样的需求(检查代理是否配置正确),尽管上述解决方案给出了提示,但没有一个是完整的解决方案,因为使用 curl 检查不使用 yum 中配置的 http 代理。

首先,可以配置yum代理:

  • proxy=http://<proxy_ip>:<proxy_port>每个仓库添加/etc/yum.repos.d/*.repo

  • 全局地,在 yum.conf 中添加proxy=http://<ip>:<port>http_caching=none(不进行缓存以避免获取过时的 repmod.xml 导致校验和不匹配)

1.首先,获取存储库和 URL 列表:

#Produces a csv with 'repo_id#repo_url' rows
yum repolist enabled -v|egrep 'Repo-id|Repo-baseurl' | paste -sd ' \n' | awk '{printf ("%s#%s\n",$3,$6)}' > repolist.csv

2. 清除 yum 缓存以避免缓存答案

yum clean all

然后对于 repolist.csv 中的每个存储库执行以下操作:

3a. 使用 yum 查询 repo 内容(基本需要 15 秒)

#count packages, outputs: 9352
yum --quiet --disablerepo=* --enablerepo=base list available|wc -l

3b. 或者使用 repoquery 查询特定包(基本需要 7 秒)

# outputs: 1
repoquery --repoid=base --qf "%-20{repoid} %{name}"  openssh-clients|wc -l

答案4

我认为没有通用命令可以检查 yum 连接性。您可以这样做:创建一个测试存储库,/etc/yum.repos.d/test.repo只检查单个位置而不是整个镜像列表,这样可以加快速度。

[test]
name= test
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
enabled = 0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

然后以以下形式输入 yum 命令

yum --disablerepo=* --enablerepo=test list available

如果出现连接错误,则可能是您的互联网连接出现问题。当然,在上面的例子中,centos.org 也可能出现故障,但可能性较小。

相关内容