[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: looking for packages versions of running daemons



Israel Garcia <igalvarez@gmail.com> writes:
>On 9/9/09, Cameron Hutchison <lists@xdna.net> wrote:
>> Israel Garcia <igalvarez@gmail.com> writes:
>>
>>>I have more than 10 debian (etch and lenny) servers and I want to find
>>>a way to know remotely on every server:
>>
>>>1. Name of running daemons and ports (tcp/udp) they're using.
>>>2. Version of the package (installed by APT) used by these daemons.
>>>3. Version of the latest package (from deb mirros) used by these daemons.
>>
>>>I tried to make a script but didn't resolve my problem.
>>
>That's really nice. It gives what I'm looking for..BUT, I have other
>daemons installed from source, so dpkg -S returns an error.  In my
>case ruby. See below:

Ok. Here's version 2. Fixes are:
  * Sorted the output by port number and removed duplicates. Duplicates
    happen when a daemon listens on multiple IP addresses (samba is one).
  * Skip non-existent processes
  * remove (delete) from the end of readlink paths. This may happen if a
    package has been upgraded and the old exe deleted.
  * Use argv[0] if its an executable instead of /proc/pid/exe. This
    makes daemons that are running under interpretters (perl, ruby, etc)
    identified properly. In my case, postgrey failed, as a perl process.
  * Ignore dpkg -S errors, and write a shorter line if there is no
    package for the process.

>Ruby was compile from source, How can I modify this script to remove
>this error or better run -v option on daemons not installed by APT.

What do you mean by -v option? If you mean run the exe with -v to get
the version, that could easily fail and do unpredictable things, as -v
is not standardised as a way to get the version of a program.

netstat -lntup \
        | awk '/^tcp/ { print $4"/"$1, $7 } /^udp/ { print $4"/"$1, $6 }' \
        | sed -n 's|^[^ ]*:\([^ ]*\) \([0-9]*\)/.*|\1 \2|p' \
        | sort -nu \
        | while read port pid ; do
                [ -d /proc/$pid ] || continue
                bin=$(xargs -n 1 -0 echo < /proc/$pid/cmdline | awk '{print $1 ; exit}')
                [ -x "$bin" ] || bin=$(readlink /proc/$pid/exe | sed 's/ (deleted)//')
                pkg=$(dpkg -S $bin 2>/dev/null | cut -d: -f1)
                [ -n "$pkg" ] || { echo "$bin on port $port"; continue; }
                version=$(dpkg-query -W --showformat='${Version}' $pkg)
                latest=$version
                latest=$(apt-cache show -a $pkg | grep "^Version:" | { while read x ver ; do
                        if dpkg --compare-versions $latest lt $ver ; then
                                latest=$ver
                        fi
                done ; echo $latest; } )
                echo -n "$bin on port $port from package $pkg (version $version"
                if [ $latest != $version ] ; then
                        echo -n ", $latest available"
                fi
                echo ")"
        done


Reply to: