So, over the last month I have been working on a script to do a master cleanup on the HP procurve switch. I talked to HP internal people and they use a completely different system than me, so they did not help. I believe that all this turned out. This is what my script should have done:
- Establishes a telnet connection to switch
- Reboots the switch through an SNMP trap for attached APC
- expects input from the switch to identify it in order to access the extended boot rom
- refers to extended bootrom.
- changes directory to compact flash card
- runs the list command on the switch and uploads it to an array
- starts foreach for each element of the array to determine what the firmware, file and folder are A) ignores the firmware B) adds each file to the main delete array C) runs the same foreach file in the files in the folders and adds them to the main delete array, and then to the folder (for deletion).
- starts foreach to delete all elements in the main delete array to clear the switch.
- Reboots and sets up basic switch information.
UPDATE
So, I could not get $telnet->buffer_empty;to work in 100% of cases, so I decided to make a call to the second list command and upload it to a junk file. Below is my script and so far it has been working on 10 switches that I tested. My night shift will break the switches and test this reset script.
, , script HP Procurve, , .
$| = 1;
use Net::Telnet;
use Net::SNMP;
use strict;
use CGI;
my $power_host = ('Power-APC');
my $power_port = ('Power-APC-Port');
my $system_name = ('system-name');
my $console_server = ('Console-Server');
my $console_mgmtA = ('Console-ManagementA');
my $match;
my ($garbage, $trash);
my $master_value;
my $telnet;
my @array;
my @master_array;
checkForNull("Console-Server",$console_server,1);
checkForNull("Console-ManagementA",$console_mgmtA,1);
checkForNull("Power_Host",$power_host,1);
checkForNull("Power_Port",$power_port,1);
checkForNull("System_Name", $system_name,1);
print("Please wait while $system_name resets.\n");
print("Please do not interact with any consoles open to this device\n");
print("The reset will take approximitly 5 minutes\n");
$telnet = new Net::Telnet (Timeout=>120, Errmode=>'return', Port => $console_mgmtA);
$telnet->open($console_server);
sleep(1);
print("Deleting configuration files.\n");
my ($snmp, $error) = Net::SNMP->session(
-hostname => shift || "$power_host",
-community => shift || 'private',
-port => shift || 161
);
if (!defined($snmp)) {
printf("ERROR: %s.\n", $error);
exit 1;
}
my $sysx = ".1.3.6.1.4.1.318.1.1.12.3.3.1.1.4.$power_port";
my $result = $snmp->set_request(
-varbindlist => [$sysx, INTEGER, '3']
);
$snmp->close;
$telnet->waitfor(match=> '/Profiles/');
$telnet->cmd(string => '0', prompt => '/=>/');
$telnet->cmd (string=> 'cd cfa0', prompt => '/=>/');
my $garbage_output = $telnet -> cmd (string => 'ls', prompt => '/=>/');
$telnet->buffer_empty;
undef @array;
@array = $telnet->cmd(string => 'ls', prompt => '/=>/');
foreach my $item (@array) {
chomp ($item);
if ($item=~ /\//) {
subfolders ($item);
push(@master_array, "rmdir $item");
}
elsif ($item =~ /btm.swi|secondary.swi/) {
}
else {
push(@master_array,"rm $item");
}
foreach $master_value (@master_array){
$telnet -> print ("$master_value");
}
$telnet->print('boot');
$telnet->put(chr(13));
sleep(120);
$telnet->waitfor(match=> '/continue/');
sleep (1);
$telnet->put(chr(13));
sleep (1);
$telnet->put(chr(13));
$telnet->waitfor(match=> '/continue/');
$telnet->put(chr(13));
$telnet->print('config');
$telnet->put(chr(13));
$telnet->print("hostname $system_name\n");
$telnet->print('lldp run');
$telnet->put(chr(13));
$telnet->print('wr mem');
$telnet->put(chr(13));
$telnet->print('boot');
$telnet->put(chr(13));
$telnet->print('y');
print("$system_name has been reset to factory settings.\n");
}
sub subfolders {
my @subfolder_contents;
my $subfolder = $_[0];
my $file;
@subfolder_contents = $telnet ->cmd(string => "ls $subfolder", prompt => '/=>/');
foreach $file (@subfolder_contents){
chomp ($file);
push(@master_array, "rm $subfolder$file");
}
};
sub checkForNull {
my $name = $_[0];
my $param = $_[1];
my $required = $_[2];
if($param eq "") {
if ($required == 1) {
print("\nSorry, no '$name' specified for this device, cannot continue.\n");
exit 1;
}
}
}
exit;