python 学习分享-实战篇类 Fabric 主机管理程序开发
# 类 Fabric 主机管理程序开发:
# 1. 运行程序列出主机组或者主机列表
# 2. 选择指定主机或主机组
# 3. 选择让主机或者主机组执行命令或者向其传输文件(上传/下载)
# 4. 充分使用多线程或多进程
# 5. 不同主机的用户名密码、端口可以不同
import paramiko,threading,pickle,os Base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
config_path = Base_path +'/core/config'
#主机配置信息管理
list_host = [{'host':'192.168.117.236','port':22,'user':'ubuntu1','password':'ubuntu1','group':1},
{'host':'192.168.117.237','port':22,'user':'ubuntu2','password':'ubuntu2','group':1},
{'host':'192.168.117.239','port':22,'user':'ubuntu3','password':'ubuntu3','group':2},
{'host':'192.168.117.240','port':22,'user':'aaa','password':'bbb','group':2}]
def pickle_dump(path,file):
#存入配置文件
with open(path,'wb') as f:
pickle.dump(file,f)
#pickle_dump(config_path,list_host)
def pickle_load(path):
#取出配置文件
with open(path,'rb') as f:
list_host_new = pickle.load(f)
return list_host_new
# list_host_new = pickle_load(config_path)
# print(list_host_new) class Paramiko_sshd(object):
#sshd类
def __init__(self,host,port,user,password):
self.host = host
self.port = port
self.user = user
self.password = password def sshd_command(self,command):
#ssh远程主机执行命令,并返回结果
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(self.host,self.port,self.user,self.password)
std_in,std_out,std_err = ssh_client.exec_command(command)
print(self.host.center(30,'*'))
print(command.upper().center(30,'='))
for line in std_out:
print(line.strip('\n'))
ssh_client.close()
except Exception as e:
print(e)
def sshd_upload_file(self,server_path,local_path):
#上传文件
try:
t = paramiko.Transport((self.host,self.port))
t.connect(username=self.user,password=self.password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(local_path,server_path)
t.close()
print(self.host.center(30, '*'))
print('上传成功')
except Exception as e:
print(e)
def sshd_down_file(self,server_path,local_path):
#下载文件
try:
t = paramiko.Transport((self.host, self.port))
t.connect(username=self.user, password=self.password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get(server_path, local_path)
t.close()
print(self.host.center(30, '*'))
print('下载成功')
except Exception as e:
print(e)
def help():
#帮助信息
command_option = input('''
*************请选择***********
1.执行命令
2.上传文件
3.下载文件
4.返回
5.退出程序
''').strip()
return command_option def review():
while 1:
#展示界面
list_host_new = pickle_load(config_path)
host_group_1 = []
host_group_2 = []
for line in list_host_new:
if line['group'] == 1:
host_group_1.append(line)
else:
host_group_2.append(line)
print('group-1'.center(30,'='))
for line_1 in host_group_1:
print('host:',line_1['host'],'port:',line_1['port'])
print('group-2'.center(30,'='))
for line_2 in host_group_2:
print('host:',line_2['host'],'port:',line_2['port']) group_select = input('请输入您要操作的主机组:\033[1;35m group-1 \033[0m | \033[1;35m group-2 \033[0m '
'或输入\033[1;35mexit\033[0m退出程序 ==>').strip()
if group_select in ['group-1','group-2']:
if group_select == 'group-1': #增加中间变量,来确定是对组一操作还是对组二进行操作
group_sure = host_group_1
else:
group_sure = host_group_2
while 1:
command_option = help()
if command_option == '':
command = input('请输入执行的命令:').strip()
for line_3 in group_sure:
sshd = Paramiko_sshd(line_3['host'],line_3['port'],line_3['user'],line_3['password'])
t = threading.Thread(target=sshd.sshd_command,args=(command,))
t.start()
t.join() elif command_option == '':
server_path = input('请输入上传到的服务器目录:').strip()
local_path = input('请输入本地文件目录:').strip()
for line_3 in group_sure:
sshd = Paramiko_sshd(line_3['host'],line_3['port'],line_3['user'],line_3['password'])
t = threading.Thread(target=sshd.sshd_upload_file,args=(server_path,local_path,))
t.start()
t.join() elif command_option == '':
server_path = input('请输入下载的服务器目录文件路径:').strip()
local_path = input('请输入存放到本地的路径:').strip()
for line_3 in group_sure:
sshd = Paramiko_sshd(line_3['host'],line_3['port'],line_3['user'],line_3['password'])
t = threading.Thread(target=sshd.sshd_down_file,args=(server_path,local_path,))
t.start()
t.join()
elif command_option == '':
break
elif command_option == '':
exit()
else:
print('%s为非法命令,请重新输入!'%command_option)
elif group_select == 'exit':
break
else:
print('%s为非法命令,请重新输入!'%group_select) if __name__ =='__main__':
review()
python 学习分享-实战篇类 Fabric 主机管理程序开发的更多相关文章
- python作业类Fabric主机管理程序开发(第九周)
作业需求: 1. 运行程序列出主机组或者主机列表 2. 选择指定主机或主机组 3. 选择让主机或者主机组执行命令或者向其传输文件(上传/下载) 4. 充分使用多线程或多进程 5. 不同主机的用户名密码 ...
- python10作业思路及源码:类Fabric主机管理程序开发(仅供参考)
类Fabric主机管理程序开发 一,作业要求 1, 运行程序列出主机组或者主机列表(已完成) 2,选择指定主机或主机组(已完成) 3,选择主机或主机组传送文件(上传/下载)(已完成) 4,充分使用多线 ...
- 类 Fabric 主机管理程序开发
类 Fabric 主机管理程序开发:1. 运行程序列出主机组或者主机列表2. 选择指定主机或主机组3. 选择让主机或者主机组执行命令或者向其传输文件(上传/下载4. 充分使用多线程或多进程5. 不同主 ...
- python 学习分享-实战篇选课系统
# 角色:学校.学员.课程.讲师 # 要求: # 1. 创建北京.上海 2 所学校 # 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 # ...
- python 学习分享-实战篇高级的ftp
#server代码 import socketserver,os,hashlib Base_paht = os.path.dirname(os.path.dirname(os.path.abspath ...
- python 学习分享-实战篇简单的ftp
import socket import os import time import pickle Basedb = os.path.dirname(os.path.dirname(os.path.a ...
- python 学习分享-实战篇增删改查作业
一大波函数来袭 作业要求: 1本次作业通过空格及逗号,将文件拆分成列表,在通过判断add.del.update.select等关键字,来判断用户执行的是哪种命令,根据不同的命令调用不同的函数去处理. ...
- python第五十二天---第九周作业 类 Fabric 主机管理程序
类 Fabric 主机管理程序开发:1. 运行程序列出主机组或者主机列表2. 选择指定主机或主机组3. 选择让主机或者主机组执行命令或者向其传输文件(上传/下载)4. 充分使用多线程或多进程5. 不同 ...
- python 学习分享-函数篇
函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可以自己创建函数,这 ...
随机推荐
- linux命令之grep命令
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...
- 通过WEB网管登录
6.1 通过WEB网管登录简介 S5100-SI/EI系列以太网交换机提供内置的WEB Server,用户可以通过WEB网管终端(PC)登录到交换机上,利用内置的WEB Server以WEB方式直观 ...
- EF 连接 mysq l数据库 code first模式 的实践
准备工作: 1.下载vs2015 2.下载mysql2017 3.安装 开始: 1.创建 控制台文件 2.添加引用 Mysql.Data , Mysql.Data.Entity.EF6,Mysql.w ...
- _default_ VirtualHost overlap on port 80, the first has precedence
去掉#NameVirtualHost *:80,然后重启httpd
- left join后面加上where条件浅析
select a.*,b.* from table1 a left join table2 b on b.X=a.X where XXX 如上:一旦使用了left join,没有where条件时,左表 ...
- 项目 XXX 受源代码管理。向源代码管理注册此项目时出错。建议不要对此项目进行任何修改
原本带vss或者svn管理的项目独立复制出来后,如果出现下面问题 解决办法: 使用记事本打开,项目csproj文件删除图中
- 重新认识下数组的concat方法
最近在学习react,看官方文档的时候,有一个例子中的一句话让我困惑.就是讲todoList的例子 concat不是连接数组的吗?看了一下concat的介绍 数组虽然是对象类型,但是对象毕竟不是数组啊 ...
- 模拟ie9的placeholder
ie9 的input框没有placeholder属性 啧啧啧~~~ 所以就用span标签来模拟一下 先判断浏览器类型 if(navigator.useAgent.indexOf("MSIE ...
- 内置函数系列之 sorted排序
sorted排序函数语法: sorted(可迭代对象,key=函数(默认为None),reverse=False) 将可 迭代对象的每一个元素传进key后面的函数中,根据函数运算的结果(返回值)进行排 ...
- thymelef模板报错 the entity name must immediately follow the '&' in the entity reference
thymelef模板里面是不能实用&符号的 要用&转义符代替,官网也有文档说明可以用官方的通配符代替,官方文档http://www.thymeleaf.org/doc/tutorial ...