Script pour changer le password d'une antenne UBNT

De Société Coopérative d'Aménagement Numérique Icaunaise
Aller à la navigation Aller à la recherche
#!/usr/bin/perl
use FindBin qw($Bin $Script);
use WWW::Mechanize;

die "Syntax: $Script      
 ...
Changes the password on 1 or more AirOS units.
" unless @ARGV >= 6;

my $user      = shift @ARGV;
my $op        = shift @ARGV;
my $np        = shift @ARGV;
my $rouser    = shift @ARGV;
my $ropass    = shift @ARGV;
my @addresses = @ARGV;

open L, ">>$Bin/$Script.log" or die "Unable to write to $Bin.log: $!";

sub l {
    print STDERR @_;
    print L @_;
}

for my $a (@addresses) {

    l "Changing password on $a\n";

    my $mech = WWW::Mechanize->new();
    my $entry;
    my $start = "http://$a/login.cgi?uri=/system.cgi";

    $mech->get($start);
    $mech->field('username',$user);
    $mech->field('password',$op);
    $response = $mech->submit(); # to get login cookie

    if (!$response->is_success) {
         l $response->status_line, "\n";
    }

    $mech->get(qq|http://$a/system.cgi|);
    $mech->field('NewPassword',$np);
    $mech->field('NewPassword2',$np);
    $mech->field('OldPassword',$op);
    $mech->field('ro_status', "enabled");
    $mech->field('rousername', $rouser);
    $mech->field('roPassword', $ropass);
    $mech->field('hasRoPassword', "true");
    $mech->click_button(name => "change");

    $response = $mech->submit();

    if (!$response->is_success) {
        l $response->status_line, "\n";
    }

    $response = $mech->get(qq|http://$a/apply.cgi|);

    if (!$response->is_success) {
         l $response->status_line, "\n";
    }


}

close L;

exit 0;