huangfh 3 months ago
parent
commit
5785cf064d
1 changed files with 17 additions and 9 deletions
  1. 17 9
      scripts/upload/upload.js

+ 17 - 9
scripts/upload/upload.js

@@ -13,13 +13,21 @@ const url = process.argv[7]
 const [serverIp, port] = serverAddress.split(':');
 const [serverIp, port] = serverAddress.split(':');
 const sshPort = port || '22';  // 如果没有指定端口,默认使用22
 const sshPort = port || '22';  // 如果没有指定端口,默认使用22
 
 
+// 这里填入您之前获取的主机密钥的 SHA256 指纹
+//plink -ssh root@192.168.0.109 -P 22 exit
+const hostKey = "SHA256:qS+DxUoW0NM1Nc//Dh0ebUHTB+MXMNTr+BF0njciwrU"
+
 // 检查命令是否可用
 // 检查命令是否可用
 function commandExists(command) {
 function commandExists(command) {
     try {
     try {
+      if (process.platform === 'win32') {
+        execSync(`where ${command}`, { stdio: 'ignore' });
+      } else {
         execSync(`which ${command}`, { stdio: 'ignore' });
         execSync(`which ${command}`, { stdio: 'ignore' });
-        return true;
+      }
+      return true;
     } catch (e) {
     } catch (e) {
-        return false;
+      return false;
     }
     }
 }
 }
 
 
@@ -43,12 +51,12 @@ function uploadServer() {
     const sshCommand = `cd ${filePath} && rm -rf static && unzip -o ${fileName}`;
     const sshCommand = `cd ${filePath} && rm -rf static && unzip -o ${fileName}`;
     if (process.platform === 'win32') {
     if (process.platform === 'win32') {
         // Windows
         // Windows
-        runCommand(`pscp -P ${sshPort} -pw ${password} ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
-        runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw ${password} -batch "${sshCommand}"`);
+        runCommand(`pscp -P ${sshPort} -pw ${password} -hostkey "${hostKey}" ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
+        runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw ${password} -hostkey "${hostKey}" -batch "${sshCommand}"`);
     } else {
     } else {
         // Mac/Linux
         // Mac/Linux
-        runCommand(`sshpass -p "${password}" scp -P ${sshPort} ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
-        runCommand(`sshpass -p "${password}" ssh -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
+        runCommand(`sshpass -p "${password}" scp -o StrictHostKeyChecking=no -P ${sshPort} ./zip/${fileName} ${username}@${serverIp}:${filePath}`);
+        runCommand(`sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
     }
     }
     console.log('编译打包后自动部署到服务器上完成');
     console.log('编译打包后自动部署到服务器上完成');
     console.log(`服务器上的地址:${url}`);
     console.log(`服务器上的地址:${url}`);
@@ -60,10 +68,10 @@ function delPublic(name) {
     const sshCommand = `cd ${filePath} && rm -rf ${name}`;
     const sshCommand = `cd ${filePath} && rm -rf ${name}`;
     if (process.platform === 'win32') {
     if (process.platform === 'win32') {
         // Windows
         // Windows
-        runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw ${password} -batch "${sshCommand}"`);
+        runCommand(`plink -P ${sshPort} -ssh ${username}@${serverIp} -pw ${password} -hostkey "${hostKey}" -batch "${sshCommand}"`);
     } else {
     } else {
         // Mac/Linux
         // Mac/Linux
-        runCommand(`sshpass -p "${password}" ssh -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
+        runCommand(`sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -p ${sshPort} ${username}@${serverIp} "${sshCommand}"`);
     }
     }
     console.log(`${name} 等目录移除完成`);
     console.log(`${name} 等目录移除完成`);
 }
 }
@@ -84,7 +92,7 @@ function runCode() {
 
 
     //判断命令是否支持
     //判断命令是否支持
     if (process.platform === 'win32') {
     if (process.platform === 'win32') {
-        if (!commandExists('putty')) {
+        if (!commandExists('pscp') || !commandExists('plink')) {
             console.log('putty 未安装,请先安装 putty');
             console.log('putty 未安装,请先安装 putty');
             process.exit(1);
             process.exit(1);
             return
             return