Use python to generate xshell connection file .sh files in batches

''' 
Requirement: According to the successfully connected xsh file, change its ip and host address, generate xsh files in batches, avoid tedious manual connection 
python version: python3
''' import os import re # f_map: host and ip mapping files: # host01 10.1.1.1 # host02 10.2.2.2 # f_template: xshell template file # f_new: new xshell file # ip in ip_template template file ip_template = " 10.12.125.63 " with open( " map.txt " ) as f_map, open( " template.xsh " ) as f_template: #Open the map file to read the mapping relationship line by line while True: line = f_map.readline() if len(line)== 0: break #Remove the characters specified at the beginning and end of the string (default is space) line.strip() regex = re.compile( ' \s+ ' ) #cut host and ip into arrays line = regex.split(line) print (line) #Copy the template file line by line, modify the host address at the same time, and generate a new xsh file with open(line[0]+ " .xsh " , ' w ' ) as f_new: while True: content = f_template.readline() if len(content)== 0: #After each read, the file pointer moves to the beginning f_template.seek(0) break content =content.replace(ip_template, line[1]) # ## To be improved, the replace function f_new.write(content) is called every time a row is read

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324767715&siteId=291194637