2017-11-27 15:30:59 +00:00
|
|
|
import { BaseSession } from 'terminus-terminal'
|
|
|
|
|
2018-08-27 04:24:12 +00:00
|
|
|
export interface LoginScript {
|
|
|
|
expect?: string
|
|
|
|
send: string
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:30:59 +00:00
|
|
|
export interface SSHConnection {
|
|
|
|
name?: string
|
|
|
|
host: string
|
2018-01-04 20:13:46 +00:00
|
|
|
port: number
|
2017-11-27 15:30:59 +00:00
|
|
|
user: string
|
|
|
|
password?: string
|
|
|
|
privateKey?: string
|
2018-09-04 20:39:00 +00:00
|
|
|
group?: string
|
2018-08-27 04:24:12 +00:00
|
|
|
scripts?: LoginScript[]
|
2018-09-12 02:39:18 +00:00
|
|
|
keepaliveInterval?: number
|
|
|
|
keepaliveCountMax?: number
|
|
|
|
readyTimeout?: number
|
2017-11-27 15:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class SSHSession extends BaseSession {
|
2018-08-27 04:24:12 +00:00
|
|
|
scripts?: LoginScript[]
|
|
|
|
|
|
|
|
constructor (private shell: any, conn: SSHConnection) {
|
2017-11-27 15:30:59 +00:00
|
|
|
super()
|
2018-09-04 20:49:12 +00:00
|
|
|
this.scripts = conn.scripts || []
|
2018-08-02 21:22:36 +00:00
|
|
|
}
|
2017-11-27 15:30:59 +00:00
|
|
|
|
2018-08-02 21:22:36 +00:00
|
|
|
start () {
|
2017-11-27 15:30:59 +00:00
|
|
|
this.open = true
|
|
|
|
|
|
|
|
this.shell.on('data', data => {
|
2018-08-27 04:24:12 +00:00
|
|
|
let dataString = data.toString()
|
|
|
|
this.emitOutput(dataString)
|
|
|
|
|
2018-09-04 20:49:12 +00:00
|
|
|
if (this.scripts) {
|
2018-08-27 04:24:12 +00:00
|
|
|
let found = false
|
2018-09-04 20:49:12 +00:00
|
|
|
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)
|
2018-08-27 04:24:12 +00:00
|
|
|
found = true
|
2018-09-04 20:49:12 +00:00
|
|
|
} else {
|
|
|
|
break
|
2018-08-27 04:24:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 09:02:02 +00:00
|
|
|
if (found) {
|
2018-09-04 20:49:12 +00:00
|
|
|
this.executeUnconditionalScripts()
|
2018-08-27 04:24:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-27 15:30:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
this.shell.on('end', () => {
|
|
|
|
if (this.open) {
|
|
|
|
this.destroy()
|
|
|
|
}
|
|
|
|
})
|
2018-08-27 04:24:12 +00:00
|
|
|
|
2018-09-04 20:49:12 +00:00
|
|
|
this.executeUnconditionalScripts()
|
2017-11-27 15:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resize (columns, rows) {
|
|
|
|
this.shell.setWindow(rows, columns)
|
|
|
|
}
|
|
|
|
|
|
|
|
write (data) {
|
|
|
|
this.shell.write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
kill (signal?: string) {
|
|
|
|
this.shell.signal(signal || 'TERM')
|
|
|
|
}
|
|
|
|
|
|
|
|
async getChildProcesses (): Promise<any[]> {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
async gracefullyKillProcess (): Promise<void> {
|
|
|
|
this.kill('TERM')
|
|
|
|
}
|
|
|
|
|
|
|
|
async getWorkingDirectory (): Promise<string> {
|
|
|
|
return null
|
|
|
|
}
|
2018-09-04 20:49:12 +00:00
|
|
|
|
|
|
|
private executeUnconditionalScripts () {
|
|
|
|
if (this.scripts) {
|
|
|
|
for (let script of this.scripts) {
|
|
|
|
if (!script.expect) {
|
|
|
|
console.log('Executing script:', script.send)
|
|
|
|
this.shell.write(script.send + '\n')
|
|
|
|
this.scripts = this.scripts.filter(x => x !== script)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-27 15:30:59 +00:00
|
|
|
}
|
2018-09-04 20:39:00 +00:00
|
|
|
|
|
|
|
export interface ISSHConnectionGroup {
|
|
|
|
name: string
|
|
|
|
connections: SSHConnection[]
|
|
|
|
}
|