Imported Upstream version 2.5.1
[ossec-hids.git] / src / agentlessd / scripts / ssh_foundry_diff
1 #!/usr/bin/env expect
2
3 # @(#) $Id$
4 # Agentless monitoring
5 #
6 # Copyright (C) 2009 Trend Micro Inc.
7 # All rights reserved.
8
9 # This program is a free software; you can redistribute it
10 # and/or modify it under the terms of the GNU General Public
11 # License (version 2) as published by the FSF - Free Software
12 # Foundation.
13
14
15 if {$argc < 1} {
16     send_user "ERROR: ssh_pixconfig_diff <hostname> <commands>\n";
17     exit 1;
18 }
19
20
21 # NOTE: this script must be called from within /var/ossec for it to work.
22 set passlist "agentless/.passlist"
23 set hostname [lindex $argv 0]
24 set commands [lrange $argv 1 end]
25 set pass "x"
26 set addpass "x"
27 set timeout 20 
28
29 if {[string compare $hostname "test"] == 0} {
30     if {[string compare $commands "test"] == 0} {
31         exit 0;
32     }
33 }
34
35 # Reading the password list.
36 if [catch {
37     set in [open "$passlist" r]
38 } loc_error] {
39     send_user "ERROR: Password list not present (use \"register_host\" first).\n"
40     exit 1;
41 }
42
43 while {[gets $in line] != -1} {
44         set me [string first "|" $line]
45         set me2 [string last "|" $line]
46         set length [string length $line]
47         
48         if {$me == -1} {
49             continue;
50         }
51         if {$me2 == -1} {
52             continue;
53         }
54         if {$me == $me2} {
55             continue;
56         }
57         
58         set me [expr $me-1]
59         set me2 [expr $me2-1]
60         
61         set host_list [string range $line 0 $me]
62         set me [expr $me+2]
63         set pass_list [string range $line $me $me2]
64         set me2 [expr $me2+2]
65         set addpass_list [string range $line $me2 $length]
66
67         if {[string compare $host_list $hostname] == 0} {
68             set pass "$pass_list"
69             set addpass "$addpass_list"
70             break
71         }
72 }
73 close $in
74
75
76 if {[string compare $pass "x"] == 0} {
77     send_user "ERROR: Password for '$hostname' not found.\n"
78     exit 1;
79 }
80
81
82 # SSHing to the box and passing the directories to check.
83 if [catch {
84     spawn ssh $hostname
85 } loc_error] {
86     send_user "ERROR: Opening connection: $loc_error.\n"
87     exit 1;
88 }
89
90 expect {
91     "WARNING: REMOTE HOST" {
92         send_user "ERROR: RSA host key for '$hostname' has changed. Unable to access.\n"
93         exit 1;
94     }
95     "*sure you want to continue connecting*" {
96         send "yes\r"
97         expect "* password:*" {
98             send "$pass\r"
99
100             expect {
101                 "Permission denied" {
102                     send_user "ERROR: Incorrect password to remote host: $hostname .\n"
103                     exit 1;
104                 }
105                 timeout {
106                     send_user "ERROR: Timeout while running on host (too long to finish): $hostname .\n"
107                     exit 1;
108                 }
109                 "*>" {
110                     send_user "\nINFO: Starting.\n"
111                 }
112             }
113         }
114     }
115     "ssh: connect to host*" {
116         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
117         exit 1;
118     }
119     "no address associated with name" {
120         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
121         exit 1;
122     }
123     "*Connection refused*" {
124         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
125         exit 1;
126     }
127     "*Connection closed by remote host*" {
128         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
129         exit 1;
130     }
131     "* password:*" {
132         send "$pass\r"
133         
134         expect {
135             "Permission denied" {
136                 send_user "ERROR: Incorrect password to remote host: $hostname .\n"
137                 exit 1;
138             }
139             timeout {
140                 send_user "ERROR: Timeout while running on host (too long to finish): $hostname .\n"
141                 exit 1;
142             }
143             "*>" {
144                 send_user "INFO: Starting.\n"
145             }
146         }
147     }
148     timeout {
149         send_user "ERROR: Timeout while connecting to host: $hostname . \n"
150         exit 1;
151     }
152 }
153
154
155 if {[string compare $addpass ""] != 0} {
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 - incorrect password*" {
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 "skip-page-display\r"
193
194
195 # Excluding uptime from the output
196 send "sh run\r"
197 send "$commands\r"
198 send "exit\rexit\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;