kermit/cisco-ios-connect
author Tomas Zeman <tzeman@volny.cz>
Tue, 09 Jan 2018 12:28:15 +0100
changeset 52 c0d94e64d89a
parent 15 c6e7cfbfb264
permissions -rwxr-xr-x
window-title: set terminal title

#!/usr/bin/env -S kermit +
#
# Author: Tomas Zeman <tzeman@volny.cz>
# This script belongs to public domain. Use it as you wish.
#
# Connects to a Cisco box via SSH and displays IOS version and running config.
# Handles --More-- prompt accordingly.
# Command session is printed to stdout, error and trace messages to stderr.
#
# Invocation:
# 	./connect <<host>>
#
# Environment:
# 	SSH_USER - ssh user
# 	SSH_PASS - password
# 	ENA_PASS - enable password
#

define waitfor {
	while true {
		minput \%1 {\%2} {--More--}
		if fail die_error \%3 \%4
		reinput 0 {--More--}
		if ok {
			output { }
		} else {
			reinput 0 {\%2}
			if ok break
			reinput 0 {\%2 }
			if ok break
		}
		clear input
	}
}

define log_trace {
	write error TRACE: \%1\{10}
}

define die_error {
	write error ERROR: \%2\{10}
	exit \%1
}

define cmd {
	log_trace {Executing command: \%4}
	lineout \%4
	waitfor \%1 \%2 \%3 \%4
}

# globals
set network directory ""
set exit on-disconnect on
set exit warning off
set input case ignore

log_trace {Connecting to \%1}
set host /pty ssh -e none -o 'StrictHostKeyChecking no' -l \$(SSH_USER) \%1
if fail die_error 1 {Connection to \%1}

log_trace {Waiting for password}
waitfor 5 {assword: } 2 {Password timeout}
lineout \$(SSH_PASS)
log_trace {Waiting for command prompt}
waitfor 5 {>} 2 {Command prompt}

lineout enable
waitfor 5 {assword: } 2 {Enable password timeout}
lineout \$(ENA_PASS)
log_trace {Waiting for command prompt (enable mode)}
waitfor 5 {\#} 2 {Command prompt}

local declare \&a
.wc := \fsplit(\v(input), &a, \{10}) 
.prompt := \&a[\&a[0]]

cmd 10 \m(prompt) 3 {show version}
cmd 20 \m(prompt) 3 {show run}

pause 1
lineout exit
log_trace {Exiting & disconnecting}
input 3 {\{10}}
log_trace {Done}
exit