Home > Software engineering >  How to ping several IP addresses without using the same line of code for every IP address?
How to ping several IP addresses without using the same line of code for every IP address?

Time:10-24

I need to ping several IPs a not all at once but one by one. My problem is that I do not want to use the same if else clause for every IP address. As you can see I have only done it for 10.10.10.5 but i do not want to repeat it for every other IP i know it is bad practice but i have no idea how to write a algorithm or something similar for it. I also need to send this info (PASS/FAIL) to a MariaDB database. Could someone please show me how i can write an algorithm or a function to iterate through the IPs but still save the result of each IP and send it to the database. Examples would be much appreciated

   def pingPreflash(self):
        self.reachable = 'PASS'
        self.unreachable = 'FAIL'
        self.preflash = subprocess.run(["ping","-n","2", "10.10.10.2"],stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        self.p1 = subprocess.run(["ping","-n","2", "192.168.11.10"],stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        self.p2 = subprocess.run(["ping","-n","2", "192.168.12.10"],stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        self.p3 = subprocess.run(["ping","-n","2", "192.168.13.10"],stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        self.p4 = subprocess.run(["ping","-n","2", "192.168.14.10"],stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        self.software = subprocess.run(["ping","-n","2", "192.168.100.2"],stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        if self.preflash.returncode == 0:
            if("unreachable" in str(self.preflash.stdout)):
                self.preflashesult = self.unreachable
                print('10.10.10.5', self.preflashesult)
            else:
                self.preflashesult = self.reachable
                print('10.10.10.5', self.preflashesult)
        elif self.preflash.returncode == 1:
            self.preflashesult = self.unreachable
            print('10.10.10.5', self.preflashesult)

CodePudding user response:

I think you've answered your own question by saying you need 'to iterate'. You should put the ip addresses in a list and iterate over them:

ip_addressses = [.....]
for ip in ip_addresses:
    supprocess.run(...)

CodePudding user response:

You can iterate over a list of ip addresses, and then run your lohic using a match statement against the output of your subprocess call.

From there you can write to the DB if required.

def pingPreflash(self):

        ip_list = ['10.10.10.2', '192.168.11.10', '192.168.12.10', '192.168.13.10', '192.168.14.10', '192.168.100.2']

        for ip in ip_list:
            output = subprocess.run(["ping","-n","2", ip],stderr=subprocess.PIPE, stdout=subprocess.PIPE)

            match output.returncode:
                case 0:
                    if 'unreachable' in str(output.stdout):
                        print(ip, 'unReachable')
                    else:
                        print(ip, 'Reachable')
                case 1:
                    print(ip, 'unReachable')

Python <3.10:

def pingPreflash(self):

        ip_list = ['10.10.10.2', '192.168.11.10', '192.168.12.10', '192.168.13.10', '192.168.14.10', '192.168.100.2']

        for ip in ip_list:
            output = subprocess.run(["ping","-n","2", ip],stderr=subprocess.PIPE, stdout=subprocess.PIPE)

            if output.returncode == 0:
                if 'unreachable' in str(output.stdout):
                    print(ip, 'unReachable')
                else:
                    print(ip, 'Reachable')
            elif output.returncode == 1:
                print(ip, 'unReachable')

If you want to store the output, you can assign the result to a dict and use the ip as the key.

def pingPreflash(self):

        ip_list = ['10.10.10.2']

        all_status = {}
        for ip in ip_list:
            output = subprocess.run(["ping","-n","2", ip],stderr=subprocess.PIPE, stdout=subprocess.PIPE)

            if output.returncode == 0:
                if 'unreachable' in str(output.stdout):
                    all_status[ip] = 'FAIL'
                else:
                    all_status[ip] = 'PASS'
            elif output.returncode == 1:
                all_status[ip] = 'FAIL'

        return all_status

Outputs

pingPreflash(1)
{'10.10.10.2': 'FAIL'}
  • Related