vscode插件开发记录---ssh调用的两种实现方式对比
【摘要】 方式一---依赖ssh.exe的子进程调用:import * as child from "child_process";const childProc = child.spawn(command, args, Object.assign({}, options, { env: currentEnv }));childProc.stdout.on("data", (data) => { ...
方式一---依赖ssh.exe的子进程调用:
import * as child from "child_process"; const childProc = child.spawn(command, args, Object.assign({}, options, { env: currentEnv })); childProc.stdout.on("data", (data) => { data = data.toString(); result = result.concat(data); }); childProc.stderr.on("data", (data) => { }); childProc.on("error", reject); childProc.on("close", (code) => { }); //==================================== let ls = child.exec(command, options); ls.stdout.setEncoding('binary'); ls.stdout.on('data', (data:string) => { let dd = iconv.decode(new Buffer(data, 'binary'), 'UTF-8'); ... }); ls.stderr.setEncoding('binary'); ls.stderr.on('data', (data:string) => { let dd = iconv.decode(new Buffer(data, 'binary'), 'UTF-8'); ... }); ls.on('error', function (error) { reject(error); return; }); ls.on('close', (code) => { if (error.length > 0 && code != 0) { reject(error + `error code:${code}`); return; } resolve(error); });
方式二---依赖纯ts实现的ssh2实现ssh调用:
ssh2的一些使用参考:https://github.com/cweijan/vscode-ssh ssh.client?.exec("echo 123",function(err, stream){ if (err) throw err; stream.on('data', async function(data:any) { const tempPath = await FileManager.record(`temp/result.mul`, null, FileModel.WRITE); .... }); }); ssh.client?.shell(false,function(err,stream){ if (err) throw err; stream.on('data', function(data: string) { Console.log('STDOUT: '+ data); }).stderr.on('data', function(data) { Console.log('STDERR: '+ data); }); stream.write("bash -c '/xxx/install.sh'\n"); stream.write("bash -c '/xxx/config.sh'\n"); })
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)