啥都不会的中年小白 2021-05-21 16:51 采纳率: 71.4%
浏览 60
已结题

python调用Telnetlib配置完最后一个交换机总是报错

#!/usr/bin/env python
# coding: utf-8

# In[1]:


#/usr/bin/env python3
from telnetlib import Telnet
import re,time,datetime,os
from threading import Thread
import xlrd

# In[2]:

# 华为设备执行命令函数
def execute_hw(address):
    account="csiaadmin"
    password="qweR%9527"
    tn = Telnet(address)
    tn.read_until(b"Username:", timeout=None)
    tn.write(account.encode()+b'\n')
    tn.read_until(b"Password:", timeout=None)
    tn.write(password.encode()+b'\n')
    tn.write(b"sys"+b'\n')
    tn.write(b"dhcp enable " + b'\n')
    tn.write(b"dhcp snooping enable " + b'\n')
    tn.write(b"port-group 1" + b'\n')
    tn.write(b"group-member GigabitEthernet0/0/1 to GigabitEthernet0/0/48" + b'\n')
    tn.write(b"dhcp snooping enable " + b'\n')
    time.sleep(2)
    tn.write(b"quit" + b'\n')
    tn.write(b"quit" + b'\n')
    tn.write(b"save" + b'\n')
    tn.write(b"y" + b'\n')
    time.sleep(2)
    tn.write(b"quit" + b'\n')
    tn.close()
    print(address)

if __name__ == '__main__':
    ff = open('jiaohuanji.txt', 'r')
    for line in ff.readlines():
        ipp = line.strip()
        execute_hw(ipp)
    ff.close()

写了一个特别简单的python代码,通过来Telnetlib模块来批量配置华为交换机,但是每次配置完最后一个交换机,就会弹出以下错误

Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/xl/caiji/beifen/peizhi.py", line 56, in <module>
    execute_hw(ipp)
  File "C:/Users/Administrator/Desktop/xl/caiji/beifen/peizhi.py", line 30, in execute_hw
    tn = Telnet(address)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\telnetlib.py", line 218, in __init__
    self.open(host, port, timeout)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\telnetlib.py", line 235, in open
    self.sock = socket.create_connection((host, port), timeout)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\socket.py", line 808, in create_connection
    raise err
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\socket.py", line 796, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

  • 写回答

3条回答 默认 最新

  • CSDN专家-黄老师 2021-05-21 18:24
    关注

    ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。这个报错是你的socket断开了,你试试将  tn.close()去掉

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 10月7日
  • 已采纳回答 9月29日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。