# esx setup
# example on how to set up ESX.  We setup ssh and also add a basic
# VLAN configuration


if [ "$(uname -s|tr 'A-Z' 'a-z')" = "linux" ];then
   str_dir_name=`dirname $0`
   . $str_dir_name/xcatlib.sh
fi

logger -t xcat -p local4.info setupesx
# Enable SSH access to root and exchange keys

sed -e 's/PermitRootLogin no/PermitRootLogin yes/' /etc/ssh/sshd_config > /etc/ssh/sshd_config.new
mv -f /etc/ssh/sshd_config.new /etc/ssh/sshd_config
#/etc/init.d/sshd restart
restartservice ssh

if [ -r /etc/ssh/sshd_config ]
then
        logger -t xcat -p local4.info "Install: setup /etc/ssh/sshd_config"
        cp /etc/ssh/sshd_config /etc/ssh/sshd_config.ORIG
        sed -i 's/^X11Forwarding .*$/X11Forwarding yes/' /etc/ssh/sshd_config
        sed -i 's/^KeyRegenerationInterval .*$/KeyRegenerationInterval 0/' /etc/ssh/sshd_config
        sed -i 's/\(.*MaxStartups.*\)/#\1/' /etc/ssh/sshd_config
        echo "MaxStartups 1024" >>/etc/ssh/sshd_config
        echo "PasswordAuthentication no" >>/etc/ssh/sshd_config
fi

if [ -r /etc/ssh/sshd_config ]
then
   echo "   StrictHostKeyChecking no" >> /etc/ssh/ssh_config
fi

if [ -d /xcatpost/_ssh ]
then
        logger -t xcat -p local4.info "Install: setup root .ssh"
        cd /xcatpost/_ssh
        mkdir -p /root/.ssh
        cp -f * /root/.ssh
   cd - >/dev/null
        chmod 700 /root/.ssh
        chmod 600 /root/.ssh/*
fi


# create a script that will launch the first time ESX does and configure
# the network
cat >/tmp/esxcfg.sh <<EOF1
#!/bin/bash
# Configure ESX Server.  You'll need to put your own IP address
# in here. We assume eth0 is your nic.  Change if needed.
NIC=eth0
#IPADDR=`ifconfig $NIC |grep "inet addr"|awk '{print $2}' |awk -F: '{print $2}' | head -1`
IPADDR=`ip -4 -oneline addr show $NIC|grep inet|awk -F ' ' '{print $4}'|awk -F '/' '{print $1}'`
#NETMASK=`ifconfig $NIC |grep "inet addr"|awk '{print $4}' |awk -F: '{print $2}'`
NETMASK=`ip -4 -oneline addr show $NIC|grep inet|awk -F ' ' '{print $4}'|awk -F '/' '{print $2}'`
NETMASK=`v4prefix2mask $NETMASK`

#esxcfg-vswitch -U vmnic0 vSwitch0
esxcfg-vswitch -L vmnic0 vSwitch0
esxcfg-vswif -i \$IPADDR -n \$NETMASK vswif0
esxcfg-firewall -e sshClient

esxcfg-firewall -e ntpClient
esxcfg-firewall -o 123,udp,out,ntpServer
esxcfg-firewall -o 514,udp,out,syslog
esxcfg-firewall -l
logger -t xcat -p local4.info "Install: restart syslog"
#service syslog restart
restartservice syslog

EOF1

chmod 755 /tmp/esxcfg.sh
cp /etc/rc.d/rc.local /etc/rc.d/rc.local.bak

cat >>/etc/rc.d/rc.local <<EOF
/tmp/esxcfg.sh
mv -f /etc/rc.d/rc.local.bak /etc/rc.d/rc.local
EOF
