const { Client } = require('ssh2'); const fs = require('fs'); const path = require('path'); const conn = new Client(); const sourceDir = 'image'; const remoteDir = '/www/huishou/dist/build/h5/static/icons'; const icons = ['paper.png', 'metal.png', 'plastic.png', 'fabric.png', 'glass.png']; conn.on('ready', () => { console.log('=== 创建目录 ==='); conn.exec(`mkdir -p ${remoteDir}`, (err) => { if (err) { console.log('MKDIR ERR:', err); conn.end(); return; } let uploaded = 0; icons.forEach(icon => { const srcPath = path.join(sourceDir, icon); const destPath = `${remoteDir}/${icon}`; fs.readFile(srcPath, (err, content) => { if (err) { console.log('READ ERR:', srcPath, err); return; } conn.sftp((err, sftp) => { if (err) { console.log('SFTP ERR:', err); return; } const writeStream = sftp.createWriteStream(destPath); writeStream.write(content); writeStream.end(); writeStream.on('close', () => { sftp.end(); uploaded++; console.log(`已上传: ${icon} (${content.length} bytes)`); if (uploaded === icons.length) { console.log('\n=== 验证服务器图标 ==='); conn.exec(`ls -la ${remoteDir}/`, (err2, stream2) => { if (err2) { console.log('LS ERR:', err2); conn.end(); return; } stream2.on('data', (d) => console.log(d.toString())); stream2.on('close', () => { console.log('\n✅ 图标上传完成!'); conn.end(); }); }); } }); }); }); }); }); }); conn.connect({ host: 'hs.yifenbao.cn', port: 22, username: 'root', password: 'Torch1112' });