-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping-hosts.py
More file actions
42 lines (36 loc) · 1.42 KB
/
ping-hosts.py
File metadata and controls
42 lines (36 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
###############################################################################
# Task: Ping hosts listed in a text file and report up/down status. If it is
# up it will try and reverse DNS to get the hostname.
#
# Usage: 'python3 ping-hosts.py' The ping-hosts.txt file should be in the same
# directory as this script.
#
#
# Author: Collin Clark
# Date: 03MAY2018
# Version: 1.0
################################################################################
import os
import platform
import socket
# Open the ping-hosts.txt file and read each line as a string
with open("ping-hosts.txt") as fp:
hostname = fp.readline()
for hostname in fp:
# Lets remove /n from the string
hostname = hostname.rstrip()
# Ping based on Windows or Unix
if platform.system() == "Windows":
response = os.system("ping "+hostname+" -n 1")
else:
response = os.system("ping -c 1 " + hostname)
print(hostname)
# A boolean on whether or not the host responded to the ping
if response == 0:
# Let's grab the DNS info for the IP above
dns_raw = socket.gethostbyaddr(hostname)
# gethostbyaddr returns a tuple of information. All we need is the first one.
slice = dns_raw[0]
print("The host "+hostname+" is UP and its hostname is "+slice)
else:
print(hostname+" is down")