首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Cisco CLI上运行Python脚本时面临的问题

在Cisco CLI上运行Python脚本时面临的问题
EN

Stack Overflow用户
提问于 2015-08-18 16:43:52
回答 1查看 632关注 0票数 1

我正在尝试编写一个Python脚本,以便在思科交换机的一个接口上启用端口安全,并将它们绑定到一组MAC_addresses,其中我将以下内容作为来自用户的输入:

  1. 要添加的MAC地址数。
  2. 要添加的MAC地址。

我的代码如下:

代码语言:javascript
复制
tn = telnetlib.Telnet('192.168.1.10')
tn.read_until(b"Password:")
telpassword="P@ssw0rd"
tn.write(telpassword.encode('ascii')+ b'\r\n')
tn.write(b"enable"+b"\r\n")
tn.write(telpassword.encode('ascii')+ b'\r\n')
tn.write(b"config terminal"+ b"\r\n")
tn.write(b"interface gigabitEthernet 1/0/10"+ b"\r\n")
tn.write(b"switchport mode access"+ b"\r\n")
tn.write(b"switchport port-security"+ b"\r\n")
maxmac = input("How many MAC Addresses you want to add"+"\n")
tn.write(b"switchport port-security maximum"+ maxmac.encode('ascii') + b"\r\n")
macadd = input("Enter the MAC Address in the format xxxx.xxxx.xxxx"+"\n")
tn.write(b"switchport port-security mac-address"+ macadd.encode('ascii') + b"vlan access" + b"\r\n")
tn.write(b"switchport port-security violation shutdown" + b"\r\n")
tn.write(b"end" + b"\r\n")
tn.write(b"wr" + b"\r\n")
print("MAC_Address "+str(macadd)+" has been added")

当我传递命令时,我必须连接字节和字符串(在转换成字节之后,我的Cisco CLI没有对那些commands.Like进行重新编码,例如,我的Cisco CLI在传递脚本时不接受以下两个命令:

代码语言:javascript
复制
tn.write(b"switchport port-security maximum"+ maxmac.encode('ascii') + b"\r\n")
tn.write(b"switchport port-security mac-address"+ strmacadd.encode('ascii') + b"vlan access" + b"\r\n")

但是,在传递以下命令时,可以很好地使用身份验证密码:

代码语言:javascript
复制
tn.write(telpassword.encode('ascii')+ b'\r\n')

当我的字符串命令(转换为字节)与字符串用户输入(也被转换为字节)连接时,我面临一些问题,如上面所示。

请指导我,在这种情况下,什么应该是正确的方式传递命令通过思科CLI。

EN

回答 1

Stack Overflow用户

发布于 2015-08-18 17:07:02

也许你想看看这个图书馆:https://github.com/nickpegg/ciscolib

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32078078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档