-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_host_info_query.pl
291 lines (232 loc) · 10.1 KB
/
remote_host_info_query.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/perl
#
# --- [remote_server_info_query] Perl Script ---
#
# Author(s): Ryan Irujo
# Inception: 01.14.2013
# Last Modified: 01.17.2013
#
# Description: Script that queries the IP Address and OS Type on a set of Servers (read from a text file).
# Add of all of the Servers you would like to query into a single column in a file named 'servers.txt'.
# You can rename the text file by specifying a different name in the '$server_list' variable below.
# The Script will prompt for a username and password to use for logging into the remote Servers
# as soon as you run it.
#
#
# Changes: 1.15.2012 - [R. Irujo]
# - Added ReadKey Module in order to hide username and password credentials passed to the script
# from the terminal.
#
#
# Syntax: ./remote_server_info_query
#
# Command Line: ./remote_server_info_query
use warnings;
use Expect;
use Term::ReadKey;
#--------------------
# Main Variables
#--------------------
my $ssh = "/usr/bin/ssh";
my $server_list = "servers.txt";
my $query_file = "ip_os_query.txt";
my $results_file = "ip_os_results.txt";
my $timeout = 10;
#----------------------------------------------------------------
# Gathering Credentials used to login to the Remote Servers.
#----------------------------------------------------------------
ReadMode('noecho');
print "Please provide a username:\n";
chomp(my $username = <STDIN>);
print "Please provide a password:\n";
chomp(my $password = <STDIN>);
ReadMode('normal');
#--------------------------------------------
# Making sure Credentials are not empty.
#--------------------------------------------
if (!defined $username || $username eq ""){
print "\nA [Username] must be provided.\n";exit 2;
}
if (!defined $password || $password eq ""){
print "\nA [Password] must be provided.\n";exit 2;
}
#--------------------------------------
# Adding/Recreating the Query File
#--------------------------------------
if (-e $query_file) {
system("rm ./$query_file");
if ($? == 0) {
print "\n$query_file found. Deleting it.\n";
system("touch ./$query_file");
if ($? == 0) {
print "New $query_file created successfully.\n";
}
elsif ($? != 0) {
print "There was a problem creating the $query_file\n";
exit 2;
}
}
}
else {
system("touch ./$query_file");
if ($? == 0) {
print "\nNew $query_file created successfully.\n";
}
elsif ($? != 0) {
print "There was a problem creating the $query_file.\n";
exit 2;
}
}
#----------------------------------------
# Adding/Recreating the Results File
#----------------------------------------
if (-e $results_file) {
system("rm ./$results_file");
if ($? == 0) {
print "\n$results_file found. Deleting it.\n";
system("touch ./$results_file");
if ($? == 0) {
print "New $results_file created successfully.\n\n";
}
elsif ($? != 0) {
print "There was a problem creating the $results_file\n";
exit 2;
}
}
}
else {
system("touch ./$results_file");
if ($? == 0) {
print "New $results_file created successfully.\n\n";
}
elsif ($? != 0) {
print "There was a problem creating the $results_file.\n";
exit 2;
}
}
#----------------------------------------------------------------------------
# Verifying Server List is Available and User is ready to Execute Script
#----------------------------------------------------------------------------
my $choice = &verify_ready_to_run();
if ($choice eq "yes") {
print "Starting NOW!\n";
}
if ($choice eq "no") {
print "Exiting Script...\n";
exit 2;
}
#----------------------------------------------
# Remote Server OS Type & IP Address Check
#----------------------------------------------
foreach $server (`cat ./$server_list`) {
chomp($server);
&ip_os_remote_query ($username, $password, $server, $timeout);
}
# Parsing out relevant entries in the Query File and formatting them in readable format in the Results File.
my $final_results = system("cat ./$query_file | grep '>' | cut -d'>' -f2 | cut -c 2-300 > ./$results_file");
# Verifying that the entries in the Query File were transferred to the Results File and then exiting the script.
my $results_check = `cat ./$results_file | wc -l`;
if ($results_check < 1 ) {
warn "\nThere were NO Results written to the [ip_os_results.txt] file.\n";
exit 0;
}
elsif ($results_check >= 1 ) {
warn "\nFinal Results are available in the [ip_os_results.txt] file.\n";
exit 2;
}
#-----------------
# Subroutines
#-----------------
sub verify_ready_to_run () {
if (-e $server_list) {
system("cat ./$server_list\n");
print "\nThe Script will now query the Servers listed above. Do you want to continue? [yes/no]\n";
chomp(my $choice = <STDIN>);
if ($choice eq "yes") {
return $choice;
}
if ($choice eq "no") {
return $choice;
}
while ($choice ne "yes" && $choice ne "no") {
print "Please type in either 'yes' or 'no':\n";
chomp(my $choice = <STDIN>);
if ($choice eq "yes") {
return $choice;
}
if ($choice eq "no") {
return $choice;
}
}
}
else {
print "Unable to run as the file name listed in the [\$server_list] variable doesn't appear to exist.\n";
exit 2;
}
}
sub ip_os_remote_query (){
my $username = shift;
my $password = shift;
my $server = shift;
my $timeout = shift;
my $prompt = '\$\s*';
# Creating new Expect Instance.
my $stats_exp = new Expect;
# This sets Expect to not be so verbose on the output to the terminal. You can comment this
# out if you want to see all of the raw output.
$stats_exp->raw_pty(1);
# Enabling STDOUT Logging which will redirect the results of the script to the Query File.
# for additional parsing later.
$stats_exp->log_stdout(1);
$stats_exp->log_file($query_file);
# Spawning SSH Session to Remote Server.
$stats_exp->spawn("$ssh $username\@$server") or die "Cannot spawn ssh: $!\n";
# Running Expect Function.
$stats_exp->expect($timeout,
# Adding SSH Key if Prompted.
[qr'\(yes/no\)\s*' , sub {my $exph = shift;
print $exph "yes\n";
exp_continue; }],
# SSH Password Prompt.
[qr'word:\s*' , sub {my $action = shift;
$action->send("$password\n");
exp_continue; }],
# Query the OS Type and IP Address of the Remote Server.
[qr'login:\s*' , sub {my $action = shift;
my $os_type = "`cat /etc/redhat-release`";
my $ip_addr = "`/sbin/ifconfig | perl -nle '/dr:(\\S+)/ && print \$1' | grep -v 127.0.0.1`";
my $fqdn = "`hostname`";
my $arch = "`uname -i`";
$action->send("printf \"$fqdn;$os_type;$arch;$ip_addr\n \" ");
exp_continue; }],
# Query the OS Type and IP Address of the Remote Server.
# This option is used if the user account running the script has never logged into the Server before.
[qr'Creating directory\s*' , sub {my $action = shift;
my $os_type = "`cat /etc/redhat-release`";
my $ip_addr = "`/sbin/ifconfig | perl -nle '/dr:(\\S+)/ && print \$1' | grep -v 127.0.0.1`";
my $fqdn = "`hostname`";
my $arch = "`uname -i`";
$action->send("printf \"$fqdn;$os_type;$arch;$ip_addr\n \" ");
exp_continue; }],
# Exit out of the Remote Server.
[$prompt , sub {my $action = shift;
$action->send("exit\n");
exp_continue; }],
# Send Notification that the Query was Successful.
[qr'logout\s*' => sub {warn "Stats Successfully Retrieved for $server!\n";
exp_continue; }],
# Exception Handling subroutines are below. All Errors are treated with 'warn' instead of 'die' to ensure that
# the Script keeps running for the the rest of the Servers from the '$server_list' variable.
[qr'Connection closed by remote host' => sub {warn "SSH Session Forcefully Closed by $server.\n";}],
[qr'incident will be reported' => sub {warn "$username does not have sudo rights on $server.\n";}],
[qr'IOError\s*' => sub {warn "Something went wrong while querying $server.\n";}],
[qr'OSError\s*' => sub {warn "Something went wrong while querying $server.\n";}],
[EOF => sub {warn "Error: Could not login!\n"; }],
[timeout => sub {warn "Error: Could not login!\n"; }],
$prompt,);
# End Expect Function
$stats_exp->soft_close();
}
#------------------
# END of Script
#------------------