[ Python - 10 ] 练习:批量管理主机工具
需求:
- 主机分组
- 登录后显示主机分组,选择分组后查看主机列表
- 可批量执行命令、发送文件,结果实时返回
- 主机用户名密码可以不同
流程图:

说明:
## 需求:
- 主机分组
- 登录后显示主机分组,选择分组后查看主机列表
- 可批量执行命令、发送文件,结果实时返回
- 主机用户名密码可以不同
## 目录结构
batch-manage
bin/
start.py # 启动主程序
conf/
setting.py # 远程主机配置文件
core/
control.py # RemoteControl类,用于远程主机执行命令及上传
local.py # 本地数据处理 ## 说明
目前实现可批量执行命令,上传文件功能。 ## 运行环境
windows
python3.0+ 代码下载地址:https://github.com/hukeyy/batch-manage
bin/
start.py
#!_*_coding:utf-8_*_
# Author: hkey
import threading, os, sys
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(base_dir)
from core import control
from core import local
from conf import setting if __name__ == '__main__': for host in setting.hosts.keys():
host_tuple = local.options()
print('\33[42;1m[put file or inter system command.]\33[0m')
while True:
cmd = input('>>>').strip()
if len(cmd) == 0: continue
t_list = []
for host in host_tuple:
remote_control = control.RemoteControl(cmd, *host)
t = threading.Thread(target=remote_control.run,)
t.setDaemon(True)
t.start()
t_list.append(t)
for t in t_list:
t.join()
conf/
setting.py
#!_*_coding:utf-8_*_
# Author: hkey
## 元组在python中是不可变对象,保存序列时,应当尽量采用这种方式
hosts = {'测试系统':(('10.0.0.11', 22, 'root', ''),
('10.0.0.12', 22, 'root', ''),
), '生产系统':(('10.0.0.12', 22, 'root', ''),
),
}
core/
control.py
#!_*_coding:utf-8_*_
# Author: hkey
import paramiko class RemoteControl(object):
'''远程主机操作类'''
def __init__(self, cmd, *kw):
self.hostname = kw[0]
self.port = kw[1]
self.username = kw[2]
self.password = kw[3]
self.cmd = cmd def run(self):
'''通过反射功能将程序解耦'''
cmd = self.cmd.split()[0]
if hasattr(self, cmd):
getattr(self, cmd)()
else:
setattr(self, cmd, self.command)
getattr(self, cmd)() def put(self):
'''上传文件'''
# try:
transport = paramiko.Transport(self.hostname, 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('\33[32;1m【%s】上传文件【%s】成功!' %(self.hostname, self.cmd.split()[1]))
# except Exception as e:
# print('\33[31;1m错误:【%s】: 【%s】\33[0m' %(self.hostname, e)) def command(self):
'''执行系统静态命令'''
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=self.hostname, 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('\33[32;1m%s\33[0m'.center(50, '-') % self.hostname)
print(result.decode())
ssh.close()
core/
local.py
#!_*_coding:utf-8_*_
# Author: hkey
import os, sys
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(base_dir)
from conf import setting
def options():
'''通过配置文件setting.py,用户选择主机组并通过用户选择的组获取到主机的信息'''
for host in setting.hosts.keys():
print(host)
while True:
choice = input('please choice groupname:')
if len(choice) ==0: continue
if choice in setting.hosts.keys():
for host in setting.hosts[choice]:
print(host[0])
return setting.hosts[choice]
else:
print('groupname not exist.')
break
[ Python - 10 ] 练习:批量管理主机工具的更多相关文章
- 【Python】JBOSS-JMX-EJB-InvokerServlet批量检测工具
一.说明 在JBoss服务器上部署web应用程序,有很多不同的方式,诸如:JMX Console.Remote Method Invocation(RMI).JMXInvokerServlet.Htt ...
- Python简单主机批量管理工具
一.程序介绍 需求: 简单主机批量管理工具 需求: 1.主机分组 2.主机信息使用配置文件 3.可批量执行命令.发送文件,结果实时返回 4.主机用户名密码.端口可以不同 5.执行远程命令使用param ...
- python 简单主机批量管理工具
需求: 主机分组 主机信息配置文件用configparser解析 可批量执行命令.发送文件,结果实时返回,执行格式如下 batch_run -h h1,h2,h3 -g web_cluster ...
- 【Python之旅】第六篇(七):开发简易主机批量管理工具
[Python之旅]第六篇(七):开发简易主机批量管理工具 python 软件开发 Paramiko模块 批量主机管理 摘要: 通过前面对Paramiko模块的学习与使用,以及Python中多线程与多 ...
- python运维之使用python进行批量管理主机
1. python运维之paramiko 2. FABRIC 一个与多台服务器远程交互的PYTHON库和工具 3. SSH连接与自动化部署工具paramiko与Fabric 4. Python批量管理 ...
- Python开发程序:简单主机批量管理工具
题目:简单主机批量管理工具 需求: 主机分组 登录后显示主机分组,选择分组后查看主机列表 可批量执行命令.发送文件,结果实时返回 主机用户名密码可以不同 流程图: 说明: ### 作者介绍: * au ...
- MySQL通用批量写入工具(Python)
背景 平台目前的分析任务主要以Hive为主,分析后的结果存储在HDFS,用户通过REST API或者Rsync的方式获取分析结果,这样的方式带来以下几个问题: (1)任务执行结束时间未知,用户 ...
- Linux系统——Ansible批量管理工具
批量管理工具: (1)ansible 操作简单(适用于500台以下服务器) (2)saltstack 比较复杂(一般适用于1000-4w台服务器) (3)puppet超级复杂 systemctl(统一 ...
- 简单主机批量管理工具(这里实现了paramiko 用su切换到root用户)
项目名:简单主机批量管理工具 一.需求 1.主机分组 2.可批量执行命令.发送文件,结果实时返回,执行格式如下 batch_run -h h1,h2,h3 -g web_clusters,db_ ...
随机推荐
- Cassandra - Insert after Delete fails silently
在delete一条数据后,再insert 相同内容的数据,结果看起来是成功的,但是当你去查找这个数据,却没有任何内容,整个过程并且没有任何异常提示. 这往往发生在单元测试的时候,我们反复清理和写入数据 ...
- 第18讲——string类
关键字:string类 字符串 C-风格字符串 C库字符串函数 字符串:存储在内存的连续字节中的一系列字符. C++处理字符串的方式有两种: 来自C语言,常被称为C-风格字符串: 基于strin ...
- HDU 1028 整数拆分问题 Ignatius and the Princess III
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- fork开源代码后如何基于某个tag建立自己的branch
应用场景: 在github上fork一个自己想看的开源项目,想基于某个tag来写一些测试demo,然后可以做到版本控制. 方法: //克隆 git clone xxxxx.git //查看tag gi ...
- DataGridView加载gif图片
当我们想加载图片时,一般情况下都会使用picturebox控件,这个控件可以加载各种格式的图片,当然也包括gif图片.但是有时,我们也希望一些数据展示控件也可以加载图片,比如说DataGridView ...
- Flink State的两张图
streamTask的invoke方法中,会循环去调用task上的每个operator的initializeState方法,在这个方法中,会真正创建除了savepointStream的其他三个对象, ...
- js & disabled mouse right button menus
js & disabled mouse right button menus 网页可以屏蔽 F12 https://www.cnblogs.com/Marydon20170307/p/9122 ...
- React & styled component
React & styled component https://www.styled-components.com/#your-first-styled-component tagged t ...
- P3434 [POI2006]KRA-The Disks
题目描述 For his birthday present little Johnny has received from his parents a new plaything which cons ...
- JDBC连接数据库的过程
以连接MySQL为例: (1)加载MySQL数据库连接的驱动程序.到MySQL官网下载该驱动程序jar包,然后把包复制到WEB-INF/lib目录下,则JDBC会调用Class.forName()方法 ...