从脚本检查yum连接?

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

有没有办法让百胜快速检查其连接,并给我一个状态代码,指出是否访问远程回购是好的?

这里有一个办法,它的关键是*回购基地:“这是由yum repolist命令报告:

# 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和pipe抽取baseurl到awk的url。

使用curl的转储标题选项来查看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的相关位。

没有命名的yum repolist会列出所有的yum储存库。 然后你可以循环处理它们。

我不认为有一个通用的命令来检查百胜连接。 你可以做的是这样的:在/etc/yum.repos.d/test.repo中创build一个testing/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也可能会下降,但是这个机会很小。