Merge pull request #437 from Domain/master

Support regex and optional script
This commit is contained in:
Eugene 2018-10-08 08:43:15 +02:00 committed by GitHub
commit 153cd1ec9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 12 deletions

View File

@ -3,6 +3,8 @@ import { BaseSession } from 'terminus-terminal'
export interface LoginScript {
expect?: string
send: string
isRegex?: boolean
optional?: boolean
}
export interface SSHConnection {
@ -37,13 +39,35 @@ 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
let match = false
let cmd = ''
if (script.isRegex) {
let re = new RegExp(script.expect, 'g')
if (dataString.match(re)) {
cmd = dataString.replace(re, script.send)
match = true
found = true
}
} else {
break
if (dataString.includes(script.expect)) {
cmd = script.send
match = true
found = true
}
}
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
}
}
}

View File

@ -94,18 +94,28 @@
tr
th String to expect
th String to be sent
th Regex
th Optional
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
toggle(
[(ngModel)]='script.optional',
)
td
.input-group.flex-nowrap
button.btn.btn-outline-info.ml-0((click)='moveScriptUp(script)')
@ -127,6 +137,14 @@
placeholder='Enter a string to be sent',
[(ngModel)]='newScript.send'
)
td
toggle(
[(ngModel)]='newScript.isRegex',
)
td
toggle(
[(ngModel)]='newScript.optional',
)
td
.input-group.flex-nowrap
button.btn.btn-outline-info.ml-0((click)='addScript()')

View File

@ -83,5 +83,7 @@ export class EditConnectionModalComponent {
clearScript () {
this.newScript.expect = ''
this.newScript.send = ''
this.newScript.isRegex = false
this.newScript.optional = false
}
}