需求
1. 运行程序列出主机组或者主机列表2. 选择指定主机或主机组3. 选择让主机或者主机组执行命令或者向其传输文件(上传/下载)4. 充分使用多线程或多进程5. 不同主机的用户名密码、端口可以不同
目录结构
简单主机批量管理工具 ├ bin # 执行文件目录 | └ SBHM.py # 执行程序 ├ conf # 配置文件目录 | ├ hosts.ini # 配置文件。目前主要保存主机连接相关信息 | └ init_conf.py # 配置文件格式化程序 └ core # 程序核心代码位置 └ main.py # 主逻辑交互程序 代码
import os,sys basepath = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0,basepath) from core import main main.main()
SBHM.py
import configparser c = configparser.ConfigParser() c[‘proxy‘]={ ‘proxy1‘ : { ‘ip‘:‘192.168.1.76‘, ‘port‘:22, ‘username‘:‘root‘, ‘password‘:‘123456‘ }, ‘proxy2‘ : { ‘ip‘: ‘192.168.1.12‘, ‘port‘: 51123, ‘username‘: ‘root‘, ‘password‘: ‘123456‘ }, ‘proxy3‘: { ‘ip‘: ‘192.168.1.34‘, ‘port‘: 22, ‘username‘: ‘root‘, ‘password‘: ‘123456‘ } } c[‘upstream‘] = { ‘img‘: { ‘ip‘: ‘192.168.1.76‘, ‘port‘: 22, ‘username‘: ‘root‘, ‘password‘: ‘123456‘ }, ‘video‘: { ‘ip‘: ‘192.168.1.12‘, ‘port‘: 51123, ‘username‘: ‘root‘, ‘password‘: ‘123456‘ }, ‘static‘: { ‘ip‘: ‘192.168.1.34‘, ‘port‘: 22, ‘username‘: ‘root‘, ‘password‘: ‘123456‘ } } with open(‘hosts.ini‘,‘w‘,encoding=‘utf-8‘) as f: c.write(f)
init_conf.py
import configparser c = configparser.ConfigParser() c[‘proxy‘]={ ‘proxy1‘ : { ‘ip‘:‘192.168.1.76‘, ‘port‘:22, ‘username‘:‘root‘, ‘password‘:‘123456‘ }, ‘proxy2‘ : { ‘ip‘: ‘192.168.1.12‘, ‘port‘: 51123, ‘username‘: ‘root‘, ‘password‘: ‘123456‘ }, ‘proxy3‘: { ‘ip‘: ‘192.168.1.34‘, ‘port‘: 22, ‘username‘: ‘root‘, ‘password‘: ‘123456‘ } } c[‘upstream‘] = { ‘img‘: { ‘ip‘: ‘192.168.1.76‘, ‘port‘: 22, ‘username‘: ‘root‘, ‘password‘: ‘123456‘ }, ‘video‘: { ‘ip‘: ‘192.168.1.12‘, ‘port‘: 51123, ‘username‘: ‘root‘, ‘password‘: ‘123456‘ }, ‘static‘: { ‘ip‘: ‘192.168.1.34‘, ‘port‘: 22, ‘username‘: ‘root‘, ‘password‘: ‘123456‘ } } with open(‘hosts.ini‘,‘w‘,encoding=‘utf-8‘) as f: c.write(f)
main.py
时间: 2025-01-01 22:54:52