import re import os tmp_file = r"C:\Users\yc\Documents\NetSarang Computer\7\Xshell\Sessions\192.168.2.253.xsh" with open (tmp_file , "r+", encoding="utf-16-le") as file: context = file.read() print(context) list1 = [ '192.168.1.1', '192.168.2.2', # 可以在下一行插入ip,格式: 'ip', '192.168.3.3' ] for ip in list1: context = re.sub("Host=.*", "Host=" + ip, context) new_file = ip + ".xsh" with open(r"C:\Users\yc\Documents\NetSarang Computer\7\Xshell\Sessions\ " + new_file, 'w+', encoding='utf-8', errors='ignore') as f2: f2.write(context) f2.close() print("生成文件" + ip + ".xsh")
标签:f2,file,批量,ip,192.168,会话,context,xshell,xsh From: https://www.cnblogs.com/yc-tec/p/16893885.html