-
Notifications
You must be signed in to change notification settings - Fork 72
/
yaptest-hosts.pl
executable file
·80 lines (74 loc) · 1.85 KB
/
yaptest-hosts.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/env perl
use strict;
use warnings;
use yaptest;
use Getopt::Long;
use Data::Dumper;
use File::Basename;
my $y = yaptest->new;
my $file;
my $test_area;
my @ips;
my $script_name = basename($0);
my $usage = "Usage: $script_name add ( ip [ ip ... ] | -f ips.txt)
$script_name query [ -i ip ] [ -t test_area ]
$script_name export out.xml [ -i ip ] [ -t test_area ]
$script_name delete ip [ ip ] ...
Adds, removed and queries IPs in the database.
";
GetOptions (
"file=s" => \$file,
"test_area=s" => \$test_area,
"ip=s" => \@ips,
);
my $command = shift or die $usage;
if ($command eq "add") {
if ($file) {
open FILE, "<$file" or die "ERROR: Can't open $file for reading: $!\n";
while (<FILE>) {
chomp;
my $ip = $_;
#test that the ip looks like an ip
if($ip =~ m/^\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?$/)
{
print "Inserting $ip\n";
$y->insert_ip($ip);
}
else
{
print "NOTE: Not inserting $ip - Invalid IP address";
}
}
} else {
unless (@ARGV) {
die $usage;
}
foreach my $ip (@ARGV) {
print "Inserting $ip\n";
$y->insert_ip($ip);
}
}
} elsif ($command eq "query" or $command eq "export") {
die $usage if (scalar(@ips) > 1); # must have 0 or 1 IPs
my $aref = $y->get_hosts(test_area => $test_area, ip_address => $ips[0]);
if ($command eq "query") {
$y->print_table_hashes($aref, undef, qw(test_area_name ip_address hostname name_type));
}
if ($command eq "export") {
my $filename = shift;
die "ERROR: filename is mandatory for 'export' command\n" unless defined($filename);
$y->export_to_xml($aref, "hosts", $filename);
print "Exported query results to $filename\n";
}
} elsif ($command eq "delete") {
unless (@ARGV) {
die $usage;
}
foreach my $ip (@ARGV) {
print "Deleting $ip\n";
$y->delete_host(test_area => $test_area, ip_address => $ip);
}
} else {
die $usage;
}
$y->{dbh}->commit;