31 lines
1.0 KiB
JavaScript
31 lines
1.0 KiB
JavaScript
const { Client } = require('ssh2');
|
|
const fs = require('fs');
|
|
|
|
const conn = new Client();
|
|
|
|
conn.on('ready', () => {
|
|
console.log('=== 上传脚本 ===');
|
|
|
|
conn.sftp((err, sftp) => {
|
|
if (err) { console.log('SFTP ERR:', err); conn.end(); return; }
|
|
|
|
const scriptContent = fs.readFileSync('set-password.sh', 'utf-8');
|
|
const writeStream = sftp.createWriteStream('/www/huishou/set-password.sh');
|
|
writeStream.write(scriptContent);
|
|
writeStream.end();
|
|
|
|
writeStream.on('close', () => {
|
|
console.log('脚本上传成功');
|
|
sftp.end();
|
|
|
|
console.log('\n=== 执行脚本 ===');
|
|
conn.exec('chmod +x /www/huishou/set-password.sh && /www/huishou/set-password.sh', (err2, stream2) => {
|
|
if (err2) { console.log('ERR2:', err2); conn.end(); return; }
|
|
stream2.on('data', (d) => console.log(d.toString()));
|
|
stream2.on('close', () => conn.end());
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
conn.connect({ host: 'hs.yifenbao.cn', port: 22, username: 'root', password: 'Torch1112' }); |