#!/bin/bash
#
# Create dpscanner and set its pw on UBUNTU, RHEL, OEL & CentOS

PW=$1

#/etc/init.d/sshd restart

OSVERSION=0
UBUNTU=0
AMAZONLINUX=0
AMAZONLINUX2=0
AMAZONLINUX=`grep -c "Amazon Linux AMI" /etc/system-release`
AMAZONLINUX2=`grep -ciE "Amazon Linux release 2|Amazon Linux 2" /etc/system-release`
DPSCANNER=`grep -ic dpscanner /etc/ssh/sshd_config`

if [ -f /etc/redhat-release ];then
OSVERSION=`awk -F \. '{print $1}' /etc/redhat-release | awk '{print $NF}'`
else
if [ -e /usr/bin/lsb_release ];then
UBUNTU=`lsb_release -d | grep -ic ubuntu`
else
echo "probably not Ubuntu."
fi
fi

###########################################
if [ $OSVERSION -eq 6 ];then
for newusers in dpscanner
do
awk -F: '{print $1}' /etc/passwd | grep ^$newusers$
if [ $? -eq 1 ];then
useradd $newusers -d /home/dpscanner -m
usermod -a -G ssh_access $newusers
usermod -a -G wheel $newusers
chage -I -1 -m 7 -M -1 -E -1 $newusers
else
chage -I -1 -m 7 -M -1 -E -1 $newusers
fi
done
echo "dpscanner:$PW" | chpasswd; history -c
if [ $DPSCANNER -eq 0 ];then
cat << EOF >> /etc/ssh/sshd_config
#
Match User dpadmin,dpadmin2,dpadmin3,dpscanner
PermitEmptyPasswords no
PasswordAuthentication yes
### Please insert your configuration above the "Match" line, no configuration can be added after this line.
EOF
##
fi
/etc/init.d/sshd restart
fi

###########################################
if [ $AMAZONLINUX -eq 1 ];then
for newusers in dpscanner
do
awk -F: '{print $1}' /etc/passwd | grep ^$newusers$
if [ $? -eq 1 ];then
useradd $newusers -d /home/dpscanner -m
usermod -a -G ssh_access $newusers
usermod -a -G wheel $newusers
chage -I -1 -m 7 -M -1 -E -1 $newusers
else
chage -I -1 -m 7 -M -1 -E -1 $newusers
fi
done
echo "dpscanner:$PW" | chpasswd; history -c
if [ $DPSCANNER -eq 0 ];then
cat << EOF >> /etc/ssh/sshd_config
#
Match User dpadmin,dpadmin2,dpadmin3,dpscanner
PermitEmptyPasswords no
PasswordAuthentication yes
### Please insert your configuration above the "Match" line, no configuration can be added after this line.
EOF
##
fi
/etc/init.d/sshd restart
fi


###########################################
if [ $UBUNTU -eq 1 ];then
for newusers in dpscanner
do
awk -F: '{print $1}' /etc/passwd | grep ^$newusers$
if [ $? -eq 1 ];then
useradd $newusers -d /home/dpscanner -m
usermod -a -G ssh_access $newusers
usermod -a -G wheel $newusers
chage -I -1 -m 7 -M -1 -E -1 $newusers
else
chage -I -1 -m 7 -M -1 -E -1 $newusers
fi
done
## this is for rhel6 or ubuntu or Amazon Linux
echo "dpscanner:$PW" | chpasswd; history -c
## this is for rhel6 or ubuntu or Amazon Linux
if [ $DPSCANNER -eq 0 ];then
cat << EOF >> /etc/ssh/sshd_config
#
Match User dpadmin,dpadmin2,dpadmin3,dpscanner
PermitEmptyPasswords no
PasswordAuthentication yes
### Please insert your configuration above the "Match" line, no configuration can be added after this line.
EOF
##
/etc/init.d/ssh restart
##
fi
fi

###########################################
if [ $OSVERSION -eq 7 -o $AMAZONLINUX2 -eq 1 ];then
## this is for rhel7 / Amazon Linux 2
for newusers in dpscanner
do
awk -F: '{print $1}' /etc/passwd | grep ^$newusers$
if [ $? -eq 1 ];then
useradd $newusers -d /home/dpscanner -m
usermod -a -G ssh_access $newusers
usermod -a -G wheel $newusers
chage -I -1 -m 7 -M -1 -E -1 $newusers
else
chage -I -1 -m 7 -M -1 -E -1 $newusers
fi
done
## 7.2
echo "dpscanner:$PW" | chpasswd; history -c
## 7.3
if [ $DPSCANNER -eq 0 ];then
cat << EOF >> /etc/ssh/sshd_config
#
Match User dpadmin,dpadmin2,dpadmin3,dpscanner
PermitEmptyPasswords no
PasswordAuthentication yes
### Please insert your configuration above the "Match" line, no configuration can be added after this line.
EOF
systemctl reload sshd
fi
#
fi
