shell脚本 关闭防火墙、安装httpd、启动httpd
[root@node11 ~]# mkdir shell
[root@node11 ~]# vim abc.sh
#!/bin/bash
#安装httpd服务
#1、挂载 准备yum源
mount /dev/sr0 /mnt &> /dev/null
df=$(df -h | grep /dev/sr0 | awk '{print $6}')
if [ "$df" == "/mnt" ]
then
echo "/dev/sr0 已挂载到 /mnt 上"
fi
cat <<EOF>> /etc/yum.repos.d/local.repo
[local]
name=centos7.3
baseurl=file:///mnt
enabled=1
gpgcheck=0
EOF
systemctl 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" ]
then
echo "SELINUX 已关闭"
else
echo "SELINUX 已开启"
fi
#2、安装httpd
echo "正在安装httpd服务,请耐心等待........"
yum -y install httpd &> /dev/null
#启动httpd
systemctl start httpd
status=$(systemctl status httpd | grep active | awk '{print $3}')
if [ "$status" == "(running)" ]
then
echo "httpd服务启动成功"
else
echo "httpd服务启动失败"
fi
[root@node11 shell]# chmod +x abc.sh
[root@node11 shell]#./abc.sh
shell脚本 修改主机名、修改IP、获取硬件信息
[root@node11 shell]# vim bcd.sh
#!/bin/bash
echo -e "===请选择以下功能=== \n1 修改计算机名 \n2 修改IP地址 \n3 查看硬件信息"
read -p "请选择功能:" nm
case $nm in
1)
#1、修改计算机名称
read -p "请输入计算机名:" name
hostnamectl set-hostname $name
bash
hostname
;;
2)
#2、修改ip
echo "IP地址格式 192.168.1.100/24"
read -p "请输入IP地址:" ip_mask
ip=$(echo "$ip_mask" | awk -F/ '{print $1}')
mask=$(echo "$ip_mask" | awk -F/ '{print $2}')
read -p "请输入网关地址:" gw
read -p "请输入DNS地址:" dns
network_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_file
sed -i 's/PREFIX=.*/PREFIX=$mask/g' $network_file
sed -i 's/GATEWAY=.*/GATEWAY=$gw/g' $network_file
sed -i 's/ONBOOT=.*/ONBOOT=yes/g' $network_file
sed -i 's/DNS1=.*/DNS1=$dns/g' $network_file
sed -i 's/BOOTPROTO=.*/BOOTPROTO=static/g' $network_file
sed -i '/NETMASK=255.255.255.0/d' $network_file
systemctl restart network
ip 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