用xshell 添加IP,密码的时候,如果少量的话,可以手动添加;如果IP多的话,需要消耗很长时间,想到可以利用脚本来实现,前提是批量机器的密码是一样的
首先在xshell 里面创建一个模板出来,将批量IP放到文件中,运行脚本就可以了
脚本如下
- # coding=utf-8
- import os
- import shutil
- import fileinput
- target_dir = unicode("你自己的xshell session 目录", "utf-8")
- def test():
- ip_doc_list = []
- if os.path.exists(target_dir) and os.listdir(target_dir):
- original_file = os.listdir(target_dir)[0]
- original_ip = original_file.rstrip(".xsh")
- with open(target_dir + "ip.txt") as ip_doc_file:
- for ip_doc_line in ip_doc_file:
- if ip_doc_line.split():
- ip_doc_list.append(ip_doc_line.split()) # 文件内容放在list里面[[ip, doc], [ip, doc],[ip]]
- # 根据模板拷贝文件
- for ip_doc in ip_doc_list:
- file_name_doc = "-" + ip_doc[1] + ".xsh" if len(ip_doc) == 2 else ".xsh"
- if str(ip_doc[0]) != str(original_ip):
- shutil.copyfile(target_dir + original_file, target_dir + ip_doc[0] + file_name_doc)
- # 修改文件中HOST字段,改成真正的IP
- print "========修改文件内容HOST============"
- target_file_list = os.listdir(target_dir)
- for target_file in target_file_list:
- target_ip = target_file.split("-")[0] if len(target_file.split("-")) == 2 else target_file.split("-")[0].rstrip(".xsh")
- for line in fileinput.input(target_dir + target_file, inplace=1):
- print line.rstrip().replace(original_ip, target_ip)
- fileinput.close()
- else:
- print " 您输入的文件路径 或文件名不合法!!!!"
-
-
- if __name__ == "__main__":
- test()