美文网首页
shell -- 批量修改ip配置文件

shell -- 批量修改ip配置文件

作者: w_dll | 来源:发表于2020-05-29 13:20 被阅读0次

    写了有一段时间了,批处理模块就不展示了,
    主要是思路,同时把ip配置文件和修改ip脚本传到目标机器上;
    目标机器根据配置文件修改ip

    修改ip脚本如下

    #!/bin/bash
    file_name=test.txt
    oldip=`cat $file_name|grep -iA3 eth0 | grep address | grep -oP '([0-9]{1,3}\.){3}[0-9]{1,3}'`
    oldmask=`cat $file_name|grep -iA3 eth0 | grep netmask | grep -oP '([0-9]{1,3}\.){3}[0-9]{1,3}'`
    oldgate=`cat $file_name|grep -iA3 eth0 | grep gateway | grep -oP '([0-9]{1,3}\.){3}[0-9]{1,3}'`
    ##############################
    #############################
    newmask='255.255.255.0'
    newgate='10.44.133.1'
    echo '旧的ip:'
    echo $oldip
    echo $oldmask
    echo $oldgate
    echo '----------------------'
    echo '新的ip'
    newip=`cat ipconf.txt | grep $oldip | awk '{print $1}'`
    echo $newip
    echo $newmask
    echo $newgate
    
    
    sed -i 's/'$oldip'/'$newip'/' $file_name
    sed -i 's/'$oldmask'/'$newmask'/' $file_name
    sed -i 's/'$oldgate'/'$newgate'/' $file_name
    

    配置文件

    [root@xxwdll-master-node test]# cat ipconf.txt 
    10.44.133.88    10.32.166.123
    10.44.133.11    10.32.166.456
    10.44.133.99    10.32.166.789
    

    测试文件

    [root@xxwdll-master-node 20200425]# cat 
    1.sh        ipconf.txt  test.txt    
    [root@xxwdll-master-node 20200425]# cat test.txt 
    # This file describes the network interfaces available on your system                           
    # and how to activate them. For more information, see interfaces(5).                            
                                
    # The loopback network interface                            
    auto lo                         
    iface lo inet loopback                          
    auto eth0                           
    iface eth0 inet static                          
    address 10.44.133.11                            
    netmask 255.255.255.0                           
    gateway 10.44.133.1                         
                                
    auto eth1                           
    iface eth1 inet static                          
    address 192.168.1.101                           
    netmask 255.255.255.0                           
                                
    auto eth2                           
    iface eth2 inet static                          
    address 192.168.2.101                           
    netmask 255.255.255.0                           
    

    相关文章

      网友评论

          本文标题:shell -- 批量修改ip配置文件

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