#! /usr/bin/perl

$AP_CMD_FILE = "/tmp/cli/ap_cmd.$$";

# Open the 
open IOSCLI, "/opt/meru/bin/ioscli -c \"show ap\" |" or die "Cannot run \"show ap\" command\n";

# Delete the file if existed 
unlink $AP_CMD_FILE or die "Cannot delete the existing AP command file\n" if -e "$AP_CMD_FILE";
open CMD_OUTFILE, ">$AP_CMD_FILE" or die "Can not create AP command file: $!\n";

# Write AP command into scripts file
while (<IOSCLI>)  {
    chomp;

    next if /^\s*$/;

    if (/^(\d+)\s+\S+\s+(\S+)\s+/) {
	my ($apid, $serial) = ($1, $2);
	next if !defined $apid;
	next if !defined $serial;

	print CMD_OUTFILE "show ap $apid\n";
	#print STDOUT "$apid $serial\n"
    }
}

# Close the AP command file
close CMD_OUTFILE;

# Close the pipe
close IOSCLI;

# Run the command scripts file
system  "/opt/meru/bin/ioscli -f $AP_CMD_FILE";

# Remove the command script file
unlink $AP_CMD_FILE;


