Created
November 18, 2024 09:00
-
-
Save CN-CODEGOD/a4a2987726778c84ba9bfb9b8c1ce621 to your computer and use it in GitHub Desktop.
JavaScript via 其他语言
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1.via bat: | |
| const { exec, spawn } = require('node:child_process'); | |
| exec('my.bat', (err, stdout, stderr) => { | |
| if (err) { | |
| console.error(err); | |
| return; | |
| } | |
| console.log(stdout); | |
| }); | |
| // Script with spaces in the filename: | |
| const bat = spawn('"my script.cmd"', ['a', 'b'], { shell: true }); | |
| // or: | |
| exec('"my script.cmd" a b', (err, stdout, stderr) => { | |
| // ... | |
| }); | |
| 2.via powershell: | |
| const { execFileSync } = require('node:child_process'); | |
| try { | |
| const stdout = execFileSync('powershell', ['123'], { | |
| // Capture stdout and stderr from child process. Overrides the | |
| // default behavior of streaming child stderr to the parent stderr | |
| stdio: 'pipe', | |
| // Use utf8 encoding for stdio pipes | |
| encoding: 'utf8', | |
| }); | |
| console.log(stdout); | |
| } catch (err) { | |
| if (err.code) { | |
| // Spawning child process failed | |
| console.error(err.code); | |
| } else { | |
| // Child was spawned but exited with non-zero exit code | |
| // Error contains any stdout and stderr from the child | |
| const { stdout, stderr } = err; | |
| console.error({ stdout, stderr }); | |
| } | |
| } | |
| 3.via python: | |
| const { execFileSync } = require('node:child_process'); | |
| try { | |
| const stdout = execFileSync('python.exe', ['arg'], { | |
| // Capture stdout and stderr from child process. Overrides the | |
| // default behavior of streaming child stderr to the parent stderr | |
| stdio: 'pipe', | |
| // Use utf8 encoding for stdio pipes | |
| encoding: 'utf8', | |
| }); | |
| console.log(stdout); | |
| } catch (err) { | |
| if (err.code) { | |
| // Spawning child process failed | |
| console.error(err.code); | |
| } else { | |
| // Child was spawned but exited with non-zero exit code | |
| // Error contains any stdout and stderr from the child | |
| const { stdout, stderr } = err; | |
| console.error({ stdout, stderr }); | |
| } | |
| } | |
| 4.via 文件: | |
| const { exec} = require('node:child_process') | |
| const os = require("os"); | |
| const iconv = require('iconv-lite'); | |
| const encoding = os.platform() == 'win32' ? 'cp936' : 'utf-8'; | |
| const binaryEncoding = 'binary'; | |
| // 执行命令并获取结果 | |
| const { argv } = require('node:process'); | |
| argv.forEach((args) => { | |
| const runExec = (powershell) => { | |
| return new Promise((resolve, reject) => { | |
| exec(powershell, { encoding: binaryEncoding }, function(error, stdout, stderr) { | |
| if (error) { | |
| reject(iconv.decode(new Buffer.from(error.message, binaryEncoding), encoding)); | |
| } else { | |
| resolve(iconv.decode(new Buffer.from(stdout, binaryEncoding), encoding)); | |
| } | |
| }); | |
| }) | |
| } | |
| var url = process.argv.slice(2); | |
| runExec(`"POWERSHELL" C:\\Users\\34683\\dre\\dre\\crawl1.ps1 ${url}`).then(res => { | |
| const jsdom = require("jsdom"); | |
| const dom = new jsdom.JSDOM(res) | |
| console.log(dom.window.document.title) | |
| }).catch(err => { | |
| console.log('执行失败', err); | |
| }) | |
| }); | |
| PS. | |
| .如果不想使用绝对路径,可以使用path 来定义相对路径 | |
| const { execFileSync } = require('node:child_process'); | |
| const path = require('node:path'); | |
| const os = require("os"); | |
| const iconv = require('iconv-lite'); | |
| // 设置编码 | |
| const encoding = os.platform() == 'win32' ? 'cp936' : 'utf-8'; | |
| // 获取当前路径 | |
| const currentDir = process.cwd(); | |
| // 构建 PowerShell 脚本路径 | |
| const scriptPath = path.join(currentDir, 'crawl1.ps1'); | |
| try { | |
| // 使用 execFileSync 运行 PowerShell 脚本 | |
| const stdout = execFileSync('powershell', [scriptPath], { | |
| // 设置同步执行 | |
| stdio: 'pipe', | |
| encoding: 'utf8', | |
| }); | |
| console.log(stdout); | |
| } catch (err) { | |
| if (err.code) { | |
| // 子进程执行失败 | |
| console.error(err.code); | |
| } else { | |
| // 子进程执行成功但出现错误 | |
| const { stdout, stderr } = err; | |
| console.error({ stdout, stderr }); | |
| } | |
| } | |
| .windows 中空格可能导致路径不完整而出错可以通过加入双引号而解决 | |
| : | |
| const { exec } = require('node:child_process'); | |
| const path = require('node:path'); | |
| const os = require("os"); | |
| const iconv = require('iconv-lite'); | |
| // 设置编码 | |
| const encoding = os.platform() == 'win32' ? 'cp936' : 'utf-8'; | |
| // 获取当前路径 | |
| const currentDir = process.cwd(); | |
| // 构建 PowerShell 脚本路径,并确保路径包含空格时被正确处理 | |
| const scriptPath = path.join(currentDir, 'crawl1.ps1'); | |
| // 异步执行 PowerShell 脚本,路径用双引号包裹 | |
| exec(`powershell -ExecutionPolicy Bypass -File "${scriptPath}"`, { encoding: 'binary' }, (error, stdout, stderr) => { | |
| if (error) { | |
| console.error(iconv.decode(Buffer.from(error.message, 'binary'), encoding)); | |
| } else { | |
| console.log(iconv.decode(Buffer.from(stdout, 'binary'), encoding)); | |
| } | |
| if (stderr) { | |
| console.error(iconv.decode(Buffer.from(stderr, 'binary'), encoding)); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment