![]() |
Loop through list of ip-addresses [SOLVED] - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Loop through list of ip-addresses [SOLVED] (/thread-37196.html) |
Loop through list of ip-addresses [SOLVED] - AlphaInc - May-11-2022 Hello everybody, I want to use a python script to check if certain ips are still online and if not, i want the system to shut down. Therefore I have created a list of client ip's and a loop to check every ip if its online or not and then, if all ip's are down, execute a command (in this example it is the command #Command1) and if not, it should execute another command (in this exampe #Command2). def ip_check(): hosts = [ "192.168.180.100", "192.168.180.101", "192.168.180.102", "192.168.180.103", "192.168.180.104", "192.168.180.105", "192.168.180.106" ] for x in hosts: host_response = os.system("ping -c 1 " + str(hosts)) if guest_response == 0: #Command1 else: #Command2The problem is, all i get is this output and I don't know how to fix it: ping: 192.168.180.106]: Name or service not known ping: 192.168.180.106]: Name or service not known ping: 192.168.180.106]: Name or service not known ping: 192.168.180.106]: Name or service not known ping: 192.168.180.106]: Name or service not known ping: 192.168.180.106]: Name or service not known ping: 192.168.180.106]: Name or service not known RE: Loop through list of ip-addresses - menator01 - May-11-2022 You need to move the if/else statement into the for loop to check each ip RE: Loop through list of ip-addresses - AlphaInc - May-11-2022 (May-11-2022, 01:40 PM)menator01 Wrote: You need to move the if/else statement into the for loop to check each ip Like this?: for x in hosts: host_response = os.system("ping -c 1 " + str(hosts)) if host_response == 0: print ("Command1") else: print ("Command2") I still get this message: ping: 192.168.180.106]: Name or service not known Command2 ping: 192.168.180.106]: Name or service not known Command2 ping: 192.168.180.106]: Name or service not known Command2 ping: 192.168.180.106]: Name or service not known Command2 ping: 192.168.180.106]: Name or service not known Command2 ping: 192.168.180.106]: Name or service not known Command2 ping: 192.168.180.106]: Name or service not known Command2 RE: Loop through list of ip-addresses - menator01 - May-11-2022 if/else inside the for loop import os def ip_check(): hosts = [f'192.168.180.{index}' for index in range(100, 107)] for x in hosts: host_response = os.system(f"ping -n 1 {x}") if host_response == 0: print('command 1') else: print('command 2') ip_check() RE: Loop through list of ip-addresses - AlphaInc - May-11-2022 (May-11-2022, 01:56 PM)menator01 Wrote: if/else inside the for loop I copied your code 1:1 but I still get the message that the name or service (106) is not known... RE: Loop through list of ip-addresses - menator01 - May-11-2022 I updated the code above. coarse I changed -c to -n RE: Loop through list of ip-addresses - AlphaInc - May-11-2022 (May-11-2022, 02:09 PM)menator01 Wrote: I updated the code above. coarse I changed -c to -n Okay nice, that works. And what if the ips don't come in a row (for example .10, .55 and .104) ? RE: Loop through list of ip-addresses - menator01 - May-11-2022 (May-11-2022, 02:20 PM)AlphaInc Wrote:(May-11-2022, 02:09 PM)menator01 Wrote: I updated the code above. coarse I changed -c to -n You can make your original list. The list I made was a quick way for me to do it. (If that is what you are referring to) |