开发者

find latest version of rpms from a mirror

开发者 https://www.devze.com 2022-12-13 20:28 出处:网络
I want to write a script to find the latest version of rpm of a given package available from a mirror for eg: http://mirror.centos.org/centos/5/updates/x86_64/RPMS/

I want to write a script to find the latest version of rpm of a given package available from a mirror for eg: http://mirror.centos.org/centos/5/updates/x86_64/RPMS/

The script should be able to run on majority of linux flavors (eg centos, redhat, ubuntu). So yum based solution is not an op开发者_如何学Pythontion. Is there any existing script that does this? Or can someone give me a general idea on how to go about this?


Thx to levislevis85 for the wget cli. Try this:

ARCH="i386"
PKG="pidgin-devel"
URL=http://mirror.centos.org/centos/5/updates/x86_64/RPMS
DL=`wget -O- -q $URL | sed -n 's/.*rpm.>\('$PKG'.*'$ARCH'.rpm\).*/\1/p' | sort | tail -1`
wget $URL/$DL

I Will put my comment here, otherwise the code will not be readable.

Try this:

ARCH="i386"
PKG="pidgin-devel"
URL=http://mirror.centos.org/centos/5/updates/x86_64/RPMS
DL=`wget -O- -q $URL | sed -n 's/.*rpm.>\('$PKG'.*'$ARCH'.rpm\).*<td align="right">\(.*\)-\(.*\)-\(.*\) \(..\):\(..\)  <\/td><td.*/\4 \3 \2 \5 \6 \1/p' | sort -k1n -k2M -k3n -k4n -k5n | cut -d ' ' -f 6 | tail -1`
wget $URL/$DL

What it does is:
wget - get the index file
sed - cut out some parts and put it together in different order. Should result in Year Month Day Hour Minute and Package, like:

2009 Oct 27 01 14 pidgin-devel-2.6.2-2.el5.i386.rpm
2009 Oct 30 10 49 pidgin-devel-2.6.3-2.el5.i386.rpm

sort - order the columns n stays for numerical and M for month
cut - cut out the filed 6
tail - show only last entry

the problem with this could be, if some older package release comes after a newer then this script will also fail. If the output of the site changes, the script will fail. There are always a lot of points where a script could fail.


using wget and gawk

#!/bin/bash
pkg="kernel-headers"
wget -O- -q http://mirror.centos.org/centos/5/updates/x86_64/RPMS | awk -vpkg="$pkg" 'BEGIN{
    RS="\n";FS="</a>"
    z=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",D,"|")
    for(i=1;i<=z;i++){
       date[D[i]]=sprintf("%02d",i)
    }
    temp=0
}
$1~pkg{
    p=$1
    t=$2
    gsub(/.*href=\042/,"",p)
    gsub(/\042>.*/,"",p)
    m=split(t,timestamp," ")
    n=split(timestamp[1],d,"-")
    q=split(timestamp[2],hm,":")
    datetime=d[3]date[d[2]]d[1]hm[1]hm[2]
    if ( datetime >= temp ){
        temp=datetime
        filepkg = p
    }
}
END{
    print "Latest package: "filepkg", date: ",temp
}'

an example run of the above:

linux$ ./findlatest.sh
Latest package: kernel-headers-2.6.18-164.6.1.el5.x86_64.rpm, date:  200911041457


Try this (which requires lynx):

lynx -dump -listonly -nonumbers http://mirror.centos.org/centos/5/updates/x86_64/RPMS/ |
    grep -E '^.*xen-libs.*i386.rpm$' |
    sort --version-sort |
    tail -n 1

If your sort doesn't have --version-sort, then you'll have to parse the version out of the filename or hope that a regular sort will do the right thing.

You may be able to do something similar with wget or curl or even a Bash script using redirections with /dev/tcp/HOST/PORT. The problem with these is that you would then have to parse HTML.

0

精彩评论

暂无评论...
验证码 换一张
取 消