There is two type of loop in Python, For loops and While Loops.
For Loops
ips = [“10.10.10.1”, “10.10.10.2”, “10.10.10.3”, “10.10.10.4”]
for each_ip in ips:
….print(each_ip)
10.10.10.1
10.10.10.2
10.10.10.3
10.10.10.4
Another example :
devices = [“device1”, “device2”]
for each_host in devices:
….device = {‘host’: each_host, ‘username’: ‘xxxx’, ‘password’: ‘xxxx’, ‘device_type’: ‘cisco_ios’, ‘session_log’: ‘logging.txt’, ‘fast_cli’: True}
….print(ssh_conn.find_prompt())
….
….print(“Configuration of vlan 100-105”)
….sent_vlan = ssh_conn.send_config_from_file(“vlans.txt”)
….print(“Commit”)
….write_conf = ssh_conn.save_config()
….print(write_conf)
List Indexes trackingÂ
ips = [“10.10.10.1”, “10.10.10.2”, “10.10.10.3”, “10.10.10.4”]
for each_ip in enumerate(ips):
….print(each_ip)
(0, ‘10.10.10.1’)
(1, ‘10.10.10.2’)
(2, ‘10.10.10.3’)
(3, ‘10.10.10.4’)
******************************************************************************************
for index, each_ip in enumerate(ips):
….print(index)
….print(each_ip)
….print(“***************”)
******************************************************************************************
Break and Continue
for index, each_ip in enumerate(ips):
….print(index)
….print(each_ip)
….if each_ip == “10.10.10.3”
……..break
….print(“***************”)
10.10.10.1
10.10.10.2
10.10.10.3
As for the continue it jump right back at the beginning of the loopÂ
for index, each_ip in ips:
….if each_ip == “10.10.10.3”
……..continue
….print(“***************”)
….print(each_ip)
10.10.10.1
***************
10.10.10.2
“***************
10.10.10.4