-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remote_Session_Assistant.py
111 lines (92 loc) Β· 3.95 KB
/
Remote_Session_Assistant.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
import paramiko
import sys
class PowerShellRemoteSession:
def __init__(self, host, port, username, password):
self.host = host
self.port = port
self.username = username
self.password = password
self.client = None
def connect(self):
"""Establish an SSH connection to the remote host."""
try:
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(self.host, port=self.port, username=self.username, password=self.password)
print(f"Connected to {self.host}")
except Exception as e:
print(f"Error connecting to {self.host}: {e}")
return False
return True
def execute_powershell_command(self, command):
"""Execute a PowerShell command remotely and return the output."""
if not self.client:
print("No active SSH session.")
return None
powershell_command = f"powershell -Command \"{command}\""
try:
stdin, stdout, stderr = self.client.exec_command(powershell_command)
# Read stdout and stderr to ensure we capture all output
output = stdout.read().decode("utf-8")
error_output = stderr.read().decode("utf-8")
if error_output:
print(f"Error: {error_output}")
return None
return output
except Exception as e:
print(f"Error executing command: {e}")
return None
def close_connection(self):
"""Close the SSH connection."""
if self.client:
self.client.close()
print(f"Connection to {self.host} closed.")
else:
print("No active SSH session to close.")
# Function to ask for user input to establish a connection
def get_connection_details():
host = input("Host (IP or domain): ")
port = int(input("Port (default 22 for SSH): ") or 22)
username = input("SSH Username: ")
password = input("SSH Password: ")
return host, port, username, password
# Main function for interactive session
def start_remote_session():
print("PowerShell Remote Session Assistant")
print("------------------------------------------------------------")
# Get the connection details from the user
host, port, username, password = get_connection_details()
ps_session = PowerShellRemoteSession(host, port, username, password)
while True:
# Try to connect to the remote machine
if ps_session.connect():
break
else:
print("Failed to connect. Please check your connection details.")
reconnect = input("Would you like to try again? (y/n): ").strip().lower()
if reconnect != 'y':
print("Exiting...")
sys.exit(1)
# Main loop to allow the user to execute commands remotely
while True:
command = input("\nEnter a PowerShell command to run (or type 'exit' to close): ")
if command.lower() == 'exit':
print("Exiting the remote session.")
ps_session.close_connection()
break
# Execute the PowerShell command
print(f"Executing command: {command}")
output = ps_session.execute_powershell_command(command)
if output:
print(f"Output:\n{output}")
else:
print("No output received or command failed.")
# Ask if the user wants to continue
continue_command = input("Do you want to execute another command? (y/n): ").strip().lower()
if continue_command != 'y':
print("Exiting the remote session.")
ps_session.close_connection()
break
# Run the session
if __name__ == "__main__":
start_remote_session()