-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagw_checker.exp
100 lines (80 loc) · 3.28 KB
/
agw_checker.exp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Go through a list of Cisco devices and send some commands.
# Telnet version
# v 0.3, 28.02.2014 PSV
#
# Requirements:
# 1. Create a file called "devices.txt" (or change the name below) with a list of your devices (one per line).
# 2. Set your login/password/enable variables below.
# Output:
# Parse you results from "results.txt" (or change the name below).
package require Expect
# Set variables
set username USERNAME
set password PASSWORD
set enablepassword ENA_PASSWORD
set device_list "devices.txt"
set results_file "results.txt"
set logfile "output.log"
# Tune timeout value if have connection problems/timeouts
set timeout 15
set send_slow {1 .01}
# Turn off screen logging = 0
# log_user 0
# Log output
# log_file -a $logfile # -a is for append
log_file $logfile
# Log results - create or truncate a file
# more i/o in main loop
set results [open $results_file w]
# Read devices' hostnames/IP addresses from file
set file_channel [open $device_list r]
set devices [read $file_channel]
close $file_channel
set device_entries [split $devices "\n"]
# Main loop
foreach device $device_entries {
if {[string length $device] == 0 || [regexp {[ \t]*#} $device]} { continue }
set hostname [lindex $device 0]
# Show which device we are working on and at what time
set results [open $results_file a+]
set date [clock format [clock seconds] -format {%Y-%b-%d %H:%M:%S}]
send_user "\n"
send_user ">>>>> Working on $hostname \[$date\] <<<<<\n"
puts $results "\n >>>>> Working on $hostname \[$date\] <<<<<\n"
send_user "\n"
# Spawn telnet session to target device
spawn telnet $hostname
# Handle connection issues or log in
expect {
timeout { send_user "\nTimeout Exceeded - Check Host\n"; puts $results "\n ** Connection to ($hostname) failed! Timeout! **\n"; close $results; continue }
eof { send_user "\nConnection To $hostname Failed\n"; puts $results "\n ** Connection to ($hostname) failed! **\n"; close $results; continue }
"*#" {}
"Username:" { send "$username\r"; expect "*assword:"; send "$password\r"}
}
# Get into enable mode
expect {
default { send_user "\nEnable Mode Failed - Check Password\n"; puts $results "\n ** Enable mode on ($hostname) failed! Check password! **\n"; continue }
"*#" {}
"*>" {
send "enable\r"
expect "*assword"
send "$enablepassword\r"
expect -re {([^\s]*)#} {
# the complete re is in :
# puts out0:$expect_out(0,string)
# the subexpression () is in:
set hostshort $expect_out(1,string)
}
}
}
# List of commands that are send to the device.
# Originally this project was done to check if there no login issues with hundreds of devices in customer's network.
# It tries to log in to the device, enter enable mode, send "sh clock" command and logout. Tune to your needs.
send "sh clock\r"
expect "*#"
send "quit \r"
send_user "\n\n ##### Telnet on $hostshort ($hostname) works OK! #####\n\n"
puts $results "\n ##### Telnet on $hostshort ($hostname) works OK! #####\n"
close $results
}
exit