Python Forum
Loop through list of ip-addresses [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop through list of ip-addresses [SOLVED]
#1
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:
        #Command2
The 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
Reply
#2
You need to move the if/else statement into the for loop to check each ip
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(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
Reply
#4
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()
AlphaInc likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
(May-11-2022, 01:56 PM)menator01 Wrote: if/else inside the for loop
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 host_response == 0:
            print('command 1')
        else:
            print('command 2')
    

I copied your code 1:1 but I still get the message that the name or service (106) is not known...
Reply
#6
I updated the code above. coarse I changed -c to -n
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#7
(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) ?
Reply
#8
(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

Okay nice, that works.
And what if the ips don't come in a row (for example .10, .55 and .104) ?

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)
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] list content check paul18fr 6 681 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,438 May-14-2023, 04:29 PM
Last Post: Winfried
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,093 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,365 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 924 Nov-16-2022, 07:58 PM
Last Post: Winfried
  [SOLVED] [BS] Why new tag only added at the end when defined outside the loop? Winfried 1 965 Sep-05-2022, 09:36 AM
Last Post: snippsat
  a function to get IP addresses of interfaces Skaperen 2 1,419 May-30-2022, 05:00 PM
Last Post: Skaperen
  [solved] Basic question on list matchiing paul18fr 7 1,854 May-02-2022, 01:03 PM
Last Post: DeaD_EyE
  [solved] Sort list paul18fr 5 2,857 Aug-18-2021, 06:34 AM
Last Post: naughtyCat
  instance methods sharing addresses mim 1 2,235 Mar-28-2021, 05:22 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020