Reputation: 565
I'm testing running simple script to ping few servers by calling a text file iplist.txt. The script working if txt file only contain IP address. Now I'm adding hostname next to the IP address in the iplist.txt and ping failed.
Original only IP address in iplist.txt
192.168.1.1
192.168.1.2
Updated with name string in iplist.txt
192.168.1.1 server1
192.168.1.2 server2
I think the issue is the strip part,
def iplist() :
# Grab list of IP from the file
pingList = open("iplist.txt", "r")
for i in pingList:
ip = i[-15:]
ip = ip.strip(' ')
ip = ip.strip('\n')
with open(os.devnull, 'w') as DEVNULL:
try:
subprocess.check_call(
['ping', '-w', '1', ip],
stdout = DEVNULL,
stderr = DEVNULL
)
Upvotes: 1
Views: 358
Reputation: 11080
The way you're getting your IP addresses from the file makes no sense. All you need to do is split each line by spaces and then get the first token (with ip = i.split(' ')[0]
). Like this:
def iplist() :
# Grab list of IP from the file
pingList = open("iplist.txt", "r")
for i in pingList:
ip = i.split(' ')[0]
with open(os.devnull, 'w') as DEVNULL:
try:
subprocess.check_call(
['ping', '-w', '1', ip],
stdout = DEVNULL,
stderr = DEVNULL
)
Upvotes: 2