add regex support

This commit is contained in:
Domain
2018-09-28 13:45:40 +08:00
parent 441164363f
commit 24288fac9a
2 changed files with 38 additions and 13 deletions

View File

@@ -3,6 +3,7 @@ import { BaseSession } from 'terminus-terminal'
export interface LoginScript { export interface LoginScript {
expect?: string expect?: string
send: string send: string
isRegex?: boolean
} }
export interface SSHConnection { export interface SSHConnection {
@@ -37,6 +38,20 @@ export class SSHSession extends BaseSession {
if (this.scripts) { if (this.scripts) {
let found = false let found = false
for (let script of this.scripts) { for (let script of this.scripts) {
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)) { if (dataString.includes(script.expect)) {
console.log('Executing script:', script.send) console.log('Executing script:', script.send)
this.shell.write(script.send + '\n') this.shell.write(script.send + '\n')
@@ -46,6 +61,7 @@ export class SSHSession extends BaseSession {
break break
} }
} }
}
if (found) { if (found) {
this.executeUnconditionalScripts() this.executeUnconditionalScripts()

View File

@@ -94,6 +94,7 @@
tr tr
th String to expect th String to expect
th String to be sent th String to be sent
th Regex
th Actions th Actions
tr(*ngFor='let script of connection.scripts') tr(*ngFor='let script of connection.scripts')
td td
@@ -106,6 +107,10 @@
type='text', type='text',
[(ngModel)]='script.send' [(ngModel)]='script.send'
) )
td
toggle(
[(ngModel)]='script.isRegex',
)
td td
.input-group.flex-nowrap .input-group.flex-nowrap
button.btn.btn-outline-info.ml-0((click)='moveScriptUp(script)') button.btn.btn-outline-info.ml-0((click)='moveScriptUp(script)')
@@ -127,6 +132,10 @@
placeholder='Enter a string to be sent', placeholder='Enter a string to be sent',
[(ngModel)]='newScript.send' [(ngModel)]='newScript.send'
) )
td
toggle(
[(ngModel)]='newScript.isRegex',
)
td td
.input-group.flex-nowrap .input-group.flex-nowrap
button.btn.btn-outline-info.ml-0((click)='addScript()') button.btn.btn-outline-info.ml-0((click)='addScript()')