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