tags:

views:

157

answers:

3

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%5F64/RPMS/

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

A: 

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.

Dennis Williamson
Hey thanks for sort --version-sort tip.
aj
sort -V seems to have a bugcat a | /build/toolchain/lin32/coreutils-8.2/bin/sort -Vkernel-2.6.18-164.2.1.el5.x86_64.rpmkernel-2.6.18-164.6.1.el5.x86_64.rpmkernel-2.6.18-164.el5.x86_64.rpmThe result should bekernel-2.6.18-164.el5.x86_64.rpmkernel-2.6.18-164.2.1.el5.x86_64.rpmkernel-2.6.18-164.6.1.el5.x86_64.rpm
aj
It doesn't actually understand version numbers. It has no way of knowing that "el5" should come before "2.1.el5". The main thing it knows is that, for example, "a10z", "a1z", "a2z" is alpha order and "a1z", "a2z", "a10z" is version order. In this example, "az" would sort at the end in either case.
Dennis Williamson
+1  A: 

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
I gave you a vote for the "wget -O- -q <url>" cli. I was to lazy to read the man page for wget.
+1  A: 

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.

Great. Thanks a lot.
aj
this does not work because sort is not doing the right thing. See my comment for Dennis Williamson
aj
the answer is in my post, for better readability. Hope it helps.
I ended up using your initial solution. But while sorting removed the $ARCH.rpm from the name-version.
aj