Skip to content

Commit

Permalink
Improved -h output and Usage information
Browse files Browse the repository at this point in the history
Added Ping.pm patch for Linux into comments
Added default for -b to power off switch after successful load
Added switch to override power off for -b
  • Loading branch information
owendelong authored and sarcasticadmin committed Feb 4, 2025
1 parent 78c1bdd commit 1675a15
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 17 deletions.
81 changes: 69 additions & 12 deletions switch-configuration/config/scripts/Loader.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
# IPC::Open2 -- Allows simplification of Pipe opens
# -- CPAN (or Debian packages) --
# Net::Ping -- Ping without system()
# NOTE: Ping.pm either requires root privilege or a patch for Linux
# to work without it.
#
# Expect -- PERL Expect Library - simplifies communication with switches
# Term::ReadKey -- Simlifies password requests and similar
##FIXME## Net::SSH::Perl -- ssh without system()
Expand All @@ -27,6 +30,44 @@
# Net::SFTP::Foreign -- sftp without system()
# -- CPAN Only --

#
# Patch for Ping.pm to work without root privilege on linux:
#230c230
#< croak("icmp ping requires root privilege") if !_isroot();
#---
#> croak("icmp ping requires root privilege") if !_isroot() && ($^O ne "linux");
#235,236c235,244
#< socket($self->{fh}, PF_INET, SOCK_RAW, $self->{proto_num}) ||
#< croak("icmp socket error - $!");
#---
#> if ($^O eq "linux")
#> {
#> socket($self->{fh}, PF_INET, SOCK_DGRAM, $self->{proto_num}) ||
#> croak("icmp socket error - $!");
#> }
#> else
#> {
#> socket($self->{fh}, PF_INET, SOCK_RAW, $self->{proto_num}) ||
#> croak("icmp socket error - $!");
#> }
#253,254c261,270
#< socket($self->{fh}, $AF_INET6, SOCK_RAW, $self->{proto_num}) ||
#< croak("icmp socket error - $!");
#---
#> if ($^O eq "linux")
#> {
#> socket($self->{fh}, $AF_INET6, SOCK_DGRAM, $self->{proto_num}) ||
#> croak("icmp socket error - $!");
#> }
#> else
#> {
#> socket($self->{fh}, $AF_INET6, SOCK_RAW, $self->{proto_num}) ||
#> croak("icmp socket error - $!");
#> }
#
# End of patch


# Pull in dependencies
package Loader;

Expand Down Expand Up @@ -133,6 +174,7 @@ sub new
Interfaces => [ Net::Interface->interfaces() ], # List of interfaces
DefaultUser => $user,
asroot => 0,
power_off => 0, # Power off the switch at end of override_switch (default=no)
};

foreach my $if (@{$self->{"Interfaces"}})
Expand Down Expand Up @@ -557,20 +599,35 @@ sub override_switch
print STDERR "Received: ($before) ($matched) ($after)\n";
$error_count++ if ($err);
croak("Did not receive Prompt after finalizing: $err for $Name\n") if ($err);
#$JUNIPER->send("quit\n");
print $JUNIPER "quit\n";
if ($self->{'asroot'})

if ($self->{'power_off'} && $error_count == 0)
{
($pos, $err, $matched, $before, $after) = $JUNIPER->expect(10,
'% ',
);
$before =~ s/\033/<Esc>/g;
$after =~ s/\033/<Esc>/g;
$error_count++ if ($err);
croak("Did not get shell prompt ($err) for $Name after exiting CLI as root\n") if ($err);
print $JUNIPER "exit\n";
print $JUNIPER "request system power-off\n";
print STDERR "Power Off Request sent.\n";
sleep 5;
$JUNIPER->hard_close();
}
elsif ($self->{'power_off'})
{
warn "WARNING: Power Off aborted due to earlier errors!\n";
print STDERR "You may need to send the power off command to the switch manually.\n";
}
else
{
print $JUNIPER "quit\n";
if ($self->{'asroot'})
{
($pos, $err, $matched, $before, $after) = $JUNIPER->expect(10,
'% ',
);
$before =~ s/\033/<Esc>/g;
$after =~ s/\033/<Esc>/g;
$error_count++ if ($err);
croak("Did not get shell prompt ($err) for $Name after exiting CLI as root\n") if ($err);
print $JUNIPER "exit\n";
}
$JUNIPER->soft_close();
}
$JUNIPER->soft_close();
print STDERR ($error_count ? "Uns" : "S") . "uccessful completion of configuration for $Name\n";
push @messages, ($error_count ? "Uns" : "S") . "uccessful completion of configuration for $Name\n";
push @messages, "Encountered $error_count errors for $Name\n";
Expand Down
30 changes: 25 additions & 5 deletions switch-configuration/config/scripts/switch_config_loader
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
# New finctionality:
#
# command line usage:
# switch_config_loader (-h | [-b] [-c <filename>] [-l] [-n] [-t <target>] [<switch_spec>])
# switch_config_loader (-h | [-b] [-c <filename>] [-k] [-l] [-n] [-p] [-t <target>] [-u <username> ] [-x] [-z] [<switch_spec>])
#
# -b -- Run in bulk mode, loop forever trying to install on each switch
# attached. Implies -l, incompatible with -k.
# NOTE: Will execute a request system power-off at the end of the config load.
#
# -c <filename> -- Use <filename> to configure every affected switch
#
Expand All @@ -56,6 +57,8 @@
#
# -u <username> -- username to provide to swtiches for authentication
#
# -x -- Do not power off switch (Only meaningful with -b)
#
# -z -- Go ahead and process switches in the Z (unused) hierarchy
#
# <switch_spec> -- One or more switch names and/or groups
Expand Down Expand Up @@ -105,8 +108,9 @@ our $opt_n;
our $opt_p;
our $opt_t;
our $opt_u;
our $opt_x;
our $opt_z;
getopts('bc:hklnpt:u:z');
getopts('bc:hklnpt:u:xz');

# Check for implicit or incompatible arguments.
if ($opt_h)
Expand All @@ -119,6 +123,7 @@ $opt_l = 1 if ($opt_t);

die "Error: -b and -k are incompatible.\n" if ($opt_b && $opt_k);
die "Error: -l and -t are incompatible.\n" if ($opt_t && $opt_l);
warn "Warning: -x only meaningful if used with -b.\n" if ($opt_x) unless($opt_b);

my @list = @ARGV;

Expand Down Expand Up @@ -221,6 +226,10 @@ unless($opt_b)
exit 0;
}

# Power off switches if in bulk mode.
$Loader->{'power_off'} = 1;
$Loader->{'power_off'} = 0 if ($opt_x);

while (1)
{
# Bulk mode
Expand Down Expand Up @@ -280,6 +289,7 @@ sub process_switch
return($result);
}


sub report_error
{
if ($opt_k)
Expand All @@ -298,11 +308,13 @@ sub usage
# Display usage information

print STDOUT <<EOF;
switch_config_loader (-h | [-b] [-c <filename>] [-l] [-n] [-t <target>] [<switch_spec>])
switch_config_loader (-h | [-b] [-c <filename>] [-k] [-l] [-n] [-p] [-t <target>] [-u <username> ] [-x] [-z] [<switch_spec>])
-b -- Run in bulk mode, loop forever trying to install on each switch
attached. Implies -l, incompatible with -k.
NOTE: Will power off switch after config commit unless -x is specified.
-c <filename> -- Use <filename> to configure every affected switch
-h -- "Help" -- Print detailed usage information and exit. Supersedes
Expand All @@ -316,13 +328,21 @@ sub usage
-n -- No-Apply -- Send out the configuration, do the show | compare,
but do not apply the configuration, rollback instead.
-t <target> -- Specify the target. <target> can be a host name, an
-p -- Prompt for password
-t <target> -- Specify the target. <target> can be a host name, an
IP address, or a /dev/ name. If /dev/, then it is treated as a
Serial Port. Otherwise, treated as a Host Name or IP Address
and passed unchecked to library routines for connecting to the
switch. Implies -l.
<switch_spec> -- One or more switch names and/or groups
-u <username> -- Specify username for login prompt (serial) or SSH
-x -- Do not power off switch (only meaningful with -b)
-z
<switch_spec> -- One or more switch names and/or groups
Cannot be used with -l or any of the options that imply -l
unless it resolves to a single switch.
For more informoation on groups, see the "expand_switch_groups()"
Expand Down

0 comments on commit 1675a15

Please sign in to comment.