From 24288fac9afd8ce958928c675e8535aef869ee95 Mon Sep 17 00:00:00 2001 From: Domain Date: Fri, 28 Sep 2018 13:45:40 +0800 Subject: [PATCH 1/3] add regex support --- terminus-ssh/src/api.ts | 30 ++++++++++++++----- .../editConnectionModal.component.pug | 21 +++++++++---- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/terminus-ssh/src/api.ts b/terminus-ssh/src/api.ts index df8e53be..ef3435de 100644 --- a/terminus-ssh/src/api.ts +++ b/terminus-ssh/src/api.ts @@ -3,6 +3,7 @@ import { BaseSession } from 'terminus-terminal' export interface LoginScript { expect?: string send: string + isRegex?: boolean } export interface SSHConnection { @@ -37,13 +38,28 @@ export class SSHSession extends BaseSession { if (this.scripts) { let found = false for (let script of this.scripts) { - if (dataString.includes(script.expect)) { - console.log('Executing script:', script.send) - this.shell.write(script.send + '\n') - this.scripts = this.scripts.filter(x => x !== script) - found = true - } else { - break + if (script.isRegex) { + let re = new RegExp(script.expect, "g"); + + if (dataString.match(re)) { + let cmd = dataString.replace(re, script.send); + console.log('Executing script:', cmd) + this.shell.write(cmd + '\n') + this.scripts = this.scripts.filter(x => x !== script) + found = true + } else { + break; + } + } + else { + if (dataString.includes(script.expect)) { + console.log('Executing script:', script.send) + this.shell.write(script.send + '\n') + this.scripts = this.scripts.filter(x => x !== script) + found = true + } else { + break + } } } diff --git a/terminus-ssh/src/components/editConnectionModal.component.pug b/terminus-ssh/src/components/editConnectionModal.component.pug index 50a01663..67c038c1 100644 --- a/terminus-ssh/src/components/editConnectionModal.component.pug +++ b/terminus-ssh/src/components/editConnectionModal.component.pug @@ -94,18 +94,23 @@ tr th String to expect th String to be sent + th Regex th Actions tr(*ngFor='let script of connection.scripts') td input.form-control( - type='text', - [(ngModel)]='script.expect' - ) + type='text', + [(ngModel)]='script.expect' + ) td input.form-control( - type='text', - [(ngModel)]='script.send' - ) + type='text', + [(ngModel)]='script.send' + ) + td + toggle( + [(ngModel)]='script.isRegex', + ) td .input-group.flex-nowrap button.btn.btn-outline-info.ml-0((click)='moveScriptUp(script)') @@ -127,6 +132,10 @@ placeholder='Enter a string to be sent', [(ngModel)]='newScript.send' ) + td + toggle( + [(ngModel)]='newScript.isRegex', + ) td .input-group.flex-nowrap button.btn.btn-outline-info.ml-0((click)='addScript()') From 9eee600ccc75d650c5d3385c4ded157d9a4d86c3 Mon Sep 17 00:00:00 2001 From: Domain Date: Fri, 28 Sep 2018 14:46:32 +0800 Subject: [PATCH 2/3] optional script --- terminus-ssh/src/api.ts | 35 ++++++++++++------- .../editConnectionModal.component.pug | 9 +++++ .../editConnectionModal.component.ts | 2 ++ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/terminus-ssh/src/api.ts b/terminus-ssh/src/api.ts index ef3435de..0721d65e 100644 --- a/terminus-ssh/src/api.ts +++ b/terminus-ssh/src/api.ts @@ -4,6 +4,7 @@ export interface LoginScript { expect?: string send: string isRegex?: boolean + optional?: boolean } export interface SSHConnection { @@ -38,26 +39,36 @@ export class SSHSession extends BaseSession { if (this.scripts) { let found = false for (let script of this.scripts) { + let match = false + let cmd = "" if (script.isRegex) { - let re = new RegExp(script.expect, "g"); - + let re = new RegExp(script.expect, "g") if (dataString.match(re)) { - let cmd = dataString.replace(re, script.send); - console.log('Executing script:', cmd) - this.shell.write(cmd + '\n') - this.scripts = this.scripts.filter(x => x !== script) + cmd = dataString.replace(re, script.send) + match = true found = true - } else { - break; } } else { if (dataString.includes(script.expect)) { - console.log('Executing script:', script.send) - this.shell.write(script.send + '\n') - this.scripts = this.scripts.filter(x => x !== script) + cmd = script.send + match = true found = true - } else { + } + } + + if (match) { + console.log('Executing script: "' + cmd + '"') + this.shell.write(cmd + '\n') + this.scripts = this.scripts.filter(x => x !== script) + } + else { + if (script.optional) { + console.log("Skip optional script: " + script.expect) + found = true + this.scripts = this.scripts.filter(x => x !== script) + } + else { break } } diff --git a/terminus-ssh/src/components/editConnectionModal.component.pug b/terminus-ssh/src/components/editConnectionModal.component.pug index 67c038c1..e2b7ce73 100644 --- a/terminus-ssh/src/components/editConnectionModal.component.pug +++ b/terminus-ssh/src/components/editConnectionModal.component.pug @@ -95,6 +95,7 @@ th String to expect th String to be sent th Regex + th Optional th Actions tr(*ngFor='let script of connection.scripts') td @@ -111,6 +112,10 @@ toggle( [(ngModel)]='script.isRegex', ) + td + toggle( + [(ngModel)]='script.optional', + ) td .input-group.flex-nowrap button.btn.btn-outline-info.ml-0((click)='moveScriptUp(script)') @@ -136,6 +141,10 @@ toggle( [(ngModel)]='newScript.isRegex', ) + td + toggle( + [(ngModel)]='newScript.optional', + ) td .input-group.flex-nowrap button.btn.btn-outline-info.ml-0((click)='addScript()') diff --git a/terminus-ssh/src/components/editConnectionModal.component.ts b/terminus-ssh/src/components/editConnectionModal.component.ts index f67a01ff..560b91f5 100644 --- a/terminus-ssh/src/components/editConnectionModal.component.ts +++ b/terminus-ssh/src/components/editConnectionModal.component.ts @@ -83,5 +83,7 @@ export class EditConnectionModalComponent { clearScript () { this.newScript.expect = '' this.newScript.send = '' + this.newScript.isRegex = false + this.newScript.optional = false } } From 0254cabddea0329a6adc460c0d355926f0aec3c9 Mon Sep 17 00:00:00 2001 From: Domain Date: Mon, 8 Oct 2018 14:01:13 +0800 Subject: [PATCH 3/3] lint --- terminus-ssh/src/api.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/terminus-ssh/src/api.ts b/terminus-ssh/src/api.ts index 0721d65e..f2650340 100644 --- a/terminus-ssh/src/api.ts +++ b/terminus-ssh/src/api.ts @@ -40,16 +40,15 @@ export class SSHSession extends BaseSession { let found = false for (let script of this.scripts) { let match = false - let cmd = "" + let cmd = '' if (script.isRegex) { - let re = new RegExp(script.expect, "g") + let re = new RegExp(script.expect, 'g') if (dataString.match(re)) { cmd = dataString.replace(re, script.send) match = true found = true } - } - else { + } else { if (dataString.includes(script.expect)) { cmd = script.send match = true @@ -61,14 +60,12 @@ export class SSHSession extends BaseSession { console.log('Executing script: "' + cmd + '"') this.shell.write(cmd + '\n') this.scripts = this.scripts.filter(x => x !== script) - } - else { + } else { if (script.optional) { - console.log("Skip optional script: " + script.expect) + console.log('Skip optional script: ' + script.expect) found = true this.scripts = this.scripts.filter(x => x !== script) - } - else { + } else { break } }