-
Notifications
You must be signed in to change notification settings - Fork 324
Expand file tree
/
Copy pathdocker_ssh.py
More file actions
93 lines (77 loc) · 2.97 KB
/
docker_ssh.py
File metadata and controls
93 lines (77 loc) · 2.97 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
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: ssh
Description:
Author: pythonzm
date: 2019-11-05
-------------------------------------------------
Change Activity:
2019-11-05:
-------------------------------------------------
"""
# -*- coding: utf-8 -*-
import docker
import threading
from socket import timeout
from channels.generic.websocket import WebsocketConsumer
class MyThread(threading.Thread):
def __init__(self, sock):
super(MyThread, self).__init__()
self.sock = sock
self._stop_event = threading.Event()
def stop(self):
self._stop_event.set()
def run(self):
while not self._stop_event.is_set():
try:
data = self.sock.tty._sock.recv(1024)
if data:
str_data = data.decode('utf-8', 'ignore')
self.sock.send(str_data)
else:
return
except timeout:
break
self.sock.send('\n由于长时间没有操作,连接已断开!', close=True)
class DockerConsumer(WebsocketConsumer):
def __init__(self, *args, **kwargs):
super(DockerConsumer, self).__init__(*args, **kwargs)
self.client = docker.from_env()
self.t1 = MyThread(self)
# self.query = QueryDict(query_string=self.scope.get('query_string'), encoding='utf-8')
# self.height = int(self.query.get('height'))
# self.width = int(self.query.get('width'))
self.tty = None
def connect(self):
if self.scope["user"].is_anonymous:
self.close(code=1007)
else:
self.accept()
cmds = ['/bin/bash', '/bin/sh']
for cmd in cmds:
try:
resp = self.client.api.exec_create('ae89214140b5', cmd=cmd, stdout=True, stderr=True, stdin=True,
tty=True)
self.tty = self.client.api.exec_start(
resp['Id'], detach=False, tty=True, stream=False, socket=True
)
prompt_data = self.tty._sock.recv(1024).decode('utf-8')
# 如果容器有/bin/bash就跳出循环,如果没有,就使用/bin/sh
if not r'/bin/bash: no such file or directory' in prompt_data:
self.send(prompt_data)
break
except Exception as e:
self.send('通过web连接容器失败!原因:{}'.format(e), close=True)
# 设置如果3分钟没有任何输入,就断开连接
self.tty._sock.settimeout(60 * 3)
self.t1.setDaemon(True)
self.t1.start()
def receive(self, text_data=None, bytes_data=None):
self.tty._sock.send(text_data.encode('utf-8'))
def disconnect(self, close_code):
try:
self.tty._sock.close()
self.client.close()
finally:
self.t1.stop()