kermit/cisco-ios-connect
changeset 15 c6e7cfbfb264
equal deleted inserted replaced
14:1de92b1b5cf3 15:c6e7cfbfb264
       
     1 #!/usr/bin/env -S kermit +
       
     2 #
       
     3 # Author: Tomas Zeman <tzeman@volny.cz>
       
     4 # This script belongs to public domain. Use it as you wish.
       
     5 #
       
     6 # Connects to a Cisco box via SSH and displays IOS version and running config.
       
     7 # Handles --More-- prompt accordingly.
       
     8 # Command session is printed to stdout, error and trace messages to stderr.
       
     9 #
       
    10 # Invocation:
       
    11 # 	./connect <<host>>
       
    12 #
       
    13 # Environment:
       
    14 # 	SSH_USER - ssh user
       
    15 # 	SSH_PASS - password
       
    16 # 	ENA_PASS - enable password
       
    17 #
       
    18 
       
    19 define waitfor {
       
    20 	while true {
       
    21 		minput \%1 {\%2} {--More--}
       
    22 		if fail die_error \%3 \%4
       
    23 		reinput 0 {--More--}
       
    24 		if ok {
       
    25 			output { }
       
    26 		} else {
       
    27 			reinput 0 {\%2}
       
    28 			if ok break
       
    29 			reinput 0 {\%2 }
       
    30 			if ok break
       
    31 		}
       
    32 		clear input
       
    33 	}
       
    34 }
       
    35 
       
    36 define log_trace {
       
    37 	write error TRACE: \%1\{10}
       
    38 }
       
    39 
       
    40 define die_error {
       
    41 	write error ERROR: \%2\{10}
       
    42 	exit \%1
       
    43 }
       
    44 
       
    45 define cmd {
       
    46 	log_trace {Executing command: \%4}
       
    47 	lineout \%4
       
    48 	waitfor \%1 \%2 \%3 \%4
       
    49 }
       
    50 
       
    51 # globals
       
    52 set network directory ""
       
    53 set exit on-disconnect on
       
    54 set exit warning off
       
    55 set input case ignore
       
    56 
       
    57 log_trace {Connecting to \%1}
       
    58 set host /pty ssh -e none -o 'StrictHostKeyChecking no' -l \$(SSH_USER) \%1
       
    59 if fail die_error 1 {Connection to \%1}
       
    60 
       
    61 log_trace {Waiting for password}
       
    62 waitfor 5 {assword: } 2 {Password timeout}
       
    63 lineout \$(SSH_PASS)
       
    64 log_trace {Waiting for command prompt}
       
    65 waitfor 5 {>} 2 {Command prompt}
       
    66 
       
    67 lineout enable
       
    68 waitfor 5 {assword: } 2 {Enable password timeout}
       
    69 lineout \$(ENA_PASS)
       
    70 log_trace {Waiting for command prompt (enable mode)}
       
    71 waitfor 5 {\#} 2 {Command prompt}
       
    72 
       
    73 local declare \&a
       
    74 .wc := \fsplit(\v(input), &a, \{10}) 
       
    75 .prompt := \&a[\&a[0]]
       
    76 
       
    77 cmd 10 \m(prompt) 3 {show version}
       
    78 cmd 20 \m(prompt) 3 {show run}
       
    79 
       
    80 pause 1
       
    81 lineout exit
       
    82 log_trace {Exiting & disconnecting}
       
    83 input 3 {\{10}}
       
    84 log_trace {Done}
       
    85 exit