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