#!/bin/bash #by caosm #time 20161122 ipaddr(){ echo "===============配置ip地址以及主机名称===========" read -p "请输入你的ip地址:" IPADDR read -p "请输入你的子网掩码:" NETMASK read -p "请输入你的网关地址:" GATEWAY read -p "请输入你的DNS1:" DNS1 sed -i ‘/^IPADDR.*$/d‘ /etc/sysconfig/network-scripts/ifcfg-eth0 sed -i ‘/^NETMASK.*$/d‘ /etc/sysconfig/network-scripts/ifcfg-eth0 sed -i ‘/^GATEWAY.*$/d‘ /etc/sysconfig/network-scripts/ifcfg-eth0 sed -i ‘/^DNS1.*$/d‘ /etc/sysconfig/network-scripts/ifcfg-eth0 sed -i ‘s/ONBOOT=no/ONBOOT=yes/g‘ /etc/sysconfig/network-scripts/ifcfg-eth0 sed -i ‘s/BOOTPROTO=dhcp/BOOTPROTO=static/g‘ /etc/sysconfig/network-scripts/ifcfg-eth0 echo "IPADDR=$IPADDR" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "NETMASK=$NETMASK" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "GATEWAY=$GATEWAY" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo "DNS1=$DNS1" >> /etc/sysconfig/network-scripts/ifcfg-eth0 service network restart >&/dev/null ping -c 3 -w 5 www.baidu.com >&/dev/null if [[ $? != 0 ]];then echo " 不能上网请检查配置 " fi echo "=================================================" echo "" sleep 2 } #HOSTNAME hostname(){ echo "==================配置主机名称====================" read -p "请输入你的主机名称:" HOSTNAME sed -i ‘3,$d‘ /etc/hosts sed -i ‘2,$d‘ /etc/sysconfig/network sed -i ‘/^HOSTNAME.*$/d‘ /etc/sysconfig/network-scripts/ifcfg-eth0 sed -i ‘/^HOSTNAME.*$/d‘ /etc/hosts echo "HOSTNAME=$HOSTNAME" >> /etc/sysconfig/network IPADDR=`ip a | grep "global eth0" | awk ‘{print $2}‘ | awk -F "/" ‘{print $1}‘` echo "$IPADDR $HOSTNAME" >>/etc/hosts #echo -n " $HOSTNAME" >> /etc/hosts echo "====================================================" echo "" sleep 2 } # yum_163 configYum(){ echo "================更新为国内YUM源==================" cd /etc/yum.repos.d/ \cp CentOS-Base.repo CentOS-Base.repo.$(date +%F) ping -c 1 www.163.com>/dev/null if [ $? -eq 0 ];then wget http://mirrors.163.com/.help/CentOS6-Base-163.repo else echo "无法连接网络。" exit $? fi \cp CentOS-Base-sohu.repo CentOS-Base.repo yum clean all # yum makecache >& /dev/null action "配置国内YUM完成" /bin/true echo "=================================================" echo "" sleep 2 } #time sync syncSysTime(){ echo "================配置时间同步=====================" \cp /var/spool/cron/root /var/spool/cron/root.$(date +%F) 2>/dev/null NTPDATE=`grep ntpdate /var/spool/cron/root 2>/dev/null |wc -l` if [ $NTPDATE -eq 0 ];then echo "#times sync by lee at $(date +% F)" >>/var/spool/cron/root echo "*/5 * * * * /usr/sbin/ntpdate time.windows.com >/dev/null 2>&1" >> /var/spool/cron/root fi echo ‘#crontab -l‘ crontab -l #action "配置时间同步完成" /bin/true echo "=================================================" echo "" sleep 2 } #Charset zh_CN.UTF-8 initI18n(){ echo "================更改为中文字符集=================" \cp /etc/sysconfig/i18n /etc/sysconfig/i18n.$(date +%F) echo "LANG="zh_CN.UTF-8"" >/etc/sysconfig/i18n source /etc/sysconfig/i18n echo ‘#cat /etc/sysconfig/i18n‘ grep LANG /etc/sysconfig/i18n #action "更改字符集zh_CN.UTF-8完成" /bin/true echo "=================================================" echo "" sleep 2 } #Close Selinux and Iptables Firewall(){ echo "============禁用SELINUX及关闭防火墙==============" \cp /etc/selinux/config /etc/selinux/config.$(date +%F) /etc/init.d/iptables stop sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g‘ /etc/selinux/config setenforce 0 /etc/init.d/iptables status echo ‘#grep SELINUX=disabled /etc/selinux/config ‘ grep SELINUX=disabled /etc/selinux/config echo ‘#getenforce ‘ getenforce #action "禁用selinux及关闭防火墙完成" /bin/true echo "=================================================" echo "" sleep 2 } #menu2 menu2(){ while true do clear cat << EOF ---------------------------------------- |****Please Enter Your Choice:[0-6]****| ---------------------------------------- (1) 配置ip地址以 (2) 配置为国内YUM源镜像 (3) 设置时间同步 (4) 配置中文字符集 (5) 禁用SELINUX及关闭防火墙 (6) 配置主机名称 (0) 返回上一级菜单 EOF read -p "Please enter your Choice[0-6]: " input2 case "$input2" in 0) clear break ;; 1) ipaddr ;; 2) configYum ;; 3) syncSysTime ;; 4) initI18n ;; 5) Firewall ;; 6) hostname ;; *) echo "----------------------------------" echo "| Warning!!! |" echo "| Please Enter Right Choice! |" echo "----------------------------------" for i in `seq -w 3 -1 1` do echo -ne "\b\b$i"; sleep 1; done clear esac done } #menu while true do clear echo "========================================" echo ‘ Linux Optimization ‘ echo "========================================" cat << EOF |-----------System Infomation----------- | DATE :$DATE | HOSTNAME :$HOSTNAME | USER :$USER | IP :$IPADDR | DISK_USED :$DISK_SDA | CPU_AVERAGE:$cpu_uptime ---------------------------------------- |****Please Enter Your Choice:[1-3]****| ---------------------------------------- (1) 一键优化 (2) 自定义优化 (3) 退出 EOF #choice read -p "Please enter your choice[0-3]: " input1 case "$input1" in 1) ip_hostname configYum syncSysTime initI18n initFirewall hostname ;; 2) menu2 ;; 3) clear break ;; *) echo "----------------------------------" echo "| Warning!!! |" echo "| Please Enter Right Choice! |" echo "----------------------------------" for i in `seq -w 3 -1 1` do echo -ne "\b\b$i"; sleep 1; done clear esac done
时间: 2024-10-22 06:09:16