我正在尝试编写一个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不会识别这些命令。例如,我的Cisco CLI在通过脚本时没有采取以下2个命令:< / p>
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')
当我的字符串命令(转换为字节)与字符串用户输入(也转换为字节)连接时,我遇到了一些问题,如上所示。
请指导我,在这种情况下,通过Cisco CLI传递命令的正确方法应该是什么。