shell脚本 关闭防火墙、安装httpd、启动httpd
[root@node11 ~]# mkdir shell[root@node11 ~]# vim abc.sh
#!/bin/bash#安装httpd服务#1、挂载 准备yum源
mount /dev/sr0 /mnt &> /dev/nulldf=$(df -h | grep /dev/sr0 | awk '{print $6}')if [ "$df" == "/mnt" ]thenecho "/dev/sr0 已挂载到 /mnt 上"
ficat <<EOF>> /etc/yum.repos.d/local.repo
[local]
name=centos7.3
baseurl=file:///mnt
enabled=1
gpgcheck=0
EOFsystemctl disable firewalld --now &> /dev/null
systemctl status firewalld | grep dead &> /dev/null && echo "防火墙已关闭"setenforce 0 &> /dev/null
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
selinux=$(getenforce)
if [ "$selinux" == "Disabled" ]thenecho "SELINUX 已关闭"elseecho "SELINUX 已开启"
fi#2、安装httpd
echo "正在安装httpd服务,请耐心等待........"
yum -y install httpd &> /dev/null#启动httpdsystemctl start httpd
status=$(systemctl status httpd | grep active | awk '{print $3}')if [ "$status" == "(running)" ]thenecho "httpd服务启动成功"elseecho "httpd服务启动失败"
fi
[root@node11 shell]# chmod +x abc.sh[root@node11 shell]#./abc.sh
shell脚本 修改主机名、修改IP、获取硬件信息
[root@node11 shell]# vim bcd.sh
#!/bin/bashecho -e "===请选择以下功能=== \n1 修改计算机名 \n2 修改IP地址 \n3 查看硬件信息"
read -p "请选择功能:" nmcase $nm in1)
#1、修改计算机名称read -p "请输入计算机名:" namehostnamectl set-hostname $namebashhostname
;;2)
#2、修改ipecho "IP地址格式 192.168.1.100/24"read -p "请输入IP地址:" ip_maskip=$(echo "$ip_mask" | awk -F/ '{print $1}')mask=$(echo "$ip_mask" | awk -F/ '{print $2}')read -p "请输入网关地址:" gwread -p "请输入DNS地址:" dnsnetwork_nm=$(ip add | grep ens | awk 'NR==1 {print $2}' | awk -F: '{print $1}')network_file="/etc/sysconfig/network-scripts/ifcfg-$network_nm"sed -i 's/IPADDR=.*/IPADDR=$ip/g' $network_filesed -i 's/PREFIX=.*/PREFIX=$mask/g' $network_filesed -i 's/GATEWAY=.*/GATEWAY=$gw/g' $network_file
sed -i 's/ONBOOT=.*/ONBOOT=yes/g' $network_filesed -i 's/DNS1=.*/DNS1=$dns/g' $network_filesed -i 's/BOOTPROTO=.*/BOOTPROTO=static/g' $network_filesed -i '/NETMASK=255.255.255.0/d' $network_filesystemctl restart networkip add
;;3)
#3、获取硬件信息cpu_mode1=$(cat /proc/cpuinfo | grep "model name" | awk -F: '{print $2}' | awk -F@ '{print $1}')cpu_cores=$(cat /proc/cpuinfo | grep "cpu cores" | awk -F: '{print $2}')mem_total=$(free -h | grep "Mem" | awk '{print $2}')disk_info=$(fdisk -l | grep -w "/dev/sd[a-z]" | awk '{print $2" "$3}')echo "==========硬件信息=========="echo -e "CPU型号:\n$cpu_mode1"echo -e "CPU内核:\t$cpu_cores"echo -e "内存信息:\t$mem_total"echo -e "磁盘信息:\n$disk_info"
;;*)echo "请输入正确编号 1 2 3"
;;esac
[root@node11 shell]# chmod +x bcd.sh[root@node11 shell]# ./bcd.sh