美文网首页
使用shell提高工作效率

使用shell提高工作效率

作者: ljyfree | 来源:发表于2019-08-20 11:05 被阅读0次

    最近经常要对centos系统做一些初始化,今天用shell写了一个脚本,记录一下,后面可以来借鉴

    直接上脚本

    [root@www-ctrl-212 ~]# cat www_init.sh 
    #!/bin/bash
    echo "------ init process before deploy www controller -----"
    
    host_name="www-ctrl-212"
    public_ip="10.25.70.212"
    public_netmask="255.255.0.0"
    public_gw="10.25.0.1"
    public_port="eth0"
    ctrl_ip="172.18.70.212"
    ctrl_netmask="255.255.0.0"
    ctrl_port="eth1"
    
    
    hostnamectl --static set-hostname ${host_name}
    setenforce 0
    cp /etc/selinux/config /etc/selinux/config_bak
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    systemctl stop firewalld
    systemctl disable firewalld
    
    rm -f /etc/sysconfig/network-scripts/ifcfg-${public_port}
    echo -e "BOOTPROTO=static\nDEVICE=${public_port}\nONBOOT=yes\nNAME=${public_port}\nIPADDR=${public_ip}\nNETMASK=${public_netmask}\nGATEWAY=${public_gw}\nARPCHECK=no" > /etc/sysconfig/network-scripts/ifcfg-${public_port}
    rm -f /etc/sysconfig/network-scripts/ifcfg-${ctrl_port}
    echo -e "BOOTPROTO=static\nDEVICE=${ctrl_port}\nONBOOT=yes\nNAME=${ctrl_port}\nIPADDR=${ctrl_ip}\nNETMASK=${ctrl_netmask}\nARPCHECK=no" > /etc/sysconfig/network-scripts/ifcfg-${ctrl_port}
    
    cp /etc/ssh/sshd_config /etc/ssh/sshd_config_bak
    sed -i 's/#Port 22/Port 22\nPort 61000/g' /etc/ssh/sshd_config
    
    systemctl restart sshd
    systemctl restart network
    echo " ------ end ------"
    [root@www-ctrl-212 ~]# 
    

    上面涉及到

    • 变量的赋值和调用
    • 使用echo将字符串写入文件,需要使用双引号来做变量替换
    • 使用sed修改文件
    • 注意Windows下编辑,需要转成LF(Notepad++,编辑->文档格式->转为Unix(LF))

    相关文章

      网友评论

          本文标题:使用shell提高工作效率

          本文链接:https://www.haomeiwen.com/subject/eivusctx.html