Imported Upstream version 2.7
[ossec-hids.git] / src / agentlessd / scripts / ssh_pixconfig_diff
1 #!/usr/bin/env expect
2
3 # @(#) $Id: ./src/agentlessd/scripts/ssh_pixconfig_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 -c des $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 # Going into enable mode.
156 send "enable\r"
157 expect {
158     "Password:" {
159         send "$addpass\r"
160
161         expect {
162             "*asswor*" {
163                 send_user "ERROR: Incorrect enable password to remote host: $hostname .\n"
164                 exit 1;
165             }
166             "*rror in authenticatio*" {
167                 send_user "ERROR: Incorrect enable password to remote host: $hostname .\n"
168                 exit 1;
169             }
170             timeout {
171                 send_user "ERROR: Timeout while going to enable mode on host: $hostname .\n"
172                 exit 1;
173             }
174             "*#" {
175                 send_user "ok on enable pass\n"
176             }
177         }
178     }
179     timeout {
180         send_user "ERROR: Timeout while running enable on host: $hostname .\n"
181         exit 1;
182     }
183 }
184
185
186 # Sending commands
187 set timeout 60 
188 send_user "\nSTORE: now\n"
189
190 send "no pager\r"
191 send "term len 0\r"
192 send "terminal 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;