I am new to Network automation with python and am writing a code to telnet to a switch and check for port security.The problem is with the loop as it skips the first interface as shown in the code and result.
import telnetlib
pool = ["10.1.1.1","10.1.1.2"]
user = "admin"`
password = "admin"`
for IP in pool:
print(f"Checking Switch --> {IP}")
tn = telnetlib.Telnet(IP)
tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
for port in range(1,4):
print(f"checking port :{port}")
tn.write(f"show port-security interface fastethernet0/{port}\n".encode('ascii'))
output = tn.read_until(b"#").decode('ascii')
print(output)
RESULT:
Checking Switch --> 10.1.1.1
checking port :1
Post-4th-lab-2#
checking port :2
show port-security interface fastethernet0/1
Port Security : Enabled
Port Status : Secure-down
Violation Mode : Shutdown
Aging Time : 0 mins
Aging Type : Absolute
SecureStatic Address Aging : Disabled
Maximum MAC Addresses : 1
Total MAC Addresses : 1
Configured MAC Addresses : 0
Sticky MAC Addresses : 1
Last Source Address:Vlan : 0000.0000.0000:0
Security Violation Count : 0
Post-4th-lab-2#
checking port :3
show port-security interface fastethernet0/2
Port Security : Enabled
Port Status : Secure-down
Violation Mode : Shutdown
Aging Time : 0 mins
Aging Type : Absolute
SecureStatic Address Aging : Disabled
Maximum MAC Addresses : 1
Total MAC Addresses : 1
Configured MAC Addresses : 0
Sticky MAC Addresses : 1
Last Source Address:Vlan : 0000.0000.0000:0
Security Violation Count : 0