-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathVipToInternalHosts.pl
executable file
·247 lines (200 loc) · 7.67 KB
/
VipToInternalHosts.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
#!/usr/bin/perl
#----------------------------------------------------------------------------
# VipToInternalHosts.pl - script to take F5 BigIp VIP address and display
# the members of the pool it is served by.
#
# By Jason Antman <[email protected]> 2012.
#
# The latest version of this script can always be found at:
# <https://github.com/jantman/misc-scripts/blob/master/VipToInternalHosts.pl>
#
# The post describing this script and giving updates can be found at:
# <http://blog.jasonantman.com/2012/04/perl-script-to-convert-f5-bigip-vip-address-to-list-of-internal-pool-member-addresses>
#
#----------------------------------------------------------------------------
# This script was based off of one found in the F5 Networks iControl Wiki.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Alternatively, the contents of this file may be used under the terms
# of the GNU General Public License (the "GPL"), in which case the
# provisions of GPL are applicable instead of those above. If you wish
# to allow use of your version of this file only under the terms of the
# GPL and not to allow others to use your version of this file under the
# License, indicate your decision by deleting the provisions above and
# replace them with the notice and other provisions required by the GPL.
# If you do not delete the provisions above, a recipient may use your
# version of this file under either the License or the GPL.
#----------------------------------------------------------------------------
#use SOAP::Lite + trace => qw(method debug);
use SOAP::Lite;
use Getopt::Long;
use Pod::Usage;
#----------------------------------------------------------------------------
# Disable SSL Cert Validation
#----------------------------------------------------------------------------
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
#----------------------------------------------------------------------------
# Default Values
#----------------------------------------------------------------------------
my $default_port = "80"; # default port number for VIPs
#----------------------------------------------------------------------------
# Validate Arguments
#----------------------------------------------------------------------------
my $sHost;
my $sUID;
my $sPWD;
my $sVIP;
my $sHelp;
my $man;
$result = GetOptions (
"host=s" => \$sHost,
"user=s" => \$sUID,
"pass:s" => \$sPWD,
"vip=s" => \$sVIP,
"man" => \$man,
"help" => \$sHelp, "h" => \$sHelp);
pod2usage(1) if $sHelp;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;
pod2usage("$0: Host must be specified.\n") unless $sHost;
pod2usage("$0: Username must be specified.\n") unless $sUID;
pod2usage("$0: VIP address must be specified.\n") unless $sVIP;
if ( ! $sPWD ) {
print "Enter password for $sUID\@$sHost: ";
chomp($sPWD = <>);
print "\n";
}
# if no port specified, cat on default
if ( $sVIP !~ /.+:\d+/ ) {
$sVIP .= ":".$default_port;
}
#----------------------------------------------------------------------------
# Transport Information
#----------------------------------------------------------------------------
sub SOAP::Transport::HTTP::Client::get_basic_credentials
{
return "$sUID" => "$sPWD";
}
#----------------------------------------------------------------------------
# checkResponse
#----------------------------------------------------------------------------
sub checkResponse()
{
my ($soapResponse) = (@_);
if ( $soapResponse->fault )
{
print $soapResponse->faultcode, " ", $soapResponse->faultstring, "\n";
exit();
}
}
#----------------------------------------------------------------------------
# GetInterface
#----------------------------------------------------------------------------
sub GetInterface()
{
my ($name) = (@_);
my $Interface = SOAP::Lite
-> uri("urn:iControl:$name")
-> readable(1)
-> proxy("https://$sHost/iControl/iControlPortal.cgi");
#----------------------------------------------------------------------------
# Attempt to add auth headers to avoid dual-round trip
#----------------------------------------------------------------------------
eval { $Interface->transport->http_request->header
(
'Authorization' => 'Basic ' . MIME::Base64::encode("$sUID:$sPWD", '')
); };
return $Interface;
}
$Pool = &GetInterface("LocalLB/Pool");
$VirtualServer = &GetInterface("LocalLB/VirtualServer");
#----------------------------------------------------------------------------
# GetHostsFromVip
#----------------------------------------------------------------------------
sub GetHostsFromVip()
{
my @vips = (@_);
$soapResponse = $VirtualServer->get_list();
&checkResponse($soapResponse);
@vs_list = @{$soapResponse->result};
$soapResponse = $VirtualServer->get_destination(
SOAP::Data->name(virtual_servers => [@vs_list])
);
&checkResponse($soapResponse);
@dest_list = @{$soapResponse->result};
$soapResponse = $VirtualServer->get_default_pool_name(
SOAP::Data->name(virtual_servers => [@vs_list])
);
&checkResponse($soapResponse);
@pool_list = @{$soapResponse->result};
my $found = 0;
foreach $vip (@vips)
{
for $i (0 .. $#vs_list)
{
$dest = @dest_list[$i];
$addr = $dest->{"address"};
$port = $dest->{"port"};
next unless ( $vip eq "$addr:$port");
$vs = @vs_list[$i];
$p2 = @pool_list[$i];
print "VIP ${addr}:${port} ($vs) -> Pool '$p2'\n";
$found = 1; last;
}
last if $found == 1;
}
# get the members of the pool
$soapResponse = $Pool->get_member(
SOAP::Data->name(pool_names => [$p2])
);
&checkResponse($soapResponse);
@pool_list = @{$soapResponse->result};
print "Members of Pool '$p2':\n";
foreach $member (@{@pool_list[0]})
{
$addr = $member->{"address"};
$port = $member->{"port"};
print "\t$addr:$port\n";
}
}
#----------------------------------------------------------------------------
# Main logic
#----------------------------------------------------------------------------
&GetHostsFromVip($sVIP);
__END__
=head1 NAME
vipToInternalHosts.pl - F5 Big-Ip VIP address to internal pool member addresses.
=head1 SYNOPSIS
vipToInternalHosts.pl --host=hostname --user=username [--pass=password] --vip=address|ip[:port]
=head1 OPTIONS
=over 8
=item B<--host=hostname>
Specify hostname of the F5 BigIp host.
=item B<--user=username>
Specify username to use to authenticate to F5 host.
=item B<--pass=password>
Specify password to use to authenticate to F5 host. If omitted, prompt for password to be entered interactively.
=item B<--vip=[address|address:port|hostname|hostname:port]>
VIP address in the form address[:port]. If port is omitted, defaults to 80.
=back
=head1 DESCRIPTION
Take API access credentials for a F5 BigIp load balancer and a VIP address, translate to a pool name and the internal addresses of the pool members.
=head1 SAMPLE OUTPUT
> ./VipToInternalHosts.pl --host=prod-lb1.example.com --user=myname --pass=mypassword --vip=128.6.30.130:80
VIP 128.6.30.130:80 (f5_vip_name) -> Pool 'pool_name'
Members of Pool 'pool_name':
10.145.15.10:80
10.145.15.11:80
>
=cut