-
Notifications
You must be signed in to change notification settings - Fork 19
/
OpenWrt重拨_Bncr.js
59 lines (56 loc) · 1.79 KB
/
OpenWrt重拨_Bncr.js
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
/**作者
* @author 薛定谔的大灰机
* @name OpenWrt
* @origin 大灰机
* @version 1.0.0
* @description OpenWrt重拨
* @platform tgBot qq ssh HumanTG wxQianxun wxXyo
* @rule op重拨
* @admin true
* @disable false
*/
sysMethod.testModule(['ssh2'], { install: true });
const Client = require('ssh2').Client;
// SSH参数
const host = '10.0.0.1'
const port = 22
const username = ''
const password = ''
const command = '/sbin/ifdown wan;sleep 1s;ifup wan' // 执行命令
module.exports = async s => {
try {
output = await sshExecCommand(host, port, username, password, command);
console.log("重拨结果:" + ":" + output.replace("\n", "").replace("Done", "成功"));
await s.reply("重拨结果:" + ":" + output.replace("\n", "").replace("Done", "成功"));
} catch (error) {
console.error(error);
await s.reply(error);
}
}
async function sshExecCommand(host, port, username, password, command) {
return new Promise((resolve, reject) => {
conn = new Client();
conn.on('ready', () => {
conn.exec(command, (err, stream) => {
if (err) {
reject(err);
return;
}
let output = '';
stream.on('close', (code, signal) => {
conn.end();
resolve(output);
}).on('data', (data) => {
output += data.toString();
}).stderr.on('data', (data) => {
console.error('STDERR: ' + data);
});
});
}).connect({
host: host,
port: port,
username: username,
password: password
});
});
}