Skip to content

Commit 405bb7d

Browse files
author
Marcin Przepiorowski
committed
Merge branch 'issue#100' into develop
2 parents 84c1b5f + a370bf3 commit 405bb7d

File tree

4 files changed

+220
-13
lines changed

4 files changed

+220
-13
lines changed

bin/dx_connection_check.pl

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
#
14+
# Copyright (c) 2019 by Delphix. All rights reserved.
15+
#
16+
# Program Name : dx_connection_check.pl
17+
# Description : Check if port is open
18+
# Author : Marcin Przepiorowski
19+
# Created : Dec 2019
20+
#
21+
#
22+
23+
use strict;
24+
use warnings;
25+
use JSON;
26+
use Getopt::Long qw(:config no_ignore_case no_auto_abbrev); #avoids conflicts with ex host and help
27+
use File::Basename;
28+
use Pod::Usage;
29+
use FindBin;
30+
use Data::Dumper;
31+
32+
my $abspath = $FindBin::Bin;
33+
34+
use lib '../lib';
35+
use Engine;
36+
use Formater;
37+
use Toolkit_helpers;
38+
use Host_obj;
39+
use Action_obj;
40+
41+
my $version = $Toolkit_helpers::version;
42+
43+
GetOptions(
44+
'help|?' => \(my $help),
45+
'd|engine=s' => \(my $dx_host),
46+
'hostip=s' => \(my $hostip),
47+
'port=i' => \(my $port),
48+
'debug:i' => \(my $debug),
49+
'dever=s' => \(my $dever),
50+
'all' => (\my $all),
51+
'version' => \(my $print_version),
52+
'configfile|c=s' => \(my $config_file)
53+
) or pod2usage(-verbose => 1, -input=>\*DATA);
54+
55+
pod2usage(-verbose => 2, -input=>\*DATA) && exit if $help;
56+
die "$version\n" if $print_version;
57+
58+
my $engine_obj = new Engine ($dever, $debug);
59+
$engine_obj->load_config($config_file);
60+
61+
if (defined($all) && defined($dx_host)) {
62+
print "Option all (-all) and engine (-d|engine) are mutually exclusive \n";
63+
pod2usage(-verbose => 1, -input=>\*DATA);
64+
exit (1);
65+
}
66+
67+
68+
if (! (defined($hostip) && defined($port) ) ) {
69+
print "Option hostip and port are required. \n";
70+
pod2usage(-verbose => 1, -input=>\*DATA);
71+
exit (1);
72+
}
73+
74+
# this array will have all engines to go through (if -d is specified it will be only one engine)
75+
my $engine_list = Toolkit_helpers::get_engine_list($all, $dx_host, $engine_obj);
76+
77+
78+
my %restore_state;
79+
80+
my $ret = 0;
81+
82+
83+
for my $engine ( sort (@{$engine_list}) ) {
84+
# main loop for all work
85+
if ($engine_obj->dlpx_connect($engine)) {
86+
print "Can't connect to Dephix Engine $dx_host\n\n";
87+
$ret = $ret + 1;
88+
next;
89+
};
90+
91+
my ($connfault, $reason) = $engine_obj->checkSSHconnectivity("dummy", "password", $hostip, $port);
92+
93+
if (version->parse($engine_obj->getApi()) >= version->parse(1.10.0)) {
94+
if ($connfault == 0) {
95+
print "Connection using SSH with dummy user and password is sucessful\n";
96+
next;
97+
}
98+
my $out = $reason->{"error"}->{"details"};
99+
if ($out =~ m/An error occurred when attempting/) {
100+
print "Connection to $hostip:$port refused - port closed\n";
101+
$ret = $ret + 1;
102+
} elsif ($out =~ m/Could not log into/) {
103+
print "Connection to $hostip:$port sucessful - port seems to be opened\n";
104+
} else {
105+
print "Engine responded with unknown state. Please check below:\n";
106+
print Dumper $reason->{"details"};
107+
print Dumper $reason->{"commandOutput"};
108+
$ret = $ret + 1;
109+
}
110+
111+
}
112+
113+
}
114+
115+
exit $ret;
116+
117+
118+
__DATA__
119+
120+
=head1 SYNOPSIS
121+
122+
dx_connection_check [ -engine|d <delphix identifier> | -all ] [ -configfile file ] -hostip IP/FQDN -port portno [ --help|? ] [ -debug ]
123+
124+
=head1 DESCRIPTION
125+
126+
Check connectivity between Delphix Engine and host using a specified port
127+
128+
=head1 ARGUMENTS
129+
130+
Delphix Engine selection - if not specified a default host(s) from dxtools.conf will be used.
131+
132+
=over 10
133+
134+
=item B<-engine|d>
135+
Specify Delphix Engine name from dxtools.conf file
136+
137+
=item B<-all>
138+
Run script on all engines
139+
140+
=item B<-hostip IP/FQDN>
141+
Host IP or FQDN
142+
143+
=item B<-port portno>
144+
Port to check
145+
146+
147+
=back
148+
149+
=head1 OPTIONS
150+
151+
=over 2
152+
153+
=item B<-help>
154+
Print this screen
155+
156+
=item B<-debug>
157+
Turn on debugging
158+
159+
=back
160+
161+
=head1 EXAMPLES
162+
163+
Check if listener port is open
164+
165+
dx_connection_check -d 53 -hostip 192.168.1.20 -port 1521
166+
Connection to 192.168.1.20:1521 refused - port closed
167+
168+
Check if ssh port is open
169+
170+
dx_connection_check -d 53 -hostip 192.168.1.20 -port 22
171+
Connection to 192.168.1.20:22 sucessful - port seems to be opened
172+
173+
=cut

bin/dx_set_envpass.pl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313
#
14-
# Copyright (c) 2016 by Delphix. All rights reserved.
14+
# Copyright (c) 2016,2019 by Delphix. All rights reserved.
1515
#
1616
# Program Name : dx_set_envpass.pl
1717
# Description : Set password for OS Delphix user or DB Delphix user
@@ -119,6 +119,7 @@
119119
my $hostref = $environments->getHost($envitem);
120120

121121
my $connfault = 0;
122+
my $result;
122123

123124
if ( $environments->getType($envitem) ne 'WindowsHostEnvironment' ) {
124125

@@ -131,18 +132,20 @@
131132
for my $clunode ( @{$cluhosts} ) {
132133

133134
my $nodeaddr = $hosts->getHostAddr($clunode->{host});
134-
$connfault = $connfault + $engine_obj->checkSSHconnectivity($username, $password, $nodeaddr);
135+
my $port = $hosts->getHostPort($clunode->{host});
136+
($connfault, $result) = $connfault + $engine_obj->checkSSHconnectivity($username, $password, $nodeaddr, $port);
135137

136138
}
137139
} else {
138140
my $nodeaddr = $hosts->getHostAddr($hostref);
139-
$connfault = $engine_obj->checkSSHconnectivity($username, $password, $nodeaddr);
141+
my $port = $hosts->getHostPort($hostref);
142+
($connfault, $result) = $engine_obj->checkSSHconnectivity($username, $password, $nodeaddr, $port);
140143
}
141144

142145
} else {
143146
if ( $hostref ne 'CLUSTER' ) {
144147
my $nodeaddr = $hosts->getHostAddr($hostref);
145-
$connfault = $engine_obj->checkConnectorconnectivity($username, $password, $nodeaddr);
148+
($connfault, $result) = $engine_obj->checkConnectorconnectivity($username, $password, $nodeaddr);
146149
}
147150

148151

lib/Engine.pm

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -856,20 +856,29 @@ sub getTime {
856856

857857
# Procedure checkSSHconnectivity
858858
# parameters:
859-
# - minus - date current date minus minus minutes
860-
# return timezone of Delphix engine
859+
# -username - user name to check
860+
# -password - password
861+
# -hostip - ip of host to check
862+
# -port - port of host to check
863+
# return array (0 if OK, 1 if not OK, reason)
861864

862865
sub checkSSHconnectivity {
863866
my $self = shift;
864867
my $username = shift;
865868
my $password = shift;
866869
my $host = shift;
870+
my $port = shift;
867871

868872
logger($self->{_debug}, "Entering Engine::checkSSHconnectivity",1);
869873

874+
if (!defined($port)) {
875+
$port = 22;
876+
}
877+
870878
my %conn_hash = (
871879
"type" => "SSHConnectivity",
872880
"address" => $host,
881+
"port" => $port,
873882
"credentials" => {
874883
"type" => "PasswordCredential",
875884
"password" => $password
@@ -890,8 +899,7 @@ sub checkSSHconnectivity {
890899
} else {
891900
$ret = 0;
892901
}
893-
894-
return $ret;
902+
return ($ret, $result);
895903
}
896904

897905
# Procedure checkConnectorconnectivity

lib/Host_obj.pm

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#
1+
#
22
# Licensed under the Apache License, Version 2.0 (the "License");
33
# you may not use this file except in compliance with the License.
44
# You may obtain a copy of the License at
5-
#
5+
#
66
# http://www.apache.org/licenses/LICENSE-2.0
7-
#
7+
#
88
# Unless required by applicable law or agreed to in writing, software
99
# distributed under the License is distributed on an "AS IS" BASIS,
1010
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -67,9 +67,9 @@ sub getAllHosts {
6767
logger($self->{_debug}, "Entering Host_obj::getAllHosts",1);
6868

6969
my $hosts = $self->{_hosts};
70-
70+
7171
return sort (keys %{$hosts});
72-
72+
7373
}
7474

7575
# Procedure getHost
@@ -146,6 +146,29 @@ sub getHostAddr {
146146
return $ret;
147147
}
148148

149+
150+
# Procedure getHostPort
151+
# parameters:
152+
# - reference - reference of host
153+
# Return host hash for specific host reference
154+
155+
sub getHostPort {
156+
my $self = shift;
157+
my $reference = shift;
158+
159+
logger($self->{_debug}, "Entering Host_obj::getHostPort",1);
160+
161+
my $hosts = $self->{_hosts};
162+
my $ret;
163+
164+
if (defined($reference) && defined($hosts->{$reference}) ) {
165+
$ret = $hosts->{$reference}->{sshPort};
166+
} else {
167+
$ret = 'NA';
168+
}
169+
return $ret;
170+
}
171+
149172
# Procedure getTimezone
150173
# parameters:
151174
# - reference - reference of host

0 commit comments

Comments
 (0)