python 读取本地文件进行远程配置交换机

拓扑搭建:

环境搭建:

注意这里的云连接的是19.1这个网段,所以交换机SW1也要配置到同一个网段

sw1:

配置IP,划分vlan:

[sw1]vl 100

[sw1]int vlan 100

[sw1-Vlanif100]ip add 192.168.19.10 24

[sw1]int g0/0/1
[sw1-GigabitEthernet0/0/1]po li ac
[sw1-GigabitEthernet0/0/1]po de vl 100

交换机部署ssh:

[sw1]stelnet server enable         # 启动ssh服务的远程登录功能

[sw1]ssh user zhangxiong         # 新建SSH用户

[sw1]ssh user zhangxiong authentication-type password        # 配置ssh用户密码认证方式

[sw1]ssh user zhangxiong service-type stelnet        # 配置ssh用户的服务方式为远程登录

# 开放 vty 端口,能被远程登录

# 开放 编号为 0 1 2 3 4 的 vty端口,同时支持五个远程用户连接

# 认证模式为 aaa---> authentication 认证(谁能登录进来)
#                          ---> authorizathon  授权(谁进来能干什么)
#                          ---> according 审计(谁进来干了什么)

[sw1]user-interface vty 0 4

[sw1]authentication-mode aaa

[sw1]protocol inbound ssh        # 登入协议为ssh

 # 三A认证配置
[sw1]aaa
[sw1-aaa]local-user zhangxiong password cipher huawei@123     # 配置本地账号的登录口令
[sw1-aaa]local-user zhangxiong privilege level 15        # 配置账号登录后的管理级别,15最高
[sw1-aaa]local-user zhangxiong service-type ssh        # 配置本地用户的接入类型

这时基础配置就完成了,主要是在sw1上开启了ssh服务并配置了账号和密码验证,然后配置远程登录,从而可以实现用python进行连接sw1。接下来开始正题:

python代码:

import paramiko
import time

# 1.创建客户端对象
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# 使用客户端,导入参数
ssh.connect(hostname="192.168.19.10", username="zhangxiong", 
                                      password="huawei@123")

command = ssh.invoke_shell()  # 创建一个命令对象 ,调用shell


"""
读取当前目录的SW1.txt文件,然后将文件中每行的命令编码后发送到终端
"""
with open('SW1.txt', 'r', encoding='utf-8') as f:
    while True:
        content = f.readline()  # 将读取到的文件,按行,存储为一个列表
        if not content:
            break
        command.send(content.encode())

print("please wait ....")
time.sleep(5)  # 发送完命令,需要一个延时,等待设备 将回显值回传,等待时间不能太短
output = command.recv(65535)  # 接收服务器的返回数据
print(output.decode())  # 将接收的数据解码

ssh.close()

sw1.txt文件:

system-view
#
sysname SW1
#
vlan batch 10 to 14 21 to 23 100 to 101
#
stp instance 1 root primary
stp instance 2 root secondary
#
stp region-configuration
 region-name school
 instance 1 vlan 11 to 14
 instance 2 vlan 21 to 23
 instance 3 vlan 100
 active region-configuration
#
interface Vlanif10
 ip address 192.168.10.253 255.255.255.0
#
interface Vlanif11
 ip address 192.168.11.253 255.255.255.0
 vrrp vrid 11 virtual-ip 192.168.11.254
 vrrp vrid 11 priority 120
 vrrp vrid 11 preempt-mode timer delay 30
#
interface Vlanif12
 ip address 192.168.12.253 255.255.255.0
 vrrp vrid 12 virtual-ip 192.168.12.254
 vrrp vrid 12 priority 120
 vrrp vrid 12 preempt-mode timer delay 30
#
interface Vlanif13
 ip address 192.168.13.253 255.255.255.0
 vrrp vrid 13 virtual-ip 192.168.13.254
 vrrp vrid 13 priority 120
 vrrp vrid 13 preempt-mode timer delay 30
#
interface Vlanif14
 ip address 192.168.14.253 255.255.255.0
 vrrp vrid 14 virtual-ip 192.168.14.254
 vrrp vrid 14 priority 120
 vrrp vrid 14 preempt-mode timer delay 30
#
interface Vlanif21
 ip address 192.168.21.253 255.255.255.0
 vrrp vrid 21 virtual-ip 192.168.21.254
#
interface Vlanif22
 ip address 192.168.22.253 255.255.255.0
 vrrp vrid 22 virtual-ip 192.168.22.254
#
interface Vlanif23
 ip address 192.168.23.253 255.255.255.0
 vrrp vrid 23 virtual-ip 192.168.23.254
#
interface Vlanif100
 ip address 192.168.19.10 255.255.255.0
#
interface Eth-Trunk1
 port link-type trunk
 port trunk allow-pass vlan 11 to 14 21 to 23
 mode lacp-static
#
interface GigabitEthernet0/0/1
 port link-type trunk
 port trunk allow-pass vlan 10 100 to 101
#
interface GigabitEthernet0/0/2
 eth-trunk 1
#
interface GigabitEthernet0/0/3
 eth-trunk 1
#
interface GigabitEthernet0/0/4
 port link-type trunk
 port trunk allow-pass vlan 11 100
#
interface GigabitEthernet0/0/5
 port link-type trunk
 port trunk allow-pass vlan 12 100
#
interface GigabitEthernet0/0/6
 port link-type trunk
 port trunk allow-pass vlan 13 100
#
interface GigabitEthernet0/0/7
 port link-type trunk
 port trunk allow-pass vlan 14 100
#
interface GigabitEthernet0/0/8
 port link-type trunk
 port trunk allow-pass vlan 21 100
#
interface GigabitEthernet0/0/9
 port link-type trunk
 port trunk allow-pass vlan 22 100
#
interface GigabitEthernet0/0/10
 port link-type trunk
 port trunk allow-pass vlan 23 100
#
interface GigabitEthernet0/0/11
 port link-type trunk
 port trunk allow-pass vlan 100 to 101
#
ospf 1 router-id 192.168.100.10
 area 0.0.0.0
  network 192.168.10.0 0.0.0.255
 area 0.0.0.1
  network 192.168.11.0 0.0.0.255
  network 192.168.12.0 0.0.0.255
  network 192.168.13.0 0.0.0.255
  network 192.168.14.0 0.0.0.255
  network 192.168.21.0 0.0.0.255
  network 192.168.22.0 0.0.0.255
  network 192.168.23.0 0.0.0.255

在sw1上打开终端,执行dis cu 查看配置可以看到sw1.txt配置文档配置过的命令

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 您可以使用 paramiko 模块来实现 Python 远程登录交换机并导出交换机配置至本地。 以下是一个示例代码: ``` import paramiko # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname='交换机的IP地址', port=22, username='用户名', password='密码') # 执行命令 stdin, stdout, stderr = ssh.exec_command('show run') # 获取命令结果 result = stdout.read().decode() # 关闭连接 ssh.close() # 将结果写入本地文件 with open('配置文件的本地路径', 'w') as f: f.write(result) ``` 请注意,您需要在计算机上安装 paramiko 模块,如果没有安装,可以通过执行以下命令安装: ``` pip install paramiko ``` ### 回答2: 使用Python远程登录交换机并导出交换机配置至本地的过程可以通过Paramiko库来实现。Paramiko是一个用于SSH客户端和服务器的Python实现,可以通过该库建立SSH连接并执行远程命令。 首先,需要安装Paramiko库,可以使用pip命令进行安装。 ```python pip install paramiko ``` 接下来,通过以下代码可以实现远程登录交换机、导出配置文件并保存至本地的功能。 ```python import paramiko # 建立SSH连接 ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(hostname='交换机IP地址', port=22, username='用户名', password='密码') # 远程执行命令,导出配置文件 stdin, stdout, stderr = ssh_client.exec_command('show running-config') config = stdout.read().decode() # 将配置文件保存至本地 with open('switch-config.txt', 'w') as file: file.write(config) # 关闭SSH连接 ssh_client.close() ``` 以上代码中,需要替换`交换机IP地址`、`用户名`和`密码`为实际的交换机IP地址、登录用户名和密码。 执行以上代码后,将会在运行代码的目录下生成一个名为`switch-config.txt`的文件,其中保存了交换机配置信息。 需要注意的是,此方法仅适用于支持SSH协议的交换机,且需要正确配置SSH连接的参数。在实际使用过程中,可以根据不同交换机厂商和具体设备型号来调整代码。 ### 回答3: 要使用Python远程登录交换机并导出交换机配置至本地,可以使用Paramiko库实现。Paramiko是一个用于SSHv2协议的Python实现,可以用来建立SSH连接并执行命令。 首先,需要安装Paramiko库。可以使用pip命令进行安装: ``` pip install paramiko ``` 然后,可以编写Python代码实现远程登录交换机并导出配置。以下是一个示例代码: ```python import paramiko # 定义交换机的IP地址、用户名和密码 ip = '交换机IP地址' username = '用户名' password = '密码' # 创建SSH客户端对象 client = paramiko.SSHClient() # 自动添加主机密钥 client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: # 连接交换机 client.connect(ip, username=username, password=password) # 打开一个SSH会话通道 ssh_session = client.invoke_shell() # 发送命令到交换机 ssh_session.send('show running-config\n') # 等待命令执行完成 while not ssh_session.recv_ready(): pass # 接收输出并保存至文件 output = ssh_session.recv(65535).decode() with open('交换机配置.txt', 'w') as file: file.write(output) # 关闭SSH会话通道 ssh_session.close() finally: # 关闭SSH连接 client.close() ``` 将代码中的`交换机IP地址`、`用户名`和`密码`替换为实际的交换机信息。运行代码后,会通过SSH连接到交换机,执行`show running-config`命令,并将输出保存至本地的`交换机配置.txt`文件中。 需要注意的是,确保Python环境中已安装Paramiko库。同时,交换机需要开启SSH服务并允许使用指定的用户名和密码进行远程登录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值