forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerup_checks.py
139 lines (118 loc) · 5.41 KB
/
powerup_checks.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from __future__ import print_function
import os # Load the Library Module
import sqlite3 # Load the Library Module
import subprocess # Load the Library Module
import sys # Load the Library Module
from time import strftime # Load just the strftime Module from Time
# Script Name : powerup_checks.py
# Author : Craig Richards
# Created : 25th June 2013
# Last Modified :
# Version : 1.0
# Modifications :
# Description : Creates an output file by pulling all the servers for the given site from SQLITE database, then goes through the list pinging the servers to see if they are up on the network
dropbox = os.getenv(
"dropbox"
) # Set the variable, by getting the value of the variable from the OS
config = os.getenv(
"my_config"
) # Set the variable, by getting the value of the variable from the OS
dbfile = "Databases/jarvis.db" # Set the variable to the database
master_db = os.path.join(
dropbox, dbfile
) # Create the variable by linking the path and the file
listfile = "startup_list.txt" # File that will hold the servers
serverfile = os.path.join(
config, listfile
) # Create the variable by linking the path and the file
outputfile = "server_startup_" + strftime("%Y-%m-%d-%H-%M") + ".log"
# Below is the help text
text = """
You need to pass an argument, the options the script expects is
-site1 For the Servers relating to site1
-site2 For the Servers located in site2"""
def windows(): # This is the function to run if it detects the OS is windows.
f = open(outputfile, "a") # Open the logfile
for server in open(serverfile, "r"): # Read the list of servers from the list
# ret = subprocess.call("ping -n 3 %s" % server.strip(), shell=True,stdout=open('NUL', 'w'),stderr=subprocess.STDOUT) # Ping the servers in turn
ret = subprocess.call(
"ping -n 3 %s" % server.strip(),
stdout=open("NUL", "w"),
stderr=subprocess.STDOUT,
) # Ping the servers in turn
if ret == 0: # Depending on the response
f.write(
"%s: is alive" % server.strip().ljust(15) + "\n"
) # Write out to the logfile is the server is up
else:
f.write(
"%s: did not respond" % server.strip().ljust(15) + "\n"
) # Write to the logfile if the server is down
def linux(): # This is the function to run if it detects the OS is nix.
f = open("server_startup_" + strftime("%Y-%m-%d") + ".log", "a") # Open the logfile
for server in open(serverfile, "r"): # Read the list of servers from the list
ret = subprocess.call(
"ping -c 3 %s" % server,
shell=True,
stdout=open("/dev/null", "w"),
stderr=subprocess.STDOUT,
) # Ping the servers in turn
if ret == 0: # Depending on the response
f.write(
"%s: is alive" % server.strip().ljust(15) + "\n"
) # Write out to the logfile is the server is up
else:
f.write(
"%s: did not respond" % server.strip().ljust(15) + "\n"
) # Write to the logfile if the server is down
def get_servers(query): # Function to get the servers from the database
conn = sqlite3.connect(master_db) # Connect to the database
cursor = conn.cursor() # Create the cursor
cursor.execute(
"select hostname from tp_servers where location =?", (query,)
) # SQL Statement
print("\nDisplaying Servers for : " + query + "\n")
while True: # While there are results
row = cursor.fetchone() # Return the results
if row == None:
break
f = open(serverfile, "a") # Open the serverfile
f.write("%s\n" % str(row[0])) # Write the server out to the file
print(row[0]) # Display the server to the screen
f.close() # Close the file
def main(): # Main Function
if os.path.exists(serverfile): # Checks to see if there is an existing server file
os.remove(serverfile) # If so remove it
if len(sys.argv) < 2: # Check there is an argument being passed
print(text) # Display the help text if there isn't one passed
sys.exit() # Exit the script
if (
"-h" in sys.argv
or "--h" in sys.argv
or "-help" in sys.argv
or "--help" in sys.argv
): # If the ask for help
print(text) # Display the help text if there isn't one passed
sys.exit(0) # Exit the script after displaying help
else:
if sys.argv[1].lower().startswith("-site1"): # If the argument is site1
query = "site1" # Set the variable to have the value site
elif (
sys.argv[1].lower().startswith("-site2")
): # Else if the variable is bromley
query = "site2" # Set the variable to have the value bromley
else:
print(
"\n[-] Unknown option [-] " + text
) # If an unknown option is passed, let the user know
sys.exit(0)
get_servers(query) # Call the get servers funtion, with the value from the argument
if os.name == "posix": # If the OS is linux.
linux() # Call the linux function
elif os.name in ("nt", "dos", "ce"): # If the OS is Windows...
windows() # Call the windows function
print(
"\n[+] Check the log file " + outputfile + " [+]\n"
) # Display the name of the log
if __name__ == "__main__":
main() # Call the main function