a62eead9447fcd4f9d1a2b8ec2daf9a95861091b
[ossec-hids.git] / src / agentlessd / scripts / ssh_asa-fwsmconfig_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 -c des $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 # Going into enable mode.
155 send "enable\r"
156 expect {
157     "Password:" {
158         send "$addpass\r"
159
160         expect {
161             "*asswor*" {
162                 send_user "ERROR: Incorrect enable password to remote host: $hostname .\n"
163                 exit 1;
164             }
165             "*rror in authenticatio*" {
166                 send_user "ERROR: Incorrect enable password to remote host: $hostname .\n"
167                 exit 1;
168             }
169             timeout {
170                 send_user "ERROR: Timeout while going to enable mode on host: $hostname .\n"
171                 exit 1;
172             }
173             "*#" {
174                 send_user "ok on enable pass\n"
175             }
176         }
177     }
178     timeout {
179         send_user "ERROR: Timeout while running enable on host: $hostname .\n"
180         exit 1;
181     }
182 }
183
184
185
186 # Sending commands
187 set timeout 60 
188 send_user "\nSTORE: now\n"
189
190 send "term pager 0\r"
191
192 # Excluding uptime from the output
193 send "show version | grep -v Configuration last| up\r"
194 send "show running-config\r"
195 send "$commands\r"
196 send "exit\r"
197
198 expect {
199     timeout {
200         send_user "ERROR: Timeout while running commands on host: $hostname .\n"
201         exit 1;
202     }
203     eof {
204         send_user "\nINFO: Finished.\n"
205         exit 0;
206     }
207 }
208
209 send_user "ERROR: Unable to finish properly.\n"
210 exit 1;