Code:
#!/bin/bash
SOURCE_FILE=/home/user/scripts/input.csv
if test -f "$SOURCE_FILE"; then
echo "$SOURCE_FILE exists"
fi
REPORT_FILE="/home/user/scripts/Report_$(date +"%F_%T").csv"
echo $REPORT_FILE
UNREACHABLE="255"
INVALID_PASSWORD="5"
NO_HOME_DIRECTORY="2"
PASSWORD_EXPIRED="1"
FLAVOUR=""
HOSTNAME=""
touch $REPORT_FILE
echo "Input Hostname,\
Input IP,\
SSH Access,\
Tag,\
OS Type,\
Hostname,\
IP Adress,\
Uptime,\
ACTIVE KERNEL,\
INACTIVE KERNELS" >> $REPORT_FILE
exec < $SOURCE_FILE || exit 1
read header # read (and ignore) the first line
while IFS=, read -r InputHost\
InputIP\
Tag\
UserAgentTag\
ResourceProvisioner\
OSType\
OSVersion\
OSDetail\
Location\
PowerState\
; do
sshpass -p '********' ssh -qno StrictHostKeyChecking=no -o ConnectTimeout=1 userlogin@$InputIP 'ls -l /home/userlogin >/dev/null 2>&1' > /dev/null 2>&1
status="$(echo $?)"
echo "processing $InputHost"
FLAVOUR=$(sshpass -p '********' ssh -qno StrictHostKeyChecking=no -o ConnectTimeout=1 userlogin@$InputIP 'cat /etc/redhat-release 2>/dev/null || lsb_release -a 2>/dev/null | grep Description | cut -f2')
HOSTNAME=$(sshpass -p '********' ssh -qno StrictHostKeyChecking=no -o ConnectTimeout=1 userlogin@$InputIP 'hostname')
IPADDRESS=$(sshpass -p '********' ssh -qno StrictHostKeyChecking=no -o ConnectTimeout=1 userlogin@$InputIP 'hostname -I')
ACTIVE_KERNEL=$(sshpass -p '********' ssh -qno StrictHostKeyChecking=no -o ConnectTimeout=1 userlogin@$InputIP 'uname -r')
INACTIVE_KERNEL=$(sshpass -p '********' ssh -qno StrictHostKeyChecking=no -o ConnectTimeout=1 userlogin@$InputIP 'rpm -qa | grep '^kernel-[0-9]' |grep -vE `uname -r`')
UPTIME=$(sshpass -p '********' ssh -qno StrictHostKeyChecking=no -o ConnectTimeout=1 userlogin@$InputIP 'uptime | cut -d "," -f 1')
if [ "$status" == "$UNREACHABLE" ]
then
echo "$InputHost,\
$InputIP,\
cannot connect,\
$Tag,\
$FLAVOUR,\
$HOSTNAME,\
$IPADDRESS,\
$UPTIME,\
"$ACTIVE_KERNEL,\
$INACTIVE_KERNEL >> $REPORT_FILE
elif [ "$status" == "$INVALID_PASSWORD" ]
then
echo "$InputHost,\
$InputIP,\
invalid account access,\
$Tag,\
$FLAVOUR,\
$HOSTNAME,\
$IPADDRESS,\
$UPTIME,\
"$ACTIVE_KERNEL,\
$INACTIVE_KERNEL >> $REPORT_FILE
elif [ "$status" == "$NO_HOME_DIRECTORY" ]
then
echo "$InputHost,\
$Tag,\
$FLAVOUR,\
$HOSTNAME,\
$IPADDRESS,\
$UPTIME,\
"$ACTIVE_KERNEL,\
$INACTIVE_KERNEL >> $REPORT_FILE
elif [ "$status" == "$PASSWORD_EXPIRED" ]
then
echo "$InputHost,\
$InputIP,\
password expired,\
$Tag,\
$FLAVOUR,\
$HOSTNAME,\
$IPADDRESS,\
$UPTIME,\
"$ACTIVE_KERNEL,\
$INACTIVE_KERNEL >> $REPORT_FILE
else
echo "$InputHost,\
$InputIP,\
successful ssh access,\
$Tag,\
$FLAVOUR,\
$HOSTNAME,\
$IPADDRESS,\
$UPTIME,\
"$ACTIVE_KERNEL,\
$INACTIVE_KERNEL >> $REPORT_FILE
fi
done < $SOURCE_FILE