码迷,mamicode.com
首页 > 其他好文 > 详细

简单主机批量管理工具

时间:2017-07-18 23:09:51      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:返回   c89   too   单主机   ace   字典   需求   base   格式   

题目:简单主机批量管理工具

需求:

  1. 主机分组
  2. 登录后显示主机分组,选择分组后查看主机列表
  3. 可批量执行命令、发送文件,结果实时返回
  4. 主机用户名密码可以不同

 

流程图:

技术分享

说明:

技术分享
技术分享
### 作者介绍:
* author:lzl
### 博客地址:
* http://www.cnblogs.com/lianzhilei/p/5881434.html

### 功能实现
题目:简单主机批量管理工具

    需求:
    主机分组
    登录后显示主机分组,选择分组后查看主机列表
    可批量执行命令、发送文件,结果实时返回
    主机用户名密码可以不同

### 目录结构:
    Host-Manage
    │
    ├── ftpclient #客户端程序
            ├── README.txt
            ├── management.py #服务端入口程序
            ├── database #数据库
            ├── test.py #修改数据库


### 注释
    可批量执行命令、发送文件
    上传命令格式: put database /tmp/db


### 运行环境
    windows系统
    python3.0+
技术分享

 

主程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#-Author-Lian
 
import json
import paramiko
import threading
 
class Remotehost(object):
    #远程操作主机
    def __init__(self,host,port,username,password,cmd):
        self.host = host
        self.port = port
        self.username = username
        self.password = password
        self.cmd = cmd
 
    def command(self):
        #获取命令
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())   # 允许连接不在know_hosts文件中的主机
        ssh.connect(hostname=self.host, port=self.port, username=self.username, password=self.password)   # 连接服务器
        stdin, stdout, stderr = ssh.exec_command(self.cmd)              # 获取命令结果
        res ,err = stdout.read(),stderr.read()              # 三元运算
        result = res if res else err
        print("[%s]".center(50,"-")%self.host)
        print(result.decode())                                      # 打印输出
        ssh.close()
 
    def put(self):
        #上传
        try:
            transport = paramiko.Transport((self.host, self.port))
            transport.connect(username=self.username, password=self.password)
            sftp = paramiko.SFTPClient.from_transport(transport)
            sftp.put(self.cmd.split()[1], self.cmd.split()[2])              # 上传文件
            transport.close()
            print("\033[32;0m【%s】 上传 文件【%s】 成功....\033[0m"%(self.host,self.cmd.split()[2]))
        except Exception as error:                                # 抓住异常
            print("\033[31;0m错误:【%s】【%s】\033[0m"%(self.host,error))
 
    def run(self):
        #反射
        cmd_str = self.cmd.split()[0]
        if hasattr(self,cmd_str):
            getattr(self,cmd_str)()
        else:
            setattr(self,cmd_str,self.command)
            getattr(self,cmd_str)()
 
 
if __name__ == "__main__":
    #主程序
    with open("database","r") as file:
        data_dict = json.loads(file.read())     #获取数据库信息
    for in data_dict:                        #打印地址组
        print(k)
 
    group_choice = input("输入要操作的组名:").strip()
    if data_dict.get(group_choice):
        host_dict = data_dict[group_choice]     #定义主机字典
        for in host_dict:                    #打印所选地址组所有的主机名
            print(k)
        while True:
            cmd = input("选择进行的操作的命令:").strip()
            thread_list=[]
            if cmd:                                 #命令不为空
                for in host_dict:
                    host, port, username, password=k,host_dict[k]["port"],host_dict[k]["username"],host_dict[k]["password"]
                    func = Remotehost(host,port,username,password,cmd)      #实例化类
                    = threading.Thread(target=func.run)                   #创建线程
                    t.start()
                    thread_list.append(t)
                for in thread_list:
                    t.join()                                                #等待线程执行结果
    else:
        print("\033[31;0m操作组不存在\033[0m")

 

数据库:

技术分享
{"group1": {"192.168.20.217": {"password": "123456", "username": "root", "port": 22}, "192.168.20.219": {"password": "zyw@123", "username": "root", "port": 22}}, "group2": {"192.168.20.217": {"password": "123456", "username": "root", "port": 22}}}

简单主机批量管理工具

标签:返回   c89   too   单主机   ace   字典   需求   base   格式   

原文地址:http://www.cnblogs.com/luoahong/p/7203145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!