jeudi 6 septembre 2018

Bash if / for two different OS's

I have a script that is collecting inventory information on linux machines. Unfortunately my company runs two different flavors of linux (Maipo / Santiago), which kick out two different formats of output on some of the commands. Specifically the eth0 IP addresses on each machine.

In this case, what i'm trying to do is add an if / then statement to match a REGEX pattern, and if it does, then perform this action, and append the output to the previous output.

Here is the script:

#!/bin/sh
#Version 1.1
## This is a script to collect all machine information for system inventory. The goal is deploy this to all linux machines via splunk, and compile a true inventory.

SERVERNAME=`uname -n | awk -F "." '{ print $1 }'`
PROC_COUNT=`cat /proc/cpuinfo | grep processor | wc -l`
SOCKETS=`cat /proc/cpuinfo | egrep 'physical id' | sort -u | wc -l`
CORES=`cat /proc/cpuinfo | egrep 'cpu cores' | sort -u | awk '{ print $4 }'`
UPTIME=` uptime |awk '{print $3" "$4" "$5}'`
MEMKB=`cat /proc/meminfo | grep MemTotal | awk '{ print $2 }'`
MEMORY=`expr $MEMKB / 1048576`
OS=`uname -s`
OSVERSION=`cat /etc/redhat-release  |awk '{print $0}'`
KERNAL=`uname -a | awk '{ print \$3 }'`
OS_MANUFACTURER=`cat /etc/redhat-release  |awk '{print $1" "$2}'`
pat1=".*Santiago.*"
pat2=".*Maipo"



SERIAL_NUMBER=`dmidecode -s system-serial-number`
SERVER_TYPE=''
    if [[ $SERIAL_NUMBER =~ ^VMwa ]]; then
        SERVER_TYPE="Virtual"
            elif [[ $SERIAL_NUMBER =~ ^ec2 ]]; then
        SERVER_TYPE="Amazon EC2"
            else SERVER_TYPE="Physical"
    fi

SPEED=`cat /proc/cpuinfo | grep MHz | tail -1 | awk '{ print $4 }'`
CPU_TYPE=`cat /proc/cpuinfo | grep '^model name' | awk -F: '{ print $2 }' | tail -1`
DISKS=`lsblk -ibo KNAME,TYPE,SIZE,MODEL | grep disk | wc -l`
MODEL=`lshal | grep -i 'system.hardware.product' | awk -F\' '{ print $2 }'`
MANUFACTURER=`lshal | grep -i 'system.hardware.vendor' | awk -F\' '{ print $2 }'`
INSTALL_DATE=`rpm -qi basesystem | grep -i "Install Date" | awk '{ print $3" "$4" "$5" "$6" "$7" "$8" "$9 }'`
TIMESTAMP=`date +"%F %T %Z"`

echo -n "$TIMESTAMP | serverName=$SERVERNAME | serverType=$SERVER_TYPE | procCount=$PROC_COUNT | sockets=$SOCKETS | cores=$CORES | uptime=\"$UPTIME\" | memory=$MEMORY | os=$OS | osVersion=\"$OSVERSION\" | kernalVersion=$KERNAL | osManufacturer=\"$OS_MANUFACTURER\" | SN=\"$SERIAL_NUMBER\" | cpuSpeed=$SPEED | cpuType=\"$CPU_TYPE\" | diskCount=$DISKS | model=\"$MODEL\" | manufacurer=\"$MANUFACTURER\" | installDate=\"$INSTALL_DATE\" | "

#Ethernet Card Details

num=1
    if [[ "$OS_VERSION" =~ $pat1 ]]; then
        for i in `/sbin/ifconfig -a |grep -i -B 3 "UP" |grep -i "HWaddr"|awk '{print $1}'`
            do {
                MacAddress=`/sbin/ifconfig $i |grep -i -B 3 "UP" |grep -i "HWaddr"| awk '{print $NF}'`
                Ip_Address=`/sbin/ifconfig $i|grep -i "inet addr"|awk '{print $2}'|awk -F: '{print $2}'`
                echo  "ethName$num=$i | ethMac$num=\"$MacAddress\" | ethIp$num=\"$Ip_Address\" | "
                num=$(expr $num+1|bc)
                }
            done;
    elif [[ "$OS_VERSION" =~ $pat2 ]]; then
        for i in `/sbin/ifconfig -a | grep -i  "flags" | awk '{print $1}' | sed 's/:$//' | grep -v "lo"`
             do
                {
                 MacAddress=`/sbin/ifconfig $i |grep -i  "ether" | awk '{ print $2 }'`
                 Ip_Address=`/sbin/ifconfig $i | grep -i inet | awk '{ print $2 }'`
                 echo  "ethName$num=$i | ethMac$num=\"$MacAddress\" | ethIp$num=\"$Ip_Address\" | "
                 num=$(expr $num+1|bc)
                }
             done;
    fi
echo "";
exit

When I run it, the output looks like this:

2018-09-06 12:57:20 EDT | serverName=MyServer | serverType=Virtual | procCount=4 | sockets=2 | cores=2 | uptime="11 days, 8:23," | memory=15 | os=Linux | osVersion="Red Hat Enterprise Linux Server release 7.5 (Maipo)" | kernalVersion=3.10.0-862.9.1.el7.x86_64 | osManufacturer="Red Hat" | SN="MySN" | cpuSpeed=2700.000 | cpuType=" Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" | diskCount=4 | model="" | manufacurer="" | installDate="Fri 09 Feb 2018 01:49:32 PM EST" |

Notice there is nothing after the last |. That's where the IP address portion should be but it doesn't print there. I have stripped out just the #Ethernet Card Details section, and it runs successfuly on both versions of RHEL. I just can't seem to get it to append the IP addresses. Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire