使用python批量生成xshell连接文件.sh文件

'''
需求:根据连接成功的xsh文件,更改其ip和host地址,批量生成xsh文件,避免繁琐的手工连接
python版本:python3
''' import os import re #f_map:host和ip映射文件: #host01 10.1.1.1 #host02 10.2.2.2 #f_template:xshell模板文件 #f_new:新建的xshell文件 #ip_template 模板文件中的ip ip_template = "10.12.125.63" with open("map.txt") as f_map, open("template.xsh") as f_template: #打开映射文件逐行读取映射关系 while True: line = f_map.readline() if len(line)==0: break #移除字符串头尾指定的字符(默认为空格) line.strip() regex = re.compile('\s+') #将host和ip切割成数组 line = regex.split(line) print(line) #逐行拷贝模板文件,同时修改host地址,生成新的xsh文件 with open(line[0]+".xsh",'w') as f_new: while True: content = f_template.readline() if len(content)==0: #每读一遍后,文件指针移到开头 f_template.seek(0) break content=content.replace(ip_template, line[1]) ###有待改进,每次读取行都会调用replace函数 f_new.write(content)

猜你喜欢

转载自www.cnblogs.com/lonquanzj/p/8926791.html
今日推荐