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