-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLazY.py
162 lines (143 loc) · 4.86 KB
/
LazY.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import paramiko
import sys
import os
import time
import subprocess
hostlist = ['192.168.0.1','192.168.0.2'] #Ip`s without port. SSH server port must be 22. Can be multiple.
username = 'user' #Username of the windows account. Must be admin.
password = 'password' #The password of the account
auto_reconnect = False #If set to True, will ping the host until it is back online except when doing a shutdown.
os.system('cls' if os.name == 'nt' else 'clear')
print("LazY is running")
print("Made by Blnix.")
print("")
while True:
if len(hostlist) > 1:
print("Multiple host found:")
for index, item in enumerate(hostlist, start=1):
print(f"{index}. {item}")
index_position = len(hostlist) + 1
print(str(index_position) + ". To use another IP.")
user_choice = input()
if user_choice.isdigit() and 1 <= int(user_choice) <= len(hostlist):
host = hostlist[int(user_choice) - 1]
break
elif user_choice.isdigit() and int(user_choice) > len(hostlist):
print("What ip do you want to connect to?")
host = input()
break
else:
os.system('cls' if os.name == 'nt' else 'clear')
print("Invalid choice. Please enter a valid number.")
else:
host = hostlist[0]
break
def create_ssh(host=host, username=username, password=password):
try:
global ssh
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=username, password=password)
print("connected")
return ssh
except paramiko.AuthenticationException:
print("Authentication failed. Check your username and password.")
except paramiko.SSHException as e:
print(f"Unable to establish SSH connection: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if auto_reconnect:
wait_for_host()
else:
sys.exit()
def shutdown_target():
print("Trying to shutdown target")
try:
ssh.exec_command('shutdown /s /t 0')
print("Target will shutdown soon")
except Exception as e:
print("Something went wrong:", str(e))
def reboot_target():
print("Trying to reboot target")
try:
ssh.exec_command('shutdown /r /t 0')
print("Target will reboot soon")
except Exception as e:
print("Something went wrong:", str(e))
def sleep_target():
print("Trying to turn to sleep mode")
try:
ssh.exec_command('rundll32.exe powrprof.dll,SetSuspendState 0,1,0')
print("Target will sleep soon")
except Exception as e:
print("Something went wrong:", str(e))
def logoff_target():
global ssh
try:
print("Closing connection")
ssh.close()
print("Closed")
except:
print("Exiting script.")
ssh = None
def wait_for_host():
global ssh
print("Waiting for host to start:")
time.sleep(5)
online = False
check = 0
while not online:
progressbar = "[" + "#" * check + " " * (30 - check) + "]"
print("\r" + progressbar, end="", flush=True)
time.sleep(0.1)
check = check + 1
if check > 30:
os.system('cls' if os.name == 'nt' else 'clear')
print("Checking host.")
is_host_up = ping()
if is_host_up:
print("Host is back up!")
ssh = create_ssh()
online = True
else:
print("Host is still down.")
check = 0
def ping():
if os.name == 'nt':
ping_command = f"ping -n 1 {host}"
else:
ping_command = f"ping -c 1 {host}"
try:
ping_result = str(subprocess.check_output(ping_command, shell=True))
except:
ping_result = ""
return 'ttl' in ping_result.lower()
print(f"Connecting to: {host}")
ssh = create_ssh()
while True:
print("Commands to execute:")
print("1. for shutdown")
print("2. for reboot")
print("3. for sleep")
print("4. for exit")
command = input()
if command == '1':
shutdown_target()
logoff_target()
sys.exit()
elif command == '2':
reboot_target()
elif command == '3':
sleep_target()
elif command == '4':
logoff_target()
sys.exit()
else:
os.system('cls' if os.name == 'nt' else 'clear')
print(f"{command} is not a valid input.")
if (command == '2' or command == '3') and not auto_reconnect:
logoff_target()
sys.exit()
elif (command == '2' or command == '3') and auto_reconnect:
logoff_target()
wait_for_host()