我正在尝试编写一个Python脚本,以便在思科交换机的一个接口上启用端口安全,并将它们绑定到一组MAC_addresses,其中我将以下内容作为来自用户的输入:
我的代码如下:
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在传递脚本时不接受以下两个命令:
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")但是,在传递以下命令时,可以很好地使用身份验证密码:
tn.write(telpassword.encode('ascii')+ b'\r\n')当我的字符串命令(转换为字节)与字符串用户输入(也被转换为字节)连接时,我面临一些问题,如上面所示。
请指导我,在这种情况下,什么应该是正确的方式传递命令通过思科CLI。
发布于 2015-08-18 17:07:02
也许你想看看这个图书馆:https://github.com/nickpegg/ciscolib
https://stackoverflow.com/questions/32078078
复制相似问题