added kermit/cisco-ios-connect: example of communication w/ Cisco IOS box
authorTomas Zeman <tzeman@volny.cz>
Tue, 16 Aug 2011 08:46:17 +0200
changeset 15 c6e7cfbfb264
parent 14 1de92b1b5cf3
child 16 6999a9e28d93
added kermit/cisco-ios-connect: example of communication w/ Cisco IOS box
kermit/cisco-ios-connect
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kermit/cisco-ios-connect	Tue Aug 16 08:46:17 2011 +0200
@@ -0,0 +1,85 @@
+#!/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