|
| 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 |
0 commit comments