RHELのアクティブなサービス一覧を取得する

とりま動けば良いや、って大急ぎで書いたので、汚いのは勘弁。サービスの一覧を取得して、そのサービスファイルを入れたrpmパッケージから変更されたファイルの一覧を作成するスクリプト。

#!/bin/bash

while read line || [ -n "${line}" ]; do
        user=$(echo -n $line | cut -d ' ' -f 1)
        host=$(echo -n $line | cut -d ' ' -f 2)
        port=$(echo -n $line | cut -d ' ' -f 3)
        if [ -z "$user" ]; then
                user="root"
        fi
        if [ -z "$port" ]; then
                port=22
        fi
        if [ -z "$host" ]; then
                exit
        fi
        # guess RHEL7/8
        os_vers=$(ssh -n -o "StrictHostKeyChecking  no" -p ${port} ${user}@${host} '[ -f /etc/os-release ]&& OS_VERS="$(eval $(grep ^VERSION= /etc/os-release); echo ${VERSION})"; echo ${OS_VERS:0:1}')
        if [ "$os_vers" == "8" ] || [ "$os_vers" == "7" ]; then
                ssh -n -o "StrictHostKeyChecking  no" -p ${port} ${user}@${host} "sudo systemctl list-units --no-pager --state=running *.service | grep '^.*.service' | sed 's/^\(.*\)\.service.*/\\1/'" > service_$host.txt
                while read serv || [ -n "${serv}" ]; do
                        echo "service:${serv}" >> changed_conf_$host.txt
                        ssh -n -o "StrictHostKeyChecking  no" -p ${port} ${user}@${host} "sudo rpm -V \$(sudo rpm -qf /usr/lib/systemd/system/$serv.service)" >> changed_conf_$host.txt
                done < service_$host.txt
        else
                ssh -n -o "StrictHostKeyChecking  no" -p ${port} ${user}@${host} 'LV=$(grep /etc/inittab | cut -d: -f2); sudo chkconfig --list | grep ${LV}:on | awk "{print \$1}"' > service_$host.txt
                while read serv || [ -n "${serv}" ]; do
                        echo "service:${serv}" >> changed_conf_$host.txt
                        ssh -n -o "StrictHostKeyChecking  no" -p ${port} ${user}@${host} "sudo rpm -V \$(sudo rpm -qf /etc/rc.d/init.d/$serv)" >> changed_conf_$host.txt
                done < service_$host.txt
        fi

done < host_list.txt