## bonding.sh
#!/bin/bash
#

function printful(){
    echo -e "\033[1;32m$1\033[0m"
}

function printful_file(){
    FILE=$1
    cat $FILE | while read line;do
    printful "$line"
    done
}



function debian_bonding(){
    sudo apt-get install ipcalc -y &> /dev/null && printful "installed ipcalc"|| printful "cannot install ipcalc netmask will be none"
    IN_IP=$(host `hostname -f`|awk '{print $NF}')
    #IP_PREFIX=$(hostname -I|awk -F '.' '{print $1"."$2"."$3}')

    printful "now bonding....."
    sudo cp /etc/network/interfaces /etc/network/interfaces.`date +%Y-%m-%d-%H:%M:%S`.bak
    eths=(eth0 eth1 eth2 eth3 eth4 eth5 eth6)
    eths_10000=()
    eths_1000=()
    for nic in ${eths[@]};do
        sudo /sbin/ethtool $nic 2> /dev/null | grep  10000base &> /dev/null && eths_10000=(${eths_10000[@]} $nic)
        sudo /sbin/ethtool $nic 2> /dev/null | grep  1000base &> /dev/null && eths_1000=(${eths_1000[@]} $nic)
    done
    printful "10000 bonding  ${eths_10000[*]}...."
    # change the original interfaces file.
    printful "eths_10000:${eths_10000[*]}\neths_1000: ${eths_1000[*]}"
    AA=''
    used_eths=$(/sbin/ip a|grep $IN_IP|awk '{print $NF}')
    [ ${#eths_1000[@]} -ge  2 -a ${#eths_1000[@]} -lt 2 ]  && { AA=(${eths_1000[0]} ${eths_1000[1]});printful "1000 bonding  ${eths_1000[*]}...."; }
    [ ${#eths_10000[@]} -ge  2 ] && { AA=(${eths_10000[0]} ${eths_10000[1]});printful "10000 bonding  ${eths_10000[*]}...."; }
    [ "$used_eths" == '' ] && printful 'cannot get used eth nic...' && exit 2
    NETMASK=$(ipcalc `/sbin/ip addr show ${AA[0]} | awk 'NR==3{print $2}'`|grep Netmask|awk '{print $2}')
    [ $? != 0 ] && printful 'bad netmask! please check...' &&exit 1
    [ -z "$NETMASK"  ] && NETMASK=$(ipcalc `/sbin/ip addr show ${AA[1]} | awk 'NR==3{print $2}'` | awk '/Netmask/{print $2}')
    [ -z $NETMASK ] && exit 1
    IP_PREFIX=$(ipcalc `/sbin/ip addr show ${used_eths} | awk 'NR==3{print $2}'`|grep HostMin|awk '{print $2}')

   cat << EOF |sudo tee   /etc/network/interfaces
auto lo
iface lo inet loopback

auto bond0
iface bond0 inet static
    bond_slaves ${AA[0]} ${AA[1]}
    bond_mode 802.3ad
    bond_xmit_hash_policy layer2+3
    bond_miimon 100
    bond_updelay 200
    bond_downdelay 200
    address $IN_IP
    netmask $NETMASK
    gateway $IP_PREFIX

    up ip route add 10.0.0.0/8 via $IP_PREFIX
    down ip route delete 10.0.0.0/8 via $IP_PREFIX
    up ip route add 172.16.0.0/12 via $IP_PREFIX
    down ip route delete 172.16.0.0/12 via $IP_PREFIX
EOF

    printful "the interface file is now"
    printful_file "/etc/network/interfaces"
}

function rhel_bonding() {
    sudo systemctl disable NetworkManager
    command -v ipcalc || sudo yum  install ipcalc -y &> /dev/null && printful "installed ipcalc"|| printful "cannot install ipcalc netmask will be none"
    IN_IP=$(host `hostname -f`|awk '{print $NF}')
    #IP_PREFIX=$(hostname -I|awk -F '.' '{print $1"."$2"."$3}')
    [ $? != 0 ] && printful 'cannot get internal ip' && exit 1

    printful "now bonding....."
    eths=(eth0 eth1 eth2 eth3 eth4 eth5 eth6)
    eths_10000=()
    eths_1000=()
    for nic in ${eths[@]};do
        sudo /sbin/ethtool $nic 2> /dev/null | grep  10000base &> /dev/null && eths_10000=(${eths_10000[@]} $nic)
        sudo /sbin/ethtool $nic 2> /dev/null | grep  1000base &> /dev/null && eths_1000=(${eths_1000[@]} $nic)
    done
    printful "10000 bonding  ${eths_10000[*]}...."
    # change the original interfaces file.
    printful "eths_10000:${eths_10000[*]}\neths_1000: ${eths_1000[*]}"
    AA=''
    used_eths=$(/sbin/ip a|grep $IN_IP|awk '{print $NF}')
    [ ${#eths_1000[@]} -ge  2 -a ${#eths_1000[@]} -lt 2 ]  && { AA=(${eths_1000[0]} ${eths_1000[1]});printful "1000 bonding  ${eths_1000[*]}...."; }
    [ ${#eths_10000[@]} -ge  2 ] && { AA=(${eths_10000[0]} ${eths_10000[1]});printful "10000 bonding  ${eths_10000[*]}...."; }
    [ "$used_eths" == '' ] && printful 'cannot get used eth nic...' && exit 2
    NETMASK=$(ipcalc `/sbin/ip addr show $used_eths | awk 'NR==3{print $2}'` --netmask | awk -F '=' '{print $2}' )
    [ $? != 0 ] && printful 'bad netmask! please check...' &&exit 1
    [ -z "$NETMASK" -o "$NETMASK" = "255.0.0.0" ] && NETMASK=$(ipcalc `/sbin/ip addr show ${AA[1]} | awk 'NR==3{print $2}'` | awk '/Netmask/{print $2}')
    [ -z $NETMASK ] && exit 1
    IP_PREFIX=$(ipcalc $IN_IP $NETMASK --network | sed -r 's/NETWORK=(.*)\.0$/\1.1/')
    # back_up
    for nic in ${AA[@]};do
        mv /etc/sysconfig/network-scripts/ifcfg-$nic /tmp &> /dev/null
        cat << EOF | sudo tee /etc/sysconfig/network-scripts/ifcfg-$nic
DEVICE=$nic
ONBOOT=yes
BOOTPROTO=none
USERCTL=no
MASTER=bond0
SLAVE=yes
EOF
    done
    # add bonding file
    cat << EOF |sudo tee   /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
ONBOOT=yes
TYPE=Ethernet
BOOTPROTO=none
USERCTL=no
BONDING_OPTS="mode=802.3ad xmit_hash_policy=layer2+3 miimon=100 updelay=200 downdelay=200"
IPADDR=$IN_IP
NETMASK=$NETMASK
GATEWAY=$IP_PREFIX
EOF
}
function get_os() {
   operating_system=$(facter|grep 'operatingsystem '|awk '{print $NF}')
   [ $? -ne 0 ] && printful 'cannot get operating system by facter!' && exit 2
   echo $operating_system
}
function main(){
    os=$(get_os)
    echo $os
    if [ "$os" == 'RedHat' ];then
        rhel_bonding
    else if [ "$os" == 'Debian' ];then
        debian_bonding
        fi
    fi
}
main




### check_ping.sh
#!/bin/bash
#
[ -f $1 ] || "echo usage:$0 host.txt"
while read line;do
   ping -c 2 $line -W 1 &> /dev/null
   [ $? -eq 0 ] && echo -e "\e[1;32m$line: ping ok\e[0m"|| echo -e "\e[1;31m$line: ping not ready\e[0m"

done < $1


### check_ssh.sh
#!/bin/bash
#
function check_ssh(){
    ssh   -o ConnectTimeout=3 $1 'ls' &> /dev/null
    [ $? -eq 0 ] && echo $1' ssh ok'|| echo $1' ssh not ready'
}
[ -f $1 ] || { echo "$1 not a file.usage: bash $0 host.txt"; }
for hosts in `grep -v "#" $1`;do
    [ -z $hosts ] || check_ssh $hosts
done < $1