I am new in python and I need your help to improve part of my script to be more sophisticate.
==================================== a.py
devices = ['xxxx', 'xxxx', 'xxxx']
============================= MAIN CODE
from a import devices
for ip in devices:
ssh connection
value = +1;
if value == 1:
open a specific file
run the commands from this file.
elif value == 2:
open a specific file
run the commands from this file.
elif value == 3:
open a specific file
run the commands from this file.
=============================================
Issue: For each IP address that it stored in a.py file, I need to run specific pack of commands. With the IF statement it is working fine, but if I add more IP address on a.py file need to append my code.
Thank you all.
I hope I'm interpreting your question correctly. Do correct me if I'm not.
You currently have a list with 3 ip addresses, and you handle them with three if-statements. The problem is, that if you add more entries to your list, you will manually have to add more if-statements to your code?
If this assumption of your question is correct, then indeed. There is a better way of doing this:
devices = ['x.x.x.x', 'x.x.x.x', 'x.x.x.x']
for dev in devices:
do_something(dev)
This will loop over all devices in your list, and execute the same code on each device.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.