-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReboot_UCCX.py
87 lines (66 loc) · 2.67 KB
/
Reboot_UCCX.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
import pexpect
import time
import sys
import os
import re
from os import path
class RebootUccxServer:
def Ping(self,server):
pattern = r"win"
windows = re.search(pattern, sys.platform)
command = ("ping -n 6 "+str(server)) if windows else ("ping -c 6 "+str(server))
resp = os.system(command)
return (True if(resp == 0) else False)
def execute(self,hostname,username,password):
reboot_check={"status":None,"message":None}
try:
if self.Ping(hostname):
msg = f"INFO Server {hostname} is up.."
print (msg)
else:
msg = f"ERROR Server {hostname} is not up or reachable. "
print(msg)
reboot_check['status']=False
reboot_check['message']=msg
return reboot_check
#Starting SSH session
child = pexpect.spawn(f'ssh {username}@{hostname}')
child.logfile_read = sys.stdout.buffer
child.timeout = 4000
child.expect('.*password:')
child.sendline(f'{password}\n')
check = child.expect(['.admin:', "denied", "password", pexpect.EOF, pexpect.TIMEOUT], timeout=65)
if check != 0:
msg = f"ERROR Authentication Failed for {self.username} on {hostname}"
print(msg)
reboot_check['status']=False
reboot_check['message']=msg
return reboot_check
msg = f"INFO Logged in to the server {hostname} with {username}"
print(msg)
print("INFO Rebooting the Server")
#initiating the reboot
child.sendline("utils system restart\n")
check = child.expect(["no\) ?", pexpect.EOF, pexpect.TIMEOUT], timeout=25)
if check == 0:
child.sendline("yes\n")
time.sleep(20)
child.close(force=True)
reboot_check['status']=True
reboot_check['message']="Reboot Initiated Successfully"
return reboot_check
else:
child.close(force=True)
reboot_check['status']=False
reboot_check['message']="ERROR Reboot Initiation Failed"
return reboot_check
except Exception as e:
msg = f"ERROR Could not Reboot the server {hostname}"
print(msg)
reboot_check['status']=False
reboot_check['message']=str(e)
return reboot_check
if __name__=="__main__":
obj=RebootUccxServer()
reboot_dict=obj.execute("hostname","username","password")
print(reboot_dict)