shell備份systeminit

cpu_usage.sh

/usr/bin/vmstat 1 3 | tail -2 | head -1 | awk '{cpu=100-$15;printf ("%d\n", cpu)}'

available_ip.py

#!/usr/bin/env python

for x in range(1200,1300):
    y = (4798 - x * 2.0) / 8.0
    print x,y
[root@ZQWY01-3810-SALT-DXT scripts]# vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0 1839972 122252   6748  19424   38   25    38    25    0    0 12  0 87  1  0	
[root@ZQWY01-3810-SALT-DXT scripts]# cat available_ip.py 
#!/usr/bin/env python
import commands
import re

prompt = "\
    \n#########################\
    \n# Please Input Network! #\
    \n# e.g.  172.28.14.0/24  #\
    \n#########################\
    \n\
    \n                Please: "
while 1:
    n_net = raw_input(prompt)
    n_net_match = re.match('(10|172|192).\d{1,3}.\d{1,3}.0/\d{1,2}', n_net)
    if n_net == "":
        print "           Format error, e.g. 172.28.14.0/24"
    elif n_net_match == None:
        print "           Format error, e.g. 172.28.14.0/24"
    else:
        if n_net[:6] == '10.6.2':
            w_net = '117.121.18.0/24'
        elif n_net[:6] == '10.2.1':
            w_net = '211.154.165.0/24'
        elif n_net[:6] == '10.2.2':
            w_net = '211.154.161.0/24'
        elif n_net[:6] == '10.2.3':
            w_net = '211.154.162.0/24'
        elif n_net[:6] == '10.2.5':
            w_net = '115.182.20.0/24'
        else:
            w_net = 0
        break

(rpmstate, rpmoutput) = commands.getstatusoutput("rpm -q nmap")
if rpmstate != 0:
    commands.getoutput("yum install nmap -y")

#pattern = re.compile(r'\w{1,3}.\w{1,3}.\w{1,3}.\w{1,3}')

def split_n(a_list):
    pattern = re.compile(r'\n')
    split_n = pattern.split(a_list)
    return split_n

def split_point(a_ip):
    pattern = re.compile(r'\.')
    split_ip = pattern.split(a_ip)
    return split_ip

def total_list(a_net):
    net_len = a_net[:len(a_net)-4]
    total_list = []
    for i in range(1,255):
        net_len_i = net_len + str(i)
        total_list.append(net_len_i)
    return total_list

def available_list(a_net):
    active_list = split_n(commands.getoutput("nmap -n -sP " + a_net + " | grep 'for' | awk '{print $NF}'"))
    total_ip_list = total_list(a_net)
    for ip in active_list:
        if ip in total_ip_list:
            total_ip_list.remove(ip)
    return total_ip_list

n_available_list = available_list(n_net)
if w_net != 0:
    w_available_list = available_list(w_net)

def ip_3list(a_net):
    ip_3list = []
    match = re.match('(10|172|192).\d{1,3}.\d{1,3}.0/\d{1,2}', a_net)
    if match == None:
        a_list = w_available_list
    else:
        a_list = n_available_list
    for ip in a_list:
        ip_3 = split_point(ip)[3]
        ip_3list.append(ip_3)
    return ip_3list

def ip_012(a_net):
    ip_012 = split_point(a_net)[0] + '.' + split_point(a_net)[1] + '.' +  split_point(a_net)[2] + '.'
    return ip_012

if w_net != 0:
    pair_list = []
    w_ip_3list = ip_3list(w_net)
    w_ip_012 = ip_012(w_net)
    for n_ip in n_available_list:
        n_ip_3 = split_point(n_ip)[3]
        if n_ip_3 in w_ip_3list:
            pair_list.append(n_ip + "  <= Pair =>  " + w_ip_012 + n_ip_3)
    
    pair_6list = []
    for pair_ip in pair_list:
        ip_6 = split_point(pair_ip)[6]
        pair_6list.append(ip_6)
    
    n_no_pair_list = []
    w_no_pair_list = []
    for nnn in n_available_list:
        nnn3 = split_point(nnn)[3]
        if nnn3 in pair_6list:
            continue
        else:
            n_no_pair_list.append(nnn)
    for www in w_available_list:
        www3 = split_point(www)[3]
        if www3 in pair_6list:
            continue
        else:
            w_no_pair_list.append(www)
    print "\n============ Pair IP ============="
    for pip in pair_list:
        print pip
    print "                                        Summary: %s" % len(pair_list)
    print "\n=========== No Pair NIP ==========="
    for nnpip in n_no_pair_list:
        print nnpip
    print "                  Summary: %s" % len(n_no_pair_list)
    print "\n=========== No Pair WIP ==========="
    for wnpip in w_no_pair_list:
        print wnpip
    print "                  Summary: %s" % len(w_no_pair_list)
else:
    for final_n in n_available_list:
        print final_n
    print "                  Summary: %s" % len(n_available_list)

System_init.sh

 

#!/bin/bash


HW=`ifconfig | grep "HW" | head -n1 | awk '{print $NF}'` 2>/dev/null 1>/dev/null
IP=`ifconfig | grep "inet addr:" | grep -v "127.0.0.1" | head -n 1 | cut -d: -f 2 | awk '{print $1}'` 2>/dev/null 1>/dev/null
GATE=`ifconfig | grep "inet addr:" | grep -v "127.0.0.1" | head -n 1 | cut -d: -f 2 | awk -F. '{print $1"."$2"."$3".1"}'` 2>/dev/null 1>/dev/null


#################### selinux and iptables ###################
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
#/etc/init.d/iptables stop 2>/dev/null 1>/dev/null


####################  eth0  ####################
mod_eth0 ()
{
cat << EOF1 > /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
TYPE=Ethernet
HWADDR=$HW
IPADDR=$IP
NETMASK=255.255.255.0
GATEWAY=$GATE
EOF1
echo '---eth0 OK---'
}


##################### Hostname ###################
mod_hostname ()
{
read -p 'please input hostname: ' name
cat << EOF > /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=$name
NETWORKING_IPV6=no
EOF
hostname $name
echo "---hostname OK---"
}



############################## intranet #############################
intranet ()
{
mod_eth0
mod_hostname

####################  ROUTE  ################
cat << EOF > /sbin/ifup-local
#!/bin/bash
if [ "\$1" = "eth0" ];then
ip route add 172.28.0.0/16 via $GATE
ip route add 172.16.0.0/16 via $GATE
ip route add 192.168.0.0/16 via $GATE
ip route add 10.2.0.0/16 via $GATE
ip route add 10.5.0.0/16 via $GATE
ip route add 10.6.0.0/16 via $GATE
fi
EOF
chmod 755 /sbin/ifup-local
echo "---route OK---"
}



########################### internet ###########################
internet ()
{
mod_eth0
mod_hostname

###############  DNS  #############
cat << EOF > /etc/resolv.conf
nameserver 202.106.186.166
nameserver 202.106.186.169
nameserver 211.154.165.15
EOF
echo "---DNS OK---"


start_udev 2> /dev/null 1> /dev/null

ipmac=`ifconfig eth0 | grep H | awk '{print toupper($5)}'`
udevmac=`cat /etc/udev/rules.d/70-persistent-net.rules | grep eth0 | awk -F\" '{print toupper($8)}'`

if [ "$ipmac" = "$udevmac" ];then

##################  eth1  ##################
IP1=`ifconfig eth1 | grep inet | awk '{print $2}' | cut -d: -f2`
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
TYPE=Ethernet
IPADDR=${IP1}
NETMASK=255.255.255.0
EOF
echo "---eth1 OK---"

####################  ROUTE  ################
GATE1=`ifconfig eth1 | grep inet | awk '{print $2}' | cut -d: -f2 | awk -F. '{print $1"."$2"."$3".1"}'`
cat << EOF > /sbin/ifup-local
#!/bin/bash
if [ "\$1" = "eth1" ];then
    ip route add 172.28.0.0/16 via $GATE1
    ip route add 172.16.0.0/16 via $GATE1
    ip route add 192.168.0.0/16 via $GATE1
    ip route add 10.2.0.0/16 via $GATE1
    ip route add 10.5.0.0/16 via $GATE1
    ip route add 10.6.0.0/16 via $GATE1
fi
EOF
chmod 755 /sbin/ifup-local
echo "---route OK---"

fi
}



########################## salt ##########################
install_salt ()
{
################# Yum Install Salt-Minion  ####################################
# Ssh Client Setting
sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/ssh_config

yum clean all
yum makecache && yum install salt-minion -y
#yum install glibc -y

sed -e '/^master/d' -e '/^#master:/a master: salt.ourgame.com' -e '/^id:/d' -e '/^#id:/a id: '`ifconfig | grep "inet addr:" | grep -v "127.0.0.1" | tail -n 1 | cut -d: -f 2 | awk '{print }'`'' -i /etc/salt/minion;service salt-minion restart;chkconfig salt-minion on
}



############################### agent ####################################
install_agent ()
{
################# Zabbix-agent ###########################
rpm -q zabbix-agent 2> /dev/null 1> /dev/null
if [ $? -eq 0 ];then
    echo "agent-installed"
else
    yum install -y zabbix20-agent.x86_64

    chkconfig zabbix-agent on

    sed -e '/^Server=/c Server=192.168.2.36' -e '/^ServerActive=/c ServerActive=192.168.2.36' -e '/^Hostname=/c Hostname='`ifconfig | grep "inet addr:" | grep -v "127.0.0.1" | tail -n 1 | cut -d: -f 2 | awk '{print }'`'' -i /etc/zabbix/zabbix_agentd.conf

    /etc/init.d/zabbix-agent restart
fi
}



####################### salt restart #####################################
salt_restart ()
{
rm -fr /etc/salt/pki/minion/*  2>/dev/null 1>/dev/null
sed -e '/^id:/d' -e '/^#id:/a id: '$HOSTNAME'' -i /etc/salt/minion
/etc/init.d/salt-minion restart 2>/dev/null 1>/dev/null
sleep 1
/etc/init.d/salt-minion restart 2>/dev/null 1>/dev/null
}



################################# bonding_trunk ################################
bonding_trunk ()
{
read -p "Please input -->> netA netB vlanID bondIP MASK :  " netA netB vlanID bondIP MASK


cat << EOF > /etc/modprobe.d/bonding.conf
alias bond0 bonding
EOF


sed -e 's/^IPADDR/#IPADDR/' -e 's/^PREFIX/#PREFIX/' -e 's/^NETMASK/#NETMASK/' -e 's/^GATEWAY/#GATEWAY/' -e 's/dhcp/static/' -e 's/^ONBOOT=no/ONBOOT=yes/' -e '$a MASTER=bond0\nSLAVE=yes' -i /etc/sysconfig/network-scripts/ifcfg-{$netA,$netB}


cat << EOF > /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE="bond0"
BOOTPROTO="static"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
IPV6INIT=no
BONDING_OPTS="miimon=100 mode=4"
EOF

sed -e 's/bond0/&.'$vlanID'/' -e '$a VLAN=yes\nIPADDR='$bondIP'\nNETMASK='$MASK'\nGATEWAY='${bondIP%.*}.1'' -e '/^BONDING/d' /etc/sysconfig/network-scripts/ifcfg-bond0 > /etc/sysconfig/network-scripts/ifcfg-bond0.$vlanID
echo "######################### ---bonding_trunk OK--- ##############################"
}



###################### snmpd_log #########################
snmpd_log ()
{
cat << EOF > /etc/sysconfig/snmpd
# snmpd command line options
# OPTIONS="-LS0-6d -Lf /dev/null -p /var/run/snmpd.pid"
OPTIONS="-LS 0-4 d -Lf /dev/null -p /var/run/snmpd.pid -a"
EOF
chmod +x /etc/sysconfig/snmpd
echo "########### ---modified snmpd--- ############"
}




######################## repo #############################
yum_repo ()
{
cd /etc/yum.repos.d/
rm -fr *
wget http://192.168.3.10/yum/intranet/6/CentOS6-Base.repo
wget http://192.168.3.10/yum/intranet/6/epel6.repo
}



snmpd_log
/etc/init.d/rsyslog restart;chkconfig rsyslog on

echo -ne "
#######################################################################
    Input: 1:intranet | 2:internet | bonding | salt | agent
      
#######################################################################
    Please: "
    read mode

if [ $mode = 1 ];then
    intranet
    /etc/init.d/network restart
    salt_restart
elif [ $mode = 2 ];then
    internet
    /etc/init.d/network restart
    salt_restart
elif [ $mode = 'salt' ];then
    yum_repo
    install_salt
elif [ $mode = 'agent' ];then
    yum_repo
    install_agent
elif [ $mode = 'bonding' ];then
    mod_hostname
    bonding_trunk
else
    echo "please input 1 | 2 | bonding | salt | agent"
fi


#rm -fr /root/System_init.sh

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章