


Share examples about the development of Fabric-like host management programs
Fabric-like host management program development:
1. Run the program to list the host group or host list
2. Select the specified host or host group
3. Select to let the host or host group execute commands or send commands to Its transfer file (upload/download)
4. Make full use of multi-threading or multi-process
5. The username, password, and port of different hosts can be different
README
类 Fabric 主机管理程序 执行命令(SSH) 向其传输文件(上传/下载) Fabric/#程序目录 |- - -__init__.py |- - -bin/#启动目录 | |- - -__init__.py | |- - -Fabric_start.py#视图启动 | |- - -user_reg.py#主机添加启动 | |- - -cfg/#配置目录 | |- - -__init__.py | |- - -config.py#配置文件 | |- - -core/#下载文件目录 | |- - -__init__.py | |- - -main.py#主要逻辑 类 | |- - -db/#主机列表文件目录 | |- - - | |- - -get_file/#下载文件目录 | | |- - -put_file/#上传文件目录 |- - -REDMAE
Fabric/#程序目录 |- - -__init__.py |- - -bin/#启动目录 | |- - -__init__.py | |- - -Fabric_start.py#视图启动


1 import configparser2 import os ,sys3 BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#获取相对路径转为绝对路径赋于变量4 sys.path.append(BASE_DIR)#增加环境变量5 from core.main import loging6 if __name__ == '__main__':7 8 loging()
| |- - -user_reg.py#主机添加启动


1 #!usr/bin/env python 2 #-*-coding:utf-8-*- 3 # Author calmyan 4 5 import configparser 6 import os ,sys 7 BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#获取相对路径转为绝对路径赋于变量 8 sys.path.append(BASE_DIR)#增加环境变量 9 from cfg import config10 #修改个信息 磁盘大小11 def set_info(gr_name,addse,name,pwd,ports):12 config_info=configparser.ConfigParser()#读数据13 file_dir='%s%s'%(config.AUTH_FILE,gr_name)#主机组用户名密码文件路径14 15 config_info[addse]={}#ip 主机16 config_info.set(addse,config.USER,name)#用户17 config_info.set(addse,config.PWD,pwd)#密码18 config_info.set(addse,config.PORTS,ports)#端口19 with open(file_dir,'a') as f:20 config_info.write(f)#写入文件21 #config_info.write(open(file_dir,'a'))#写入文件22 print('创建完成'.center(60,'='))23 print('组:【%s】\nIP:[%s]\n用户名:[%s]\n密码:[%s]\n端口:[%s]'%(gr_name,addse,name,pwd,ports))24 25 if __name__ == '__main__':26 gr_name=input('组名:')#组27 addse=input('IP地址:')#ip地址28 name=input('用户名:')#用户29 pwd=input('密码:')#密码30 ports=input('端口:')#端口31 32 set_info(gr_name,addse,name,pwd,ports)
|- - -cfg/#配置目录 | |- - -__init__.py |- - -config.py#配置文件


1 #!usr/bin/env python 2 #-*-coding:utf-8-*- 3 # Author calmyan 4 5 import configparser 6 import os ,sys 7 BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#获取相对路径转为绝对路径赋于变量 8 sys.path.append(BASE_DIR)#增加环境变量 9 10 AUTH_FILE='%s/db/'%BASE_DIR#主机组 信息用户名密码文件路径11 FILE_DIR='%s/put_file'%BASE_DIR#要上传文件所在的目录12 GET_FILE_DIR='%s/get_file'%BASE_DIR#要上传文件所在的目录13 #print(AUTH_FILE)14 PWD='pwd'#密码15 USER='user'16 PORTS='ports'17 INST_LIST=['put','get']#指令列表18 19 PUT='put'20 GET='get'
|- - -core/#下载文件目录 | |- - -__init__.py | |- - -main.py#主要逻辑 类


1 #!usr/bin/env python 2 #-*-coding:utf-8-*- 3 # Author calmyan 4 5 import configparser 6 import os ,sys 7 import threading,time 8 import paramiko,queue 9 BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#获取相对路径转为绝对路径赋于变量 10 sys.path.append(BASE_DIR)#增加环境变量 11 from cfg import config 12 13 class Fabric_gr(object): 14 def __init__(self,gr_name):#组名 15 self.gr_name='%s%s'%(config.AUTH_FILE,gr_name)#主机组用户名密码文件路径 16 self.config_info=configparser.ConfigParser()#读数据对象 17 self.name_l=[]#定义一个列表 18 self.attr=[] 19 self.file_dir=''#上传文件路径 20 self.get_file=''#下载传文件路径 21 22 def group_open(self):#打开组文件 23 self.config_info.read(self.gr_name)#读取文件 24 for i in range(len(self.config_info.sections())): 25 self.name_l.append(self.config_info.sections()[i])#信息添加到列表 26 else: 27 print('主机列表:'.center(40,'=')) 28 for i in self.name_l: 29 print(('[%s]'%i).center(40,' ')) 30 31 def inst_attr(self,inst):#获取指令 32 self.instruction=inst 33 self.attr=self.instruction.split() 34 self.inst_a=self.attr[0] 35 36 def inst(self):#指令判断 37 if self.inst_a in config.INST_LIST: 38 return True 39 else: 40 return False 41 42 def open_list(self):#创建 线程 方法 43 if self.inst_a==config.PUT: 44 if self.File_Dir():#查找本地文件 45 pass 46 else: 47 return 48 self.re_lilst=[]#定义一个列表 49 for j in range(len(self.name_l)): 50 sttr=self.config_info.sections()[j]#获取到对象 51 user_dict={}#创建一个空字典 52 for i,v in self.config_info[sttr].items():#可以循环输出 获ip 用户 密码 端口 53 user_dict[i]=v 54 sttr=threading.Thread(target=self.thr_run,args=(sttr,user_dict[config.USER],user_dict[config.PWD],int(user_dict[config.PORTS])))#创建新线程 55 sttr.start()#启动线程 56 self.re_lilst.append(sttr)#不用JOIN,避免阻塞为串行 57 else: 58 for i in self.re_lilst:#等待线程 完成 59 i.join() 60 61 def open_list2(self):#创建 线程 方法 62 self.re_lilst=[]#定义一个列表 63 for j in range(len(self.name_l)): 64 sttr=self.config_info.sections()[j]#获取到对象 65 user_dict={}#创建一个空字典 66 for i,v in self.config_info[sttr].items():#可以循环输出 获ip 用户 密码 端口 67 user_dict[i]=v 68 sttr=threading.Thread(target=self.ssh_run,args=(sttr,user_dict[config.USER],user_dict[config.PWD],int(user_dict[config.PORTS])))#创建新线程 69 sttr.start()#启动线程 70 self.re_lilst.append(sttr)#不用JOIN,避免阻塞为串行 71 else: 72 for i in self.re_lilst:#等待线程 完成 73 i.join() 74 75 76 def thr_run(self,addrs,user,paswd,ports):#传输通道 77 try: 78 transport=paramiko.Transport((addrs,ports))#传输模块 Transport 服务器地址 端口 79 transport.connect(username=user,password=paswd)#用户名,,密码 80 sftp=paramiko.SFTPClient.from_transport(transport)#调用传输方法 81 print('[%s]连接成功!'%addrs) 82 self.file_dir='%s/%s'%(config.FILE_DIR,self.attr[1])#上传文件路径 83 if self.inst_a==config.PUT: 84 sftp.put(self.file_dir,self.attr[2])#上传文件 ,本地路径文件 ,服务器的路径文件 85 print('【%s】文件上传完成!'%addrs) 86 elif self.inst_a==config.GET: 87 self.get_file='%s/%s_%s'%(config.GET_FILE_DIR,addrs,self.attr[2])#下载文件路径 88 print(self.get_file) 89 sftp.get(self.attr[1],self.get_file)#下载文件 ,服务器的路径文件 ,本地路径文件 90 print('【%s】文件下载完成!'%addrs) 91 else: 92 print('【%s】文件相关操作失败!'%addrs) 93 pass 94 except Exception as e: 95 print(e) 96 97 def File_Dir(self):#判断文件是否存在 98 file=self.attr[1] 99 100 print(file)101 self.file_dir='%s/%s'%(config.FILE_DIR,file)#文件路径102 if os.path.isfile(self.file_dir):103 print('成功找到文件!')104 return True105 else:106 print('文件不存在!')107 return False108 def ssh_run(self,addrs,user,paswd,ports):#ssh109 ssh =paramiko.SSHClient()#创建一个SSH连接对象110 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())#允许连接不在KNOV_HOSTs文件中的主机 自动添加111 try:112 ssh.connect(hostname=addrs,port=ports,username=user,password=paswd)#连接,主机 端口 用户名 密码113 print('[%s]连接成功!'%addrs)114 except Exception as e:115 print(e)116 return117 stdin,stdout,stderr=ssh.exec_command(self.instruction)#.exec_command 为执行命令,返回结果 ,标准输入,标准输出,标准错误,错误与输出只会返回其一118 result=stdout.read()#获取结果119 try:120 if len(result)>>:')162 if inst=='exit':163 exit()164 if inst=='quit':165 continue166 if inst=='helps':167 print(info_l)168 continue169 lst.inst_attr(inst)#获取指令170 if lst.inst():#指令判断171 lst.open_list()#开启线程创建172 else:173 lst.open_list2()174 pass175 except ValueError as e:176 print(e)
The above is the detailed content of Share examples about the development of Fabric-like host management programs. For more information, please follow other related articles on the PHP Chinese website!

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6
Visual web development tools