前言
前几天学习了django-channels如何基于ws建立聊天室, 针对于ws在运维方面实际用途还是很多的.
今天基于ws,做一个webssh的小案例
用到的新模块
使用WebSSH操控远程服务器
在学习之前,需要简单了解一下paramiko的基本使用
1 2 3 4 5 6 7 8 9 10 11
| self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
self.ssh.connect(hostname="192.168.233.128", port=22, username="root", password="123456")
self.chan = self.ssh.invoke_shell(width=200, height=50)
|
建立channel以后,可以通过channel的send()方法向远程服务器发送数据.也可以通过recv()方法接受数据.
基本思路和实现步骤
我们需要在前端页面绘制出一个终端,并建立ws连接,后端建立连接前,使用paramiko 建立SSH channel。
当建立SSH连接成功以后,启动一个线程来循环监听发送和接受数据发送到前后端。
后端实现
完整的consumer代码如下
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
| from channels.generic.websocket import WebsocketConsumer import paramiko import threading
class WebSSH(threading.Thread): def __init__(self, chan, consum): super(WebSSH, self).__init__() self.chan = chan self.consum = consum
def run(self): while not self.chan.exit_status_ready(): try: data = self.chan.recv(1024) self.consum.send(data.decode()) except Exception as ex: print(str(ex))
class ChatConsumer(WebsocketConsumer): def connect(self): try: self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy) self.ssh.connect(hostname="192.168.233.128", port=22, username="root", password="123456") self.chan = self.ssh.invoke_shell(width=200, height=50) except Exception: self.close() self.accept() self.a = WebSSH(self.chan, self) self.a.setDaemon(True) self.a.start()
def disconnect(self, close_code): self.ssh.close()
def receive(self, text_data): self.chan.send(text_data)
|
前端实现
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
| <template> <div> <h1>webssh</h1> <div ref="xtermContainer"></div> </div> </template>
<script> import { Terminal } from 'xterm' import { FitAddon } from 'xterm-addon-fit' import { AttachAddon } from 'xterm-addon-attach'
export default { name: 'WebSSH', mounted () { const terminal = new Terminal({ rows: 35, cursorBlink: true, disableStdin: false }) const ws = new WebSocket('ws://127.0.0.1:8001/ws/webssh') const fitAddon = new FitAddon() const attachAddon = new AttachAddon(ws) terminal.loadAddon(fitAddon) terminal.loadAddon(attachAddon) terminal.open(this.$refs.xtermContainer) terminal.focus() terminal.resize(200, 50) fitAddon.fit() console.log(terminal.cols, terminal.rows) } } </script>
<style scoped> </style>
|
目前到此已经可以实现了WebSSH的基本功能了,是不是很简单呢?
资料