-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote_shell.py
More file actions
145 lines (114 loc) · 3.84 KB
/
remote_shell.py
File metadata and controls
145 lines (114 loc) · 3.84 KB
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
# -*- coding: utf-8 -*-
# Python 3.6
# Python_networking | remote_shell
# 22.07.2017 Tomasz Wisniewski
"""
Remote command shell for Windows (for now).
Alpha stage.
TODO:
- add exit exit route
- work on cd. it seems like wd server side always starts in the same directory. maybe add variable to store path.
"""
import socket
import subprocess, os, sys
import argparse
import locale
remote_server = "localhost"
port = 6666
# encoding = "utf-8"
# encoding = "windows-1250"
# Configure locale from the user's environment settings.
locale.setlocale(locale.LC_ALL, '')
# Wrap stdout with an encoding-aware writer.
lang, encoding = locale.getdefaultlocale()
def create_server():
try:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(("", port))
return server
except Exception as ex:
print("Error creating socket.")
def create_client():
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
return client
except Exception as ex:
print("Error creating socket.")
def server_loop():
""" Far end of the connection. This should be running on remote system. """
server = create_server()
server.listen(1)
print("Shell server is running on port {}".format(port))
connection, address = server.accept()
welcome_msg = "Hello {}".format(address[0]).encode(encoding)
connection.sendall(welcome_msg)
try:
while True:
received = connection.recv(1024) # receive commands
if not received:
break
# execute command
cmd = received.decode(encoding)
if cmd == "stop":
break
output = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# send back the results
if not output.stdout:
print(output) # debug
if output.returncode == 0:
data = "command successfully executed.".encode(encoding)
elif output.returncode == 1:
data = "command failed.\n" + output.stderr.decode(encoding)
data = data.encode(encoding)
print(data)
else:
data = output.stdout
print(data.decode(encoding))
connection.sendall(data)
except KeyboardInterrupt:
print("Interrupted.")
except TypeError as terr:
connection.sendall("stop".encode(encoding))
finally:
server.close()
connection.close()
def client_connection():
""" Client sends commands to execute on remote server."""
client = create_client()
client.connect((remote_server, port))
try:
data = client.recv(1024)
print(data.decode())
while True:
cmd = input("$:").strip()
if cmd == "stop": # temporary exit route
cmd = cmd.encode(encoding)
client.sendall(cmd)
data = client.recv(4096)
print(data.decode(encoding))
client.shutdown(2)
break
else:
cmd = cmd.encode(encoding)
client.sendall(cmd)
data = client.recv(4096)
print("server:\n{}".format(data.decode(encoding)))
if data == "stop":
break
except KeyboardInterrupt as key:
print(key)
except TypeError as terr:
client.sendall("quit")
finally:
client.close()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("mode", help="c | s - client / server")
args = parser.parse_args()
if args.mode == "s":
server_loop()
elif args.mode == "c":
client_connection()
if __name__ == '__main__':
main()