-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.pl
executable file
·80 lines (63 loc) · 2.12 KB
/
example.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
#!/usr/bin/perl -w
use strict;
use warnings;
use FMC;
use Net::DNS::Resolver;
my $fmc = FMC->new();
# Connect to the FMC.
$fmc->connect(url => 'https://172.29.0.43',
credentials => 'api:Api123321',
verbose => 1);
# Get network group called "InternalNetworks".
# This will return a hash containing name, ID and values (as an array).
my $group = $fmc->get_networkgroup("TestGroup");
# Show values before any changes.
print "\n*** Network group before:\n";
foreach my $value (@{$group->{values}})
{
print "\t$value->{value} [$value->{type}]\n";
}
print "\n";
# Resolve FQDN using the two DNS servers specified.
# DNS servers could get different replies because of GeoDNS.
my %ips = get_ips_from_fqdn("dev-prod05.conferdeploy.net", ('192.168.228.27','172.16.68.53'));
#my %ips = get_ips_from_fqdn("one.one.one.one", ('192.168.228.27','172.16.68.53'));
my @new = ();
foreach my $ip (keys %ips)
{
push @new, {type => 'Host', value => $ips{$ip}};
}
# Update the network group with the values prepared above.
# This will perform a merge, e.g. take existing values and add in any new values.
$fmc->update_networkgroup($group->{id}, \@new, 1);
# Get network group values again (post-update).
$group = $fmc->get_networkgroup($group->{id});
# Show the new values of the network group.
print "\n*** Network group after:\n";
foreach my $value (@{$group->{values}})
{
print "\t$value->{value} [$value->{type}]\n";
}
print "\n";
# Parse in an FQDN and n number of DNS servers.
# Returns unified list of A records for FQDN from the DNS servers.
sub get_ips_from_fqdn
{
my $fqdn = shift;
my @dnsservers = @_;
my $resolver = Net::DNS::Resolver->new(udp_timeout => 3);
my %ips;
foreach my $dnsserver (@dnsservers)
{
$resolver->nameservers($dnsserver);
my $packet = $resolver->query($fqdn, 'A');
if (defined($packet))
{
foreach my $ip (map { $_->address } grep { $_->type eq 'A' } $packet->answer)
{
$ips{$ip} = $ip;
}
}
}
return %ips;
}