• 输入任意IP,将IP转化为minion-IP格式(saltstack)


    注1:10.102. 可以替换成其他的,或者手动输入。

    注2:minion-可以替换为其他的,或者手动输入。

    代码如下(python3):

    import re
    
    # list = []
    list1 = []
    list2 = []
    list3 = []
    list4 = []
    with open('srcIp.txt') as fip,open('desIP','w') as f:    
        for data in fip.readlines():
    #         data = re.sub(r'[s|,]', '
    ', data)
            data = re.sub(r'[^d|.]', '
    ', data) 
            data = re.sub('10.102.', 'minion-', data)                      
            list1.append(data)
        for data in list1:
            data = data.strip('
    ')
            if data == '':
                continue
            data = re.sub(r's+', ',', data)
            list2.append(data)
        for data in list2:
            if ',' in data:
                data = data.split(',')
                for i in range(0,len(data)):
                    list3.append(data[i])
                continue
            list3.append(data)
        for data in list3:
            if not data.startswith('minion'):
                data = 'minion-' + data  
                list4.append(data)
                continue
            list4.append(data)
        print('作用IP数:%s' % len(list4))
        i = 0
        for data in list4:
            if i == len(list4) - 1:
                f.write(data)
                f.write('
    ')
                break
            data = data + ','
            f.write(data)
            i += 1

    源IP文件内容:

    10.102.37.41
    10.102.37.61
    
    10.102.38.110
    10.102.38.111
    10.102.38.71
    10.102.38.77
    10.102.38.80
    10.102.38.81
    
                 10.102.32.98
      10.102.32.99
                               10.102.32.100
      10.102.32.101
    
    
    10.102.36.32  10.102.36.33  10.102.36.72    10.102.38.87 10.102.38.88 10.102.38.89 10.102.38.90 10.102.38.91 
    
    10.102.36.32,10.102.36.33 10.102.36.72,     10.102.38.87 
    
    
    25.221      38.36
              32.21
    33.65
       ,33.65 ,32.21
       
       
       
       (25.33,95.66,58.37;55.99;)

    执行完脚本后文件内容:

    minion-37.41,minion-37.61,minion-38.110,minion-38.111,minion-38.71,minion-38.77,minion-38.80,minion-38.81,minion-32.98,minion-32.99,minion-32.100,minion-32.101,minion-36.32,minion-36.33,minion-36.72,minion-38.87,minion-38.88,minion-38.89,minion-38.90,minion-38.91,minion-36.32,minion-36.33,minion-36.72,minion-38.87,minion-25.221,minion-38.36,minion-32.21,minion-33.65,minion-33.65,minion-32.21,minion-25.33,minion-95.66,minion-58.37,minion-55.99

    可见,把各种IP变成了SaltStack可操作的IP。(我们salt minion的名称都是minion-IP后两位的格式)

    代码2,此方法直接输入内容,输出内容,省去了读文件写文件:

    import re,json
    
    list1 = []
    list2 = []
    stopword = 'run'
    str1 = ''
    print('请输入IP:')
    for line in iter(input,stopword):
        str1 += line + '
    '
    with open('sss','w') as f:
        f.write(json.dumps(str1))
    with open('sss','r') as f1,open('sss1','w') as f2:
        for data in f1.readlines():
            data = re.sub(r'[^d|.]', '
    ', data)
            data = re.sub(r'
    +', '
    ', data)
            data = re.sub('10.102.', 'minion-', data)
            print(data)
        f2.write(data)
    with open('sss1') as f3:
        for data in f3:
            if not data.startswith('minion-') and not data.startswith(' '):
                data = 'minion-' + data
            list1.append(data)
        for data in list1:
            data = re.sub(r'
    ', ',', data)
            list2.append(data)
        for data in list2:
            print(data,end='')
  • 相关阅读:
    python 企业微信告警
    容器启动报错listen unix /containerd-shim/moby/9a3b9086ece8fcd8746695836e3f057cc0313b3cdb722d76a5f571dfa428759e/shim.sock: bind: address already in use: unknown
    etcd查询k8s相关数据
    hadoop三大核心组件概念及原理
    使用nginx反代实现k8s apiserver高可用
    k8s useraccout账号创建及RDBA授权
    k8s之二进制部署
    git代码提交后jenkins的构建与持续部署
    dockerfile动态修改服务配置文件
    数据库操作语句DDL,DML,DCL
  • 原文地址:https://www.cnblogs.com/fuckily/p/5804616.html
Copyright © 2020-2023  润新知