Nodejs 网页版本的linux/Unix客户端webshell

Nodejs 网页版本的linux/Unix客户端webshell

webshell是一个在网页上在线管理linux/unix操作系统的客户端,界面操作效果和著名的xshell一样; 所以取名为webshell。 使用方法:在linux/unix安装好nodejs以及部分模块环境之后,拷贝源码到服务器,进入目录, 执行node app 打开http://127.0.0.1:9000/index.htm就可以看到效果了。 因为需要随时随地管理服务器,看到nodejs比较方便实现所以拿nodejs实现了一个,代码挺少的,感兴趣的同学可以完善一下。 下载地址: https://github.com/Einsy/webshell


3 回复

Nodejs 网页版本的Linux/Unix客户端WebShell

简介

WebShell 是一个在网页上在线管理 Linux/Unix 操作系统的客户端工具。它的界面操作效果类似于著名的 Xshell,因此取名为 WebShell。这个工具允许用户通过网页浏览器直接管理和操作远程的 Linux/Unix 服务器。

使用方法

在 Linux/Unix 系统中安装好 Node.js 及其所需的模块环境后,你可以按照以下步骤来设置并运行 WebShell

  1. 克隆仓库: 首先,你需要从 GitHub 克隆项目仓库到你的服务器上。

    git clone https://github.com/Einsy/webshell.git
    
  2. 安装依赖: 进入项目目录,并安装所需的 Node.js 模块。

    cd webshell
    npm install
    
  3. 启动服务: 在项目目录下,运行 Node.js 应用程序。

    node app
    
  4. 访问 WebShell: 打开浏览器并访问 http://127.0.0.1:9000/index.html,你将看到 WebShell 的界面。

示例代码

以下是一个简单的 Node.js 服务器代码片段,用于创建一个基本的 WebShell 界面:

const http = require('http');
const fs = require('fs');

// 创建 HTTP 服务器
const server = http.createServer((req, res) => {
    if (req.url === '/index.html') {
        // 读取 index.html 文件内容
        fs.readFile('index.html', 'utf8', (err, data) => {
            if (err) {
                console.error(err);
                res.writeHead(500);
                res.end('Internal Server Error');
            } else {
                res.writeHead(200, { 'Content-Type': 'text/html' });
                res.end(data);
            }
        });
    } else {
        res.writeHead(404);
        res.end('Not Found');
    }
});

// 监听端口
server.listen(9000, () => {
    console.log('Server is running on port 9000');
});

解释

  • HTTP 服务器:使用 Node.js 的内置 http 模块创建一个 HTTP 服务器。
  • 文件读取:通过 fs 模块读取 index.html 文件的内容,并将其发送给客户端。
  • 端口监听:服务器在端口 9000 上监听请求。

完善

由于这是一个基础示例,实际的 WebShell 功能还需要进一步完善,例如添加与服务器交互的功能(如执行命令、显示输出等)。感兴趣的开发者可以继续扩展和完善这个项目。

下载地址

你可以从 GitHub 下载完整的项目代码: https://github.com/Einsy/webshell

希望这些信息对你有所帮助!


QQ截图20141005221751.png 效果如图,在新标签里打开图片可以看到更清晰的效果。

根据你的描述,这个帖子介绍的是一个基于 Node.js 的网页版 Linux/Unix 客户端 webshell。这种工具可以在网页上直接管理和操作远程服务器。以下是如何实现这样一个简单的 webshell 的步骤及一些关键代码示例。

实现思路

  1. 后端(Node.js):使用 Node.js 来处理与服务器的交互,接收来自前端的命令并返回结果。
  2. 前端(HTML+JavaScript):创建一个简单的 HTML 页面用于输入命令,并通过 JavaScript 与后端进行通信。

示例代码

后端(Node.js)

首先,我们需要安装必要的依赖包。这里我们使用 express 作为 web 框架,child_process 用于执行系统命令:

npm install express

然后,创建一个基本的 Node.js 应用程序来处理请求和响应:

// app.js
const express = require('express');
const { exec } = require('child_process');

const app = express();
const PORT = 9000;

app.use(express.static('public'));

app.post('/run', (req, res) => {
    const cmd = req.body.command;
    exec(cmd, (error, stdout, stderr) => {
        if (error) {
            console.error(`执行错误: ${error}`);
            return res.send({ error: stderr });
        }
        res.send({ output: stdout });
    });
});

app.listen(PORT, () => {
    console.log(`服务器运行在 http://localhost:${PORT}`);
});

前端(HTML + JavaScript)

接下来,创建一个简单的 HTML 文件用于用户输入命令并显示结果:

<!-- public/index.htm -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WebShell</title>
</head>
<body>
    <h1>WebShell</h1>
    <input type="text" id="commandInput" placeholder="输入命令">
    <button onclick="runCommand()">执行</button>
    <pre id="output"></pre>

    <script>
        function runCommand() {
            const command = document.getElementById('commandInput').value;
            fetch('/run', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ command })
            }).then(response => response.json())
              .then(data => {
                  document.getElementById('output').innerText = data.output;
              }).catch(error => {
                  console.error('Error:', error);
              });
        }
    </script>
</body>
</html>

运行应用

确保所有文件位于同一目录下,运行 Node.js 服务:

node app.js

然后访问 http://localhost:9000/index.htm 即可看到页面并开始使用。

注意事项

  • 本示例仅为演示用途,实际部署时需要考虑安全性问题,比如命令注入攻击等。
  • 确保 Node.js 和相关依赖已正确安装,并且具有执行系统命令的权限。
  • 考虑使用 HTTPS 来提高安全性。
回到顶部