forked from labeveryday/ping_script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool3.py
More file actions
41 lines (32 loc) · 1.14 KB
/
tool3.py
File metadata and controls
41 lines (32 loc) · 1.14 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
41
#!/usr/bin/env python
# import built-in os library
# https://docs.python.org/3/library/os.html
import os
def main():
"""
This code is reusable.
Executes each funtion in the script
"""
results_file = open("results.txt", "w")
print(len(create_ip_list("192.168.1.", 3)))
ping_list(results_file, "192.168.1.", 3)
results_file.close()
def create_ip_list(subnet, num):
"""Appends the concatenated ip to the ip_list"""
ip_list = []
for ip in range(1, num):
ip_list.append(subnet + str(ip))
return ip_list
def ping_list(results_file, subnet, num):
"""Outputs ping ip_list reults up or down"""
ip_addresses = create_ip_list(subnet, num)
for ip in ip_addresses:
response = os.popen(f"ping {ip} -n 1").read()
if "Received = 1" and "Approximate" in response:
print(f"UP {ip} Ping Successful")
results_file.write(f"UP {ip} Ping Successful" + "\n")
else:
print(f"Down {ip} Ping Unsuccessful")
results_file.write(f"Down {ip} Ping Unsuccessful" + "\n")
if __name__ == "__main__":
main()