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]
#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


Messages In This Thread
RE: Loop through list of ip-addresses - by AlphaInc - May-11-2022, 01:52 PM
RE: Loop through list of ip-addresses - by menator01 - May-11-2022, 01:56 PM
RE: Loop through list of ip-addresses - by AlphaInc - May-11-2022, 02:06 PM
RE: Loop through list of ip-addresses - by AlphaInc - May-11-2022, 02:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Loop through directories and files one level down? Winfried 3 505 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  [solved] list content check paul18fr 6 965 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,759 May-14-2023, 04:29 PM
Last Post: Winfried
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,484 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,916 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 1,065 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 1,067 Sep-05-2022, 09:36 AM
Last Post: snippsat
  a function to get IP addresses of interfaces Skaperen 2 1,578 May-30-2022, 05:00 PM
Last Post: Skaperen
  [solved] Basic question on list matchiing paul18fr 7 2,094 May-02-2022, 01:03 PM
Last Post: DeaD_EyE
  [solved] Sort list paul18fr 5 3,064 Aug-18-2021, 06:34 AM
Last Post: naughtyCat

Forum Jump:

User Panel Messages

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