Skip to content

Commit d8d2b0f

Browse files
committed
Added processing of stdErr and codeErr command execution via ssh
1 parent 7ec4c33 commit d8d2b0f

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src/SshClient.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import SFTP from 'ssh2-promise';
88
import {EventDispatcher,Handler} from './EventDispatcher';
99
import {StatusResult,IotResult} from './IotResult';
1010
import {IoTHelper} from './Helper/IoTHelper';
11+
import { stderr } from 'process';
1112

1213
export class SshClient {
1314
constructor(){}
@@ -94,22 +95,46 @@ export class SshClient {
9495
{
9596
let socket = await ssh.spawn(command);
9697
socket.on('data', (data:any) => {
98+
lastConsoleLine=data.toString();
9799
this.FireChangedState({
98100
status:undefined,
99101
console:data,
100102
obj:undefined
101103
});
102-
lastConsoleLine=data.toString();
103-
})
104+
});
104105
let flagExit:Boolean=false;
105-
socket.on('close', () => {
106+
let codeErr:string|undefined;
107+
let stdErr:string="";
108+
socket.stderr.on('data', (data:any) => {
109+
if(data) stdErr=`${stdErr}${data}`;
110+
});
111+
socket.on('close', (code:any) => {
112+
if (code) codeErr=code.toString();
106113
flagExit=true;
107-
});
114+
});
108115
//circle
109116
do{await IoTHelper.Sleep(300);}while(!flagExit);
117+
await IoTHelper.Sleep(500);
118+
//output
119+
if(stdErr!="") {
120+
stdErr=stdErr.replace(`W: `,`WARNING: `).replace(`E: `,`ERROR: `);
121+
stdErr=IoTHelper.StringTrim(stdErr);
122+
this.FireChangedState({
123+
status:undefined,
124+
console:`STDERR: ${stdErr}`,
125+
obj:undefined
126+
});
127+
}
128+
if(codeErr) {
129+
this.FireChangedState({
130+
status:undefined,
131+
console:`CODEERR: ${codeErr}`,
132+
obj:undefined
133+
});
134+
}
110135
//Check "Successfully"
111136
lastConsoleLine=IoTHelper.StringTrim(lastConsoleLine);
112-
if (!lastConsoleLine.includes("Successfully")){
137+
if ((!lastConsoleLine.includes("Successfully"))||codeErr){
113138
return Promise.resolve(new IotResult(StatusResult.Error,`The execution of the ${nameScript}.sh script ended with an error`,lastConsoleLine));
114139
}
115140
}

0 commit comments

Comments
 (0)