输入任意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|,]', '\n', data)
        data = re.sub(r'[^\d|\.]', '\n', data) 
        data = re.sub('10.102.', 'minion-', data)                      
        list1.append(data)
    for data in list1:
        data = data.strip('\n')
        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('\n')
            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 + '\n'
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|\.]', '\n', data)
        data = re.sub(r'\n+', '\n', 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'\n', ',', data)
        list2.append(data)
    for data in list2:
        print(data,end='')

 

posted @ 2016-08-24 21:08  freedom_dog  阅读(607)  评论(0编辑  收藏  举报