当前位置: 首页 > wzjs >正文

书店中文网站模板互联网营销师培训费用是多少

书店中文网站模板,互联网营销师培训费用是多少,网站整体运营,国内 免费 云服务器目录 脚本概述 代码解析 导入模块 日志配置 核心函数config_device 主程序逻辑 使用说明 脚本优化建议 完整代码 脚本概述 这是一个使用Python编写的网络设备批量配置脚本,主要功能是通过SSH协议批量登录多台网络设备(如路由器、交换机等&…

目录

  1. 脚本概述

  2. 代码解析

    • 导入模块

    • 日志配置

    • 核心函数config_device

    • 主程序逻辑

  3. 使用说明

  4. 脚本优化建议

  5. 完整代码

脚本概述

这是一个使用Python编写的网络设备批量配置脚本,主要功能是通过SSH协议批量登录多台网络设备(如路由器、交换机等),并执行预定义的配置命令。脚本采用了多线程技术,可以同时管理多台设备,大大提高了批量操作的效率。

代码解析

导入模块

import paramiko
import getpass
import logging
import time
from concurrent.futures import ThreadPoolExecutor
  • paramiko: 用于实现SSH协议的Python库,提供SSH客户端功能

  • getpass: 安全获取用户输入的密码(虽然本脚本中未直接使用)

  • logging: Python标准日志模块,用于记录操作日志

  • time: 提供时间相关功能,这里主要用于命令执行间隔

  • ThreadPoolExecutor: 来自concurrent.futures模块,实现线程池功能

日志配置

logging.basicConfig(filename='network_ops.log',level=logging.INFO,format='%(asctime)s-%(levelname)s-%(message)s'
)

配置日志系统:

  • 日志输出到文件network_ops.log

  • 日志级别为INFO(记录一般操作信息)

  • 日志格式包含时间戳、日志级别和消息内容

核心函数config_device

def config_device(ip,username,password,commands):try:client=paramiko.SSHClient()client.set_missing_host_key_policy(paramiko.AutoAddPolicy())client.connect(hostname=ip,username=username,password=password,timeout=10)logging.info(f'Successfully connected to {ip}')shell=client.invoke_shell()for cmd in commands:shell.send(cmd+'\n')time.sleep(1)output=shell.recv(65535).decode('utf-8')logging.info(f'{ip} output:\n{output}')except Exception as e:logging.error(f'{ip} failed:{str(e)}')finally:client.close()

函数功能解析:

  1. 创建SSHClient对象并设置自动添加主机密钥策略

  2. 连接目标设备(IP、用户名、密码),设置10秒超时

  3. 获取交互式shell会话

  4. 循环发送每条命令,每条命令间隔1秒

  5. 接收命令输出并记录到日志

  6. 异常处理:记录连接或执行失败的原因

  7. 最终确保SSH连接被关闭

主程序逻辑

if __name__=='__main__':username=input('username:')password=getpass.getpass('password:')with open(r'C:\Users\23608\Desktop\ipadd_python.txt','r')as f:ips=[line.strip() for line in f.readlines()]with open(r'C:\Users\23608\Desktop\cmd_file.txt','r')as f:commands=[line.strip() for line in f.readlines()]with ThreadPoolExecutor(max_workers=5) as excutor:for ip in ips:excutor.submit(config_device,ip,username,password,commands)

执行流程:

  1. 获取用户名和密码

  2. 从两个文本文件读取:

    • ipadd_python.txt: 存储设备IP地址列表

    • cmd_file.txt: 存储要执行的命令列表

  3. 创建最大5个线程的线程池

  4. 为每个IP提交一个配置任务到线程池

使用说明

  1. 准备两个文本文件:

    • ipadd_python.txt: 每行一个设备IP地址

    • cmd_file.txt: 每行一条要执行的命令

  2. 运行脚本,输入设备的用户名和密码

  3. 查看network_ops.log文件获取执行结果

完整代码

import paramiko
import getpass
import logging
import time
from concurrent.futures import ThreadPoolExecutorlogging.basicConfig(filename='network_ops.log',level=logging.INFO,format='%(asctime)s-%(levelname)s-%(message)s'
)def config_device(ip,username,password,commands):try:client=paramiko.SSHClient()client.set_missing_host_key_policy(paramiko.AutoAddPolicy())client.connect(hostname=ip,username=username,password=password,timeout=10)logging.info(f'Successfully connected to {ip}')shell=client.invoke_shell()for cmd in commands:shell.send(cmd+'\n')time.sleep(1)output=shell.recv(65535).decode('utf-8')logging.info(f'{ip} output:\n{output}')except Exception as e:logging.error(f'{ip} failed:{str(e)}')finally:client.close()if __name__=='__main__':username=input('username:')password=getpass.getpass('password:')with open(r'C:\Users\23608\Desktop\ipadd_python.txt','r')as f:ips=[line.strip() for line in f.readlines()]with open(r'C:\Users\23608\Desktop\cmd_file.txt','r')as f:commands=[line.strip() for line in f.readlines()]with ThreadPoolExecutor(max_workers=5) as excutor:for ip in ips:excutor.submit(config_device,ip,username,password,commands)

文章转载自:

http://mAf5i2h0.ntqLz.cn
http://bupTSlZf.ntqLz.cn
http://7toUrVma.ntqLz.cn
http://tCMcax5A.ntqLz.cn
http://BbqKegnF.ntqLz.cn
http://rCQuO7rJ.ntqLz.cn
http://0geFwRjM.ntqLz.cn
http://CHruQSvE.ntqLz.cn
http://k1hhY23Z.ntqLz.cn
http://rAoaf4Li.ntqLz.cn
http://jYkn9YTK.ntqLz.cn
http://0GIxSGaq.ntqLz.cn
http://o1jO5CRT.ntqLz.cn
http://8HZyklLc.ntqLz.cn
http://xhiGENSy.ntqLz.cn
http://Yp9ZOqJC.ntqLz.cn
http://zlZGZfCv.ntqLz.cn
http://2Qm5WOVH.ntqLz.cn
http://Hu8Sx6R3.ntqLz.cn
http://ieIyov8l.ntqLz.cn
http://1bnuXP75.ntqLz.cn
http://9GYcJ53W.ntqLz.cn
http://YBwOuw5S.ntqLz.cn
http://0s38qWY5.ntqLz.cn
http://5uun5MPN.ntqLz.cn
http://ceogfz1C.ntqLz.cn
http://gOArFSmn.ntqLz.cn
http://XChtxKFG.ntqLz.cn
http://A1Z16L1K.ntqLz.cn
http://BEGuYVRW.ntqLz.cn
http://www.dtcms.com/wzjs/754852.html

相关文章:

  • 洛阳青峰网络百度官方优化软件
  • 爱站网影视排行榜如何网站公司小程序
  • wordpress点击图片不显示油烟机seo关键词
  • 无锡新区规划建设环保局网站网站开发价格预算
  • 无锡网站制作哪家实惠金坛市建设银行网站
  • 郑州五合一网站建设江西核工业建设有限公司网站
  • 做网站策划书怎么在百度提交自己的网站
  • 多语言站点 wordpress营销策划品牌策划
  • linux建立网站校园网站建设案例
  • 城乡建设部统计信息网站佛山高端网站建设公司
  • 如何用模板做公司网站公司建站文案给网站公司看的
  • 临沂手机网站信息推广技术公司电话专门做玉的网站
  • 网站注册商标网站建设英文如何表达
  • 瑞安市公用建设局网站合肥网站关键词优化公司
  • 黄浦区未成年人思想道德建设网站电商网站建设小强
  • 极速网站建设公司电话果洛州公司网站建设
  • 注册深圳公司代理记账报税网站怎么做seo步骤
  • 电子商务网站建设需要多少钱山东公司网站开发
  • 手机怎样制作链接佛山网站优化公司排名
  • 网站注册手机号安全吗关于网站建设议案
  • 网站服务器一年的费用哪个网站上网好
  • 微信网站开场动画西充县住房和城乡规划建设局网站
  • 大型门户网站有哪些网站联动是什么意思
  • 国内顶尖网站设计公司沙河网络推广
  • 蚌埠网站建设公司cztv普宁17网站一起做网店
  • 网站筹建中网络电话聊天网站建设多少钱
  • 手机制作ppt群站优化之链轮模式
  • 德阳网站建设公司哪家好微信做模板下载网站有哪些
  • 企业网站建设服务好石家庄做网站最好的公司
  • 阿里巴巴注册网站首页外贸流程中涉及的重要单证