#!/usr/bin/python3
# author 老板你再这样我要删库了
import os
# 字典的key对应所有服务器IP,字典的value对应一个列表。
info = {
"服务器ip1" : ["tomcat1"],
"服务器ip2" : ["tomcat2" , "tomcat3"]
}
# 遍历所有IP
for serverIP in info.keys():
# 遍历所有Tomcat名
for tomcatName in info[serverIp]:
command = "ssh root@" + info[serverIp] + " " + "/opt/" + tomcatName + "/bin/startup.sh"
os.system(command)
这十几行代码看着很少,但没点开发基础的人很难看得懂。
当然,如果你也用python,你也可以用os.system语句,每一个Tomcat一条语句,类似这样:
os.system("ssh root@服务器ip1 /opt/tomcat01/bin/startup.sh")
os.system("ssh root@服务器ip2 /opt/tomcat02/bin/startup.sh")
...
这样也能达到目的,但是代码重复的部分就很多,行数多了,看起来逼格不够高。