-
Notifications
You must be signed in to change notification settings - Fork 0
/
slacker_test.py
executable file
·106 lines (85 loc) · 3.72 KB
/
slacker_test.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
#!/usr/bin/python
import os
import getpass
import paramiko
import re
from scp import SCPClient
paramiko.util.log_to_file("auto_log")
def ping_check(hostname, username, password):
"""Function that does a Ping Check on the hosts"""
"""Usually the http_config only runs when the ping check is successful, but in this test, we run even when unsuccessful"""
response = os.system("ping -c 1 " + hostname)
if response == 0:
print hostname + " is up"
http_config(hostname, username, password)
else:
print hostname + " is down"
http_config(hostname, username, password)
exit
def createSSHClient(server, port, user, password):
"""This function is used to establish ssh connection"""
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(server, port, user, password)
return client
def http_config(hostname, username, password):
connect = createSSHClient(hostname, 22, username, password)
""" Installing Apache on the servers"""
print "\n Installing Apache on the Servers "
stdin, stdout, stderr = connect.exec_command('apt-get -y install apache2')
print 'This is output =',stdout.readlines()
print 'This is error =',stderr.readlines()
"""Copying the index.php file"""
print "\n Copying the Index.php file remotely"
# ssh = createSSHClient(hostname, 22, username, password)
scp = SCPClient(connect.get_transport())
scp.put(local_path, remote_path)
"""Customizing Apache on the servers"""
print "\n Customizing Apache "
stdin, stdout, stderr = connect.exec_command('mv /var/www/html/index.html /var/www/html/index.html_backup')
print 'This is output =',stdout.readlines()
print 'This is error =',stderr.readlines()
"""Changing Permissions on index.php file"""
print "\n Changing File permissions "
stdin, stdout, stderr = connect.exec_command('chown root:root /var/www/html/index.php')
print 'This is output =',stdout.readlines()
print 'This is error =',stderr.readlines()
"""Customizing Apache on the servers"""
print "\n Changing File permissions "
stdin, stdout, stderr = connect.exec_command('chmod 644 /var/www/html/index.php')
print 'This is output =',stdout.readlines()
print 'This is error =',stderr.readlines()
"""Restarting the Apache server"""
print "\n Restarting the Apache Server "
stdin, stdout, stderr = connect.exec_command('service apache2 restart')
print 'This is output =',stdout.readlines()
print 'This is error =',stderr.readlines()
"""Verify that http server is up and can spit out Hello World"""
print "\n Verifying http server"
stdin, stdout, stderr = connect.exec_command('curl -sv "http://localhost"| grep -i hello')
list_http = stdout.readlines()
print 'This is output =',list_http
print 'This is error =',stderr.readlines()
i = 0
for each in list_http:
if re.search(r'Hello', each) != None:
print "\n ***HTTP server is up and running with Hello World*** \n\n\n"
exit
else:
i =+1
if i > 0:
print "\n ***HTTP Server does not have Hello World*** \n\n\n"
connect.close()
file_name = raw_input("Enter file name containing hostnames:")
username = raw_input("Enter username:")
password = getpass.getpass('password: ')
local_path = '/Users/rdevineni/Documents/slack/files/index.php'
remote_path = '/var/www/html/'
try:
content = open(file_name, 'r')
except IOError:
print "There was an error reading the file"
host_list = content.read().splitlines()
for hostname in host_list:
ping_check(hostname, username, password)