shell脚本快速入门之-----linux设置 自定义脚本开机启动,一键式部署网卡配置文件

时间:2022-07-24
本文章向大家介绍shell脚本快速入门之-----linux设置 自定义脚本开机启动,一键式部署网卡配置文件,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、 赋予可执行权限

chmod +x /etc/rc.d/rc.local

二、 编辑启动文件

vim /etc/rc.d/rc.local

三、 在 /etc/rc.d/rc.local 中 加入 自己的执行脚本 & 后台运行

sh /opt/ping.sh &

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

#touch /var/lock/subsys/local
sh /opt/ping.sh &

四、脚本如下

#!/bin/bash
ip=www.baidu.com
ping -c 2 -w 3 -i 0.3 $ip &>/dev/null
if  [ $? -eq 0 ]
 then
echo " 可以ping的通百度"
 else
 echo "正在更改你的网卡"
sed -i '/^IPADDR=/cIPADDR=192.168.110.132' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '/^GATEWAY=/cGATEWAY=192.168.110.2' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '/^DNS1=/cDNS1=8.8.8.8' /etc/sysconfig/network-scripts/ifcfg-ens33
echo "网卡配置文件已改完  正在重启网络服务"
systemctl restart network
fi

ping -c 2 $ip &>/dev/null
if [ $? -eq 0 ] ;then
 echo "一切准备就绪"
 else
  echo "请检查你绑定的网卡是不是vm8"
  fi