Cry2Senpai
Cry2Senpai

Reputation: 71

IP address must be specified?

Heres my code running in python 2.7 on windows 10

import os

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

The test.txt file contains some random IP's, the code runs but when it does it give out the message the the IP address must be specified. My problem is I don't know how to do that within the script. When I use the regular command promt and do ping -c 1 google.com it runs through but reading it from the text file with the above python script that same google.com needs to be specified.

Q1: What does it mean to have the ip specified and how would I do it?

Q2: Should I write my code in a diffrent manner importing a different module?

Upvotes: 1

Views: 1793

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22433

import os

with open('test.txt') as f:

 for line in f:

  response = os.system("ping -c 1 " + line.strip())

  if response == 0:

   print(line, "is up!")

  else:

   print(line, "is down!")

Strip the newline off the end of the records from the file and add a space in the ping command.

Upvotes: 2

Related Questions