//win.js
const express = require('express');
const { exec } = require('child_process');
const app = express();
const port = 8081;
app.get('/connect', (req, res) => {
const ip = req.query.ip;
exec(`mstsc /v:${ip}`, (error, stdout, stderr) => {
if (error) {
console.error(`执行的错误: ${error}`);
return res.json({ success: false });
}
res.json({ success: true });
});
});
app.listen(port, () => {
console.log(`本地连接服务运行在 http://localhost:${port}`);
});
本地npm start win.js运行js文件可以开启本地8081端口的mstsc服务。可在java代码中,通过fetch方法进行调用,benip为运行win.js的机器ip,代码中ip为要远程连接的机器ip
fetch(`http://${benIP}:8081/connect?ip=${ip}`)
.then(response => response.json())
.then(data => {
if (data.success) {
alert('远程桌面连接启动成功!');
} else {
alert('远程桌面连接启动失败!');
}
})
.catch(error => {
console.error('Error:', error);
});
运行截图
链接成功