Imported Upstream version 2.7
[ossec-hids.git] / src / agentlessd / scripts / ssh_asa-fwsmconfig_diff
1 #!/usr/bin/env expect
2
3 # @(#) $Id: ./src/agentlessd/scripts/ssh_asa-fwsmconfig_diff, 2011/09/08 dcid Exp $
4
5 # Agentless monitoring
6 #
7 # Copyright (C) 2009 Trend Micro Inc.
8 # All rights reserved.
9
10 # This program is a free software; you can redistribute it
11 # and/or modify it under the terms of the GNU General Public
12 # License (version 2) as published by the FSF - Free Software
13 # Foundation.
14
15
16 if {$argc < 1} {
17     send_user "ERROR: ssh_asa-fwsmconfig_diff <hostname> <commands>\n";
18     send_user "ERROR: Must be run from /var/ossec\n";
19     exit 1;
20 }
21
22
23 # NOTE: this script must be called from within /var/ossec for it to work.
24 set passlist "agentless/.passlist"
25 set hostname [lindex $argv 0]
26 set commands [lrange $argv 1 end]
27 set pass "x"
28 set addpass "x"
29 set timeout 20 
30
31 if {[string compare $hostname "test"] == 0} {
32     if {[string compare $commands "test"] == 0} {
33         exit 0;
34     }
35 }
36
37 # Reading the password list.
38 if [catch {
39     set in [open "$passlist" r]
40 } loc_error] {
41     send_user "ERROR: Password list not present (use \"register_host\" first).\n"
42     exit 1;
43 }
44
45 while {[gets $in line] != -1} {
46         set me [string first "|" $line]
47         set me2 [string last "|" $line]
48         set length [string length $line]
49         
50         if {$me == -1} {
51             continue;
52         }
53         if {$me2 == -1} {
54             continue;
55         }
56         if {$me == $me2} {
57             continue;
58         }
59         
60         set me [expr $me-1]
61         set me2 [expr $me2-1]
62         
63         set host_list [string range $line 0 $me]
64         set me [expr $me+2]
65         set pass_list [string range $line $me $me2]
66         set me2 [expr $me2+2]
67         set addpass_list [string range $line $me2 $length]
68
69         if {[string compare $host_list $hostname] == 0} {
70             set pass "$pass_list"
71             set addpass "$addpass_list"
72             break
73         }
74 }
75 close $in
76
77
78 if {[string compare $pass "x"] == 0} {
79     send_user "ERROR: Password for '$hostname' not found.\n"
80     exit 1;
81 }
82
83
84 # SSHing to the box and passing the directories to check.
85 if [catch {
86     spawn ssh -c des $hostname
87 } loc_error] {
88     send_user "ERROR: Opening connection: $loc_error.\n"
89     exit 1;
90 }
91
92 expect {
93     "WARNING: REMOTE HOST" {
94         send_user "ERROR: RSA host key for '$hostname' has changed. Unable to access.\n"
95         exit 1;
96     }
97     "*sure you want to continue connecting*" {
98         send "yes\r"
99         expect "* password:*" {
100             send "$pass\r"
101
102             expect {
103                 "Permission denied" {
104                     send_user "ERROR: Incorrect password to remote host: $hostname .\n"
105                     exit 1;
106                 }
107                 timeout {
108                     send_user "ERROR: Timeout while running on host (too long to finish): $hostname .\n"
109                     exit 1;
110                 }
111                 "*>" {
112                     send_user "\nINFO: Starting.\n"
113                 }
114             }
115         }
116     }
117     "ssh: connect to host*" {
118         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
119         exit 1;
120     }
121     "no address associated with name" {
122         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
123         exit 1;
124     }
125     "*Connection refused*" {
126         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
127         exit 1;
128     }
129     "*Connection closed by remote host*" {
130         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
131         exit 1;
132     }
133     "* password:*" {
134         send "$pass\r"
135         
136         expect {
137             "Permission denied" {
138                 send_user "ERROR: Incorrect password to remote host: $hostname .\n"
139                 exit 1;
140             }
141             timeout {
142                 send_user "ERROR: Timeout while running on host (too long to finish): $hostname .\n"
143                 exit 1;
144             }
145             "*>" {
146                 send_user "INFO: Starting.\n"
147             }
148         }
149     }
150     timeout {
151         send_user "ERROR: Timeout while connecting to host: $hostname . \n"
152         exit 1;
153     }
154 }
155
156 # Going into enable mode.
157 send "enable\r"
158 expect {
159     "Password:" {
160         send "$addpass\r"
161
162         expect {
163             "*asswor*" {
164                 send_user "ERROR: Incorrect enable password to remote host: $hostname .\n"
165                 exit 1;
166             }
167             "*rror in authenticatio*" {
168                 send_user "ERROR: Incorrect enable password to remote host: $hostname .\n"
169                 exit 1;
170             }
171             timeout {
172                 send_user "ERROR: Timeout while going to enable mode on host: $hostname .\n"
173                 exit 1;
174             }
175             "*#" {
176                 send_user "ok on enable pass\n"
177             }
178         }
179     }
180     timeout {
181         send_user "ERROR: Timeout while running enable on host: $hostname .\n"
182         exit 1;
183     }
184 }
185
186
187
188 # Sending commands
189 set timeout 60 
190 send_user "\nSTORE: now\n"
191
192 send "term pager 0\r"
193
194 # Excluding uptime from the output
195 send "show version | grep -v Configuration last| up\r"
196 send "show running-config\r"
197 send "$commands\r"
198 send "exit\r"
199
200 expect {
201     timeout {
202         send_user "ERROR: Timeout while running commands on host: $hostname .\n"
203         exit 1;
204     }
205     eof {
206         send_user "\nINFO: Finished.\n"
207         exit 0;
208     }
209 }
210
211 send_user "ERROR: Unable to finish properly.\n"
212 exit 1;