tabby/terminus-ssh/src/api.ts

108 lines
2.5 KiB
TypeScript
Raw Normal View History

import { BaseSession } from 'terminus-terminal'
2018-08-27 04:24:12 +00:00
export interface LoginScript {
expect?: string
send: string
}
export interface SSHConnection {
name?: string
host: string
port: number
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
}
export class SSHSession extends BaseSession {
2018-08-27 04:24:12 +00:00
scripts?: LoginScript[]
constructor (private shell: any, conn: SSHConnection) {
super()
2018-09-04 20:49:12 +00:00
this.scripts = conn.scripts || []
}
start () {
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
}
}
})
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()
}
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
}
}
}
}
}
2018-09-04 20:39:00 +00:00
export interface ISSHConnectionGroup {
name: string
connections: SSHConnection[]
}