Continuing the loop

69 views Asked by At

Ok so I have code that is supposed to run through a txt file and ping the Ip's if the ping is equal to 0 its does an 'nslookup' on it and then it's supposed to continue but after it does the first one in the terminal it's left on a > as if waiting for input. In other instances, my code runs through the txt file fine but once I added in the 'nslookup' it stops after the first one and waits for input.

Is there a way to make it continue to cycle through the txt file till it gets to the end?

Heres the code I'm using I know there are other ways to do a look up on an Ip address but I'm trying to use 'nslookup' in this case unless its impossible.

import os
with open('test.txt','r') as f:
  for line in f:
         response = os.system("ping -c 1 " + line)
         if response == 0:
                 print os.system('nslookup')
         else:
                 print(line, "is down!")
1

There are 1 answers

2
Jean-François Fabre On BEST ANSWER

that's simply because you forgot to pass the argument to nslookup

When you don't pass any argument, the program starts in interactive mode with its own shell.

L:\so>nslookup
Default server :   mydomain.server.com
Address:  128.1.34.82

> 

But using os.system won't make you able to get the output of the command. For that you would need

output = subprocess.check_output(['nslookup',line.strip()])
print(output) # or do something else with it

instead of your os.system command