#!/usr/bin/env python
from multiprocessing import Process
import paramiko
import time
import sys
import new_latest_configparser #导入配置信息模块
# import groupshow
Username = "root"
Password = "123456"
Port = 22
Current_time = time.strftime("%Y-%m-%d %X", time.localtime())
#执行命令处
def runCmd(ip,cmd):
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
s.connect(hostname=ip,port=Port,username=Username,password=Password)
stdin,stdout,stderr = s.exec_command(cmd)
result = stdout.read()
# print(result)
s.close()
print("IP:[%s]at:[%s] run command [%s]".center(50, "-") % (ip, Current_time,cmd))
print(str(result, encoding="utf-8"))
except:
print("%s is not exits" %ip)
# if_continue = input("请输入是否需要继续输入命令,是按[Y],否按[N]")
def handle_Process():
while True:
print("欢迎来到ansible模拟系统,主机分组如下".center(50, "-"))
#调用主机信息模块方法
new_latest_configparser.showgroups()
User_choice = str(input("请选择组:").strip())
if len(User_choice) == 0 or User_choice not in new_latest_configparser.GROUP or User_choice.isdigit():
print("请输入正确的组名")
continue
elif User_choice == "webserver":
#打印主机信息处
print(new_latest_configparser.config.get("webserver", "IP"))
print(new_latest_configparser.config.get("webserver", "IP2"))
break
elif User_choice == "dbserver":
# print(functions.config.get("dbserver"))
print(new_latest_configparser.config.get("dbserver","IP"))
break
try:
cmd = input("please input your cmd:")
#判断主机组
if User_choice == "webserver":
#循环获取IP
for ip in new_latest_configparser.IPlist:
p = Process(target=runCmd,args=(ip,cmd))
p.start()
elif User_choice == "dbserver":
for ip in new_latest_configparser.Other_list:
p = Process(target=runCmd, args=(ip, cmd))
p.start()
except IndexError:
print("please input a command")
if __name__ == '__main__':
handle_Process()
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com