#! /usr/bin/perl

$ESSID_CMD_FILE = "/tmp/cli/essid_cmd.$$";

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

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

# Skip the headers
scalar <IOSCLI>;
scalar <IOSCLI>;
scalar <IOSCLI>;

while (<IOSCLI>)  {
    chomp;

    # skip the blank line
    next if /^\s*$/;

    /^(\S+)\s+\S+/;
    next if !defined $1;

    print CMD_OUTFILE "show essid $1\n";
    #print "-- ESSID-Name: $1\n";
}

# Close the AP command file
close CMD_OUTFILE;

# Close the WIF command file
close IOSCLI;

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

# Remove the command script file
unlink $ESSID_CMD_FILE or die "Cannot delete ESSID command file\n";
