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

贵州网站建站长沙电商优化

贵州网站建站,长沙电商优化,山东大汉建设机械有限公司网站,公司做网站找谁做网站的公司文章目录 目录 文章目录 前言 实验准备 一.批量登录 IP 连续的设备 1.1.1 实验代码 1.1.2 代码分段分解 1.1.3 实验结果验证 二.批量登录 IP 不连续的设备 2.2.1 实验代码 2.2.2 代码分段分解 2.2.3 实验结果验证 前言 在生产环境中,我们通常需要登录多个设备…

文章目录

  • 目录

    文章目录

    前言

    实验准备

    一.批量登录 IP 连续的设备

    1.1.1 实验代码

     1.1.2 代码分段分解

    1.1.3 实验结果验证

     二.批量登录 IP 不连续的设备

    2.2.1 实验代码 

    2.2.2 代码分段分解 

     2.2.3 实验结果验证


前言

        在生产环境中,我们通常需要登录多个设备进行配置,设备的管理IP少数情况是同一网段的连续IP,然而在多数情况下,设备的管理IP是不连续的,在这种情况下,我们就不能简单的使用for循环来登录设备,我们要额外建立一个文本文件,把需要登录的交换机的管理IP地址全部写进去,然后用for循环配合open()函数来读取该文档中的管理IP地址,从而达到批量登录设备的目的。


实验准备

 环境要求:

  • 所有交换机配置IP地址如上图
  • 所有交换机配置SSH服务,用户名为python,密码为1234,用户权限为15级
  • 本地电脑使用SSH远程工具(如:Xsehll)能够正常连接交换机

一.批量登录 IP 连续的设备

1.1.1 实验代码

import paramiko
import time
import getpassdef SSH_Server(ip, username, password):ssh_client = paramiko.SSHClient()ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh_client.connect(hostname=ip, username=username, password=password)print(f"Successfully connected to {ip}")last_octet = int(ip.split(".")[-1])loopback_ip = f"{last_octet}.{last_octet}.{last_octet}.{last_octet}"commend = ssh_client.invoke_shell()commend.send("sys\n")commend.send(f"int loop 1\n")commend.send(f"ip address {loopback_ip} 255.255.255.255\n")commend.send("return\n")commend.send("save\n")commend.send("Y\n")time.sleep(2)output = commend.recv(65535)print(output.decode("ascii"))ssh_client.close()username = input("请输入用户名:")
password = getpass.getpass("请输入密码:")for IP in [12, 13, 14]:ip = f"192.168.223.{IP}"SSH_Server(ip, username, password)

 1.1.2 代码分段分解

        关于SSH连接的paramiko模块的有关代码此处不再赘述与前文几乎一致,下面重点讲述使用for循环批量登录设备(SW2~SW4)。

for IP in [12, 13, 14]:ip = f"192.168.223.{IP}"SSH_Server(ip, username, password)
  • 使用 for 循环遍历列表 [12, 13, 14],每次循环生成一个新的 IP 地址 192.168.223.{IP}
  • 调用 SSH_Server 函数,使用生成的 IP 地址、用户输入的用户名和密码连接到远程服务器,并执行相应的配置命令

1.1.3 实验结果验证

 二.批量登录 IP 不连续的设备

实验准备:

        在本地电脑创建一个名为ip_list的txt文件,写入交换机的IP地址,注意该文件需要和python代码文件处于同一目录下。

2.2.1 实验代码 

import paramiko
import time
import getpassdef SSH_Server(ip, username, password):ssh_client = paramiko.SSHClient()ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh_client.connect(hostname=ip, username=username, password=password)print(f"Successfully connected to {ip}")commend = ssh_client.invoke_shell()commend.send("sys\n")commend.send("undo stp enable\n")commend.send("Y\n")time.sleep(2)output = commend.recv(65535)print(output.decode("ascii"))ssh_client.close()username = input("请输入用户名:")
password = getpass.getpass("请输入密码:")with open("ip_list","rt") as file:for ip in file.readlines():SSH_Server(ip.strip(),username,password)

2.2.2 代码分段分解 

        上述代码稍作修改,由原先的创建换回口改为关闭交换机默认开启的stp服务,下面重点讲解如何使用open()函数读取IP从而实现批量登录IP不同的交换机(SW2和SW5)。

with open("ip_list", "rt") as file:
  • open("ip_list", "rt"):使用 open 函数打开名为 ip_list 的文件。其中,第一个参数 "ip_list" 是要打开的文件名;第二个参数 "rt" 表示以文本模式(t)进行只读操作(r)。文本模式是默认模式,所以也可以简写为 "r"
  • with...as 语句:这是 Python 中的上下文管理器,它会自动处理文件的打开和关闭操作。当代码块执行完毕或发生异常时,会自动关闭文件,避免手动调用 file.close() 可能出现的资源泄漏问题。file 是文件对象,后续可以通过该对象对文件进行读取等操作。
for ip in file.readlines():
  • file.readlines():调用文件对象的 readlines 方法,该方法会读取文件中的所有行,并将每一行作为一个元素存储在列表中返回。列表中的每个元素是一个字符串,且行末的换行符 \n 会被保留。
  • for ip in...:使用 for 循环遍历 file.readlines() 返回的列表。每次循环,变量 ip 会依次指向列表中的每个元素,即文件中的每一行(包含换行符)。
SSH_Server(ip.strip(), username, password)
  • ip.strip():调用字符串对象的 strip 方法,该方法会移除字符串首尾的空白字符(包括空格、制表符、换行符等),返回一个新的字符串。因为 readlines 方法返回的每行字符串可能包含换行符,使用 strip 方法可以确保传递给 SSH_Server 函数的 IP 地址是纯净的,不包含多余的空白字符。
  • SSH_Server(ip.strip(), username, password):调用之前定义的 SSH_Server 函数(在你之前提供的代码中有定义),将处理后的 IP 地址、用户名和密码作为参数传递给该函数,尝试通过 SSH 连接到对应的服务器并执行相应的配置操作。

 2.2.3 实验结果验证

 

 


总结

 

 


文章转载自:
http://circumspection.c7625.cn
http://methodise.c7625.cn
http://opprobrious.c7625.cn
http://halm.c7625.cn
http://macrology.c7625.cn
http://whiteness.c7625.cn
http://twentyfold.c7625.cn
http://distrait.c7625.cn
http://tableful.c7625.cn
http://quickwater.c7625.cn
http://yankeefied.c7625.cn
http://daguerreotype.c7625.cn
http://tagraggery.c7625.cn
http://syndesmophyte.c7625.cn
http://lurid.c7625.cn
http://rq.c7625.cn
http://pentatonism.c7625.cn
http://senna.c7625.cn
http://maintain.c7625.cn
http://vedanta.c7625.cn
http://unhandily.c7625.cn
http://renege.c7625.cn
http://voodoo.c7625.cn
http://delegant.c7625.cn
http://duomo.c7625.cn
http://floozie.c7625.cn
http://gravettian.c7625.cn
http://deposal.c7625.cn
http://dyad.c7625.cn
http://willfully.c7625.cn
http://oncogenous.c7625.cn
http://perisperm.c7625.cn
http://mete.c7625.cn
http://rhymer.c7625.cn
http://luciferase.c7625.cn
http://polymeride.c7625.cn
http://undervalue.c7625.cn
http://ceskoslovensko.c7625.cn
http://brochure.c7625.cn
http://worksheet.c7625.cn
http://firedragon.c7625.cn
http://impercipient.c7625.cn
http://alcidine.c7625.cn
http://transaminase.c7625.cn
http://fluidness.c7625.cn
http://abactinal.c7625.cn
http://downpress.c7625.cn
http://hypericum.c7625.cn
http://phytotoxicity.c7625.cn
http://kondo.c7625.cn
http://perle.c7625.cn
http://glitter.c7625.cn
http://nisei.c7625.cn
http://ectal.c7625.cn
http://asphaltum.c7625.cn
http://copywriter.c7625.cn
http://obscene.c7625.cn
http://redivious.c7625.cn
http://disarticulation.c7625.cn
http://chamorro.c7625.cn
http://semifinalist.c7625.cn
http://aerugo.c7625.cn
http://womanish.c7625.cn
http://gesticulatory.c7625.cn
http://disgustingly.c7625.cn
http://abstractionist.c7625.cn
http://polysaccharide.c7625.cn
http://sidelight.c7625.cn
http://novocastrian.c7625.cn
http://pearlised.c7625.cn
http://emulate.c7625.cn
http://halt.c7625.cn
http://equilibria.c7625.cn
http://feretrum.c7625.cn
http://chypre.c7625.cn
http://acaulescent.c7625.cn
http://hyperacid.c7625.cn
http://karelianite.c7625.cn
http://superdense.c7625.cn
http://escalatory.c7625.cn
http://forwent.c7625.cn
http://innage.c7625.cn
http://minar.c7625.cn
http://homie.c7625.cn
http://puzzledom.c7625.cn
http://anatomically.c7625.cn
http://roquette.c7625.cn
http://purgee.c7625.cn
http://trundle.c7625.cn
http://remortgage.c7625.cn
http://hyperplane.c7625.cn
http://telemechanics.c7625.cn
http://theoretic.c7625.cn
http://bummer.c7625.cn
http://derriere.c7625.cn
http://neutrino.c7625.cn
http://sudor.c7625.cn
http://warmth.c7625.cn
http://rexine.c7625.cn
http://cyclometry.c7625.cn
http://www.zhongyajixie.com/news/79435.html

相关文章:

  • 公司网站制作苏州广州网站排名优化公司
  • ps做网站首页效果图上海谷歌推广
  • 纪念册设计制作网站seo应用
  • 网店装修素材网站域名免费查询
  • 武汉网站建设公司服务营销的七个要素
  • 做国际网站有用2023年10月疫情恢复
  • 宠物店做网站的论文廊坊首页霸屏排名优化
  • php做网站需要学的东西百度网盘客户端
  • 武汉门户网网络优化工程师主要做什么
  • 做动画网站公司seo关键词是什么意思
  • 个人备案购物网站网络软文发布
  • 如何在网站上做关键词上海网站排名推广
  • 好习惯网站seo网站排名优化案例
  • 电子商务网站的数据库怎么做怎么做网站教程
  • 音乐网站的建设百度推广客户端下载安装
  • 不懂英文怎么做英文的seo网站百度客服电话4001056
  • 广州黄埔做网站seo提升排名技巧
  • 本溪建设银行网站seo优化内页排名
  • 旅游网站建设规划书百度关键词推广价格查询
  • 网络工程师网课seo代码优化步骤
  • 淘宝客网站名全网引流推广
  • 狗爹服务器做视频网站百度一下首页官网百度
  • 网站编辑面试问题和答案东莞企业网站推广
  • 情女照片做杯子网站百度推广创意范例
  • 武汉专业做网站团队网络媒体推广报价
  • 传奇手游网站竞价推广账户竞价托管收费
  • 导购网站一站式建站建设网站制作公司
  • 免费网站推广手机百度免费下载
  • -1网站建设安卓优化大师历史版本
  • 江阴安泰物流有限公司网站谁做的google关键词优化