类似fabric主机管理demo
类似于fabric的主机管理系统
可以批量对主机进行操作
- 批量上传文件
- 批量下载文件
- 批量执行命令
demo代码
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author : Leon
# Date 2017/6/27
import threading
import paramiko
from conf import HOST_MAP
lock = threading.Lock()
class MyFabric(object):
'''MyFabric'''
def __init__(self):
self.show()
self.run()
def run(self):
while True:
ChoiceList = input("请输入选择的主机编号,一个或多个,多个请空格隔开,一个组请以group作为关键字开始[exit退出]:").strip().split()
if ChoiceList[0] == "exit":
exit("Bye")
elif ChoiceList[0] == "group":
self.ChoiceHost = [] #清空
GroupName = ChoiceList[1]
for host in HOST_MAP:
if HOST_MAP[host]["group"] == GroupName:
self.ChoiceHost.append(host)
print("经过输入帅选,确认选择的主机以及编号".center(25, '#'))
for index in self.ChoiceHost:
print("[{host}]".format(host=index))
confirm = input("请确认[yes/no]:").strip().lower()
if confirm:
if confirm == "yes":
# 选择想要执行的动作
action = input("请输入想要执行的动作(put/get/cmd):").strip()
if hasattr(self, action):
func = getattr(self, action)
func()
print("\033[36mThe End\033[0m".center(30, '-'))
elif confirm == "no":
continue
else:
self.ChoiceHost = []
for item in self.IndexHOST_MAP:
if str(self.IndexHOST_MAP[item]["index"]) in ChoiceList:
self.ChoiceHost.append(item)
print("经过输入帅选,确认选择的主机以及编号".center(25, '#'))
for index in self.ChoiceHost:
print("[{host}]".format(host=index))
confirm = input("请确认[yes/no]:").strip().lower()
if confirm:
if confirm == "yes":
# 选择想要执行的动作
action = input("请输入想要执行的动作(put/get/cmd):").strip()
if hasattr(self, action):
func = getattr(self, action)
func()
print("\033[36mThe End\033[0m".center(30, '-'))
elif confirm == "no":
continue
def show(self):
self.IndexHOST_MAP = HOST_MAP
print("\033[32mhost list\033[0m".center(20, '-'))
for index,host in enumerate(HOST_MAP):
print("[{index}]:{host}".format(index=index, host=host))
self.IndexHOST_MAP[host]["index"] = index #添加一个index字段
def _put_file(self,host,port,user,password,LocalPath,ServerPath):
try:
t = paramiko.Transport((host,port))
t.connect(username=user, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(LocalPath, ServerPath)
t.close()
print("\033[32m从[{host}]上传成功!\033[0m".format(host=host))
return True
except Exception as e:
print(e)
print("\033[31m从[{host}]上传失败!\033[0m".format(host=host))
return False
def _get_file(self,host,port,user,password,ServerPath,LocalPath):
try:
t = paramiko.Transport((host, port))
t.connect(username=user, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get(ServerPath, LocalPath)
t.close()
print("\033[32m从[{host}]下载成功!\033[0m".format(host=host))
return True
except Exception as e:
print(e)
print("\033[31m从[{host}]下载失败!\033[0m".format(host=host))
return False
def _exec_command(self,host,port,user,password,command):
lock.acquire()
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(host, port, user, password)
std_in, std_out, std_err = ssh_client.exec_command(command)
print("\033[32m[{host}]命令执行成功\033[0m".format(host=host))
for line in std_out:
print(line.strip("\n"))
ssh_client.close()
except Exception as e:
print(e)
print("\033[31m[{host}]命令执行失败\033[0m".format(host=host))
lock.release()
def get(self):
ServerPath = input("请输入需要批量下载的远程文件文件名[eg:/etc/passwd]:").strip()
LocalPath = input("请输入文件本地存放的目录以及文件名[eg:/home/passwd]:").strip()
ThreadList = []
for item in self.ChoiceHost:
t = threading.Thread(target=self._get_file,args=(item,
HOST_MAP[item]["port"],
HOST_MAP[item]["username"],
HOST_MAP[item]["password"],
ServerPath,LocalPath+"_from_"+item,))
ThreadList.append(t)
for t in ThreadList:
t.start()
t.join()
def put(self):
LocalPath = input("请输入需要上传的本地文件地址[eg:/home/passwd]:").strip()
ServerPath = input("请输入远程存放目录以及文件名[eg:/etc/passwd]").strip()
ThreadList = []
for item in self.ChoiceHost:
t = threading.Thread(target=self._put_file, args=(item,
HOST_MAP[item]["port"],
HOST_MAP[item]["username"],
HOST_MAP[item]["password"],
LocalPath,
ServerPath,))
ThreadList.append(t)
for t in ThreadList:
t.start()
def cmd(self):
command = input("请输入需要批量执行的命令:[eg:hostanme]").strip()
ThreadList = []
for item in self.ChoiceHost:
t = threading.Thread(target=self._exec_command, args=(item,
HOST_MAP[item]["port"],
HOST_MAP[item]["username"],
HOST_MAP[item]["password"],
command,))
ThreadList.append(t)
for t in ThreadList:
t.start()
t.join()
if __name__ == '__main__':
obj = MyFabric()
配置文件
HOST_MAP = {
"10.215.24.30":{
"port":22,
"username":"root",
"password":"123456",
"group":"test"
},
"10.215.24.31":{
"port":22,
"username":"root",
"password":"123456",
"group":"test1"
},
"10.215.24.32": {
"port": 22,
"username": "root",
"password": "123456",
"group": "test1"
}
}
涉及的知识点
- paramiko模块的使用
- 多线程
- 反射
类似fabric主机管理demo的更多相关文章
- paramiko类Fabric主机管理
环境:Linux python3.5 要求:类 Fabric 主机管理程序开发:1. 运行程序列出主机组或者主机列表2. 选择指定主机或主机组3. 选择让主机或者主机组执行命令或者向其传输文件(上传/ ...
- python10作业思路及源码:类Fabric主机管理程序开发(仅供参考)
类Fabric主机管理程序开发 一,作业要求 1, 运行程序列出主机组或者主机列表(已完成) 2,选择指定主机或主机组(已完成) 3,选择主机或主机组传送文件(上传/下载)(已完成) 4,充分使用多线 ...
- python作业类Fabric主机管理程序开发(第九周)
作业需求: 1. 运行程序列出主机组或者主机列表 2. 选择指定主机或主机组 3. 选择让主机或者主机组执行命令或者向其传输文件(上传/下载) 4. 充分使用多线程或多进程 5. 不同主机的用户名密码 ...
- EasyUI+MVC+EF简单用户管理Demo(问题及解决)
写在前面 iframe-src EntityFramework版本 connectionStrings View.Action.页面跳转 EasyUI中DataGrid绑定 新增.修改和删除数据 效果 ...
- 第一章 权限管理DEMO简介
源代码GitHub:https://github.com/ZhaoRd/Zrd_0001_AuthorityManagement 1.系列介绍 工作已有五年之久,一直有想通过博客写点自己知道的,在博客 ...
- LNMP服务器虚拟主机管理lnmp
安装 系统需求: 需要2 GB硬盘剩余空间 安装步骤: 1.使用putty或类似的SSH工具登陆:登陆后运行:screen -S lnmp如果提示screen命令不存在可以执行:yum install ...
- mvc 权限管理 demo
http://blog.csdn.net/zht666/article/details/8529646 new http://www.cnblogs.com/fengxing/archive/2012 ...
- Windows 2008 R2 X64 安装WebsitePanel(WSP虚拟主机管理面板)
Windows 2008 R2 X64 安装WebsitePanel(WSP2.0虚拟主机管理面板) 估计很多同学都还不知道WebsitePanel是什么东东吧,Web ...
- (转载)运行主机管理在openvswitch之上
在这篇文章里介绍了如果运行主机管理在openvswitch之上,而不是单独配置一个物理网卡用于主机管理,并且所有的vm的流量还是通过openvswitch走的. Running Host Manage ...
随机推荐
- Nagios状态长时间处于Pending的解决方法
1 nagios 守护进程引起的一系列问题 1 影响nagios web页面收集监控信息 致使页面出现时而收集不到服务信息 2 影响pnp查看图形化,出图缓慢 3 影响查看服务状态信息,致使有时候查看 ...
- web.config文件中配置数据库连接的两种方式
web.config文件中配置数据库连接的两种方式 标签: 数据库webconfig 2015-04-28 18:18 31590人阅读 评论(1)收藏举报 分类: 数据库(74) 在网站开发 ...
- 记React+.NetCore API实现动态列导出
1.效果演示 2.用到的第三方类库 前端:React,Dva,Antd 后端:ASP.NET CORE,System.Linq.Dynamic.Core,EPPlus.Core 3.基本思路 第一:E ...
- java之Servlet监听器Listener
常用应用场景:单点登录.统计在线人数 一.简介 (一)概述 1.Listener 用于监听 java web程序中的事件,例如创建.修改.删除Session.request.context等,并触发响 ...
- iOS-xcode代码统计
作为开发者,想不想知道自己写了多少行代码吗,打开终端,进入项目文件夹,然后进入想统计的某个文件夹,也可以直接在当前项目文件夹,然后终端输入下面的代码就可以了 find . "(" ...
- Go笔记-垃圾回收集和SetFinalizer
[垃圾回收] 1- Go的开发者也不用写代码来释放程序中不再使用的变量和结构占用内存,Go中有独立的进程,垃圾回收器(GC),处理这些事情.它会搜索不再使用的变量然后释放它们. 2- ...
- es head插件通过Nginx http basic 限制访问
原文链接: http://www.sojson.com/blog/213.html
- CF781D Axel and Marston in Bitland [倍增 矩阵乘法 bitset]
Axel and Marston in Bitland 好开心第一次补$F$题虽然是$Div.2$ 题意: 一个有向图,每条边是$0$或$1$,要求按如下规则构造一个序列然后走: 第一个是$0$,每次 ...
- The SSL certificate used to load resources from xxx will be distrusted in M70.
今天在浏览网站的时候遇到如下报警信息: The SSL certificate used to load resources from https://xxx.com will be distrust ...
- QT开发应用程序的欢迎界面
主界面启动太慢,通常要10秒以上,所以想加个欢迎界面,等程序加载好再显示主界面. 主界面(类名为MainWindow)启动慢的原因是构造函数需要执行大量初始化的工作. 创建了Welcome类作为欢迎界 ...