Question

I am trying to print a list of the devices that match my specific criteria. When I print everything to the screen, it works great. However, when I print it to a file it only prints one line. I am new to perl so any help would be appreciated. Thanks

$dbConnection = &openConnection();
# run the "list_device" command via the initial connection
my $device_list = $dbConnection->list_device();
foreach my $listDevices ($device_list->result()) {
    if (   ($listDevices->model !~ /PIX/)
        && ($listDevices->model !~ /ASA/)
        && ($listDevices->model !~ /ACE/)
        && ($listDevices->driverName !~ /Context/)
        && ($listDevices->hostName =~ /^ls1.*/i)
        && ($listDevices->vendor =~ /Cisco/)
    ) {
        #create device hash for LS
        $deviceHash{"deviceID"}         = $listDevices->deviceID;
        $deviceHash{"deviceType"}       = $listDevices->deviceType;
        $deviceHash{"vendor"}           = $listDevices->vendor;
        $deviceHash{"model"}            = $listDevices->model;
        $deviceHash{"primaryIPAddress"} = $listDevices > primaryIPAddress;
        $deviceHash{"hostName"}         = $listDevices->hostName;

        # mapping array
        my @returnData = (
            "deviceID",         "hostName",
            "primaryIPAddress", "deviceType",
            "vendor",           "model"
        );
        open OVERWRITE, ">overwrite.txt" or die $!            
        # loop through the hash and print out the device information
        foreach my $data (@returnData) {
            my $returnDataLength = @returnData;

            if (exists $deviceHash{$data}) {
                                print OVERWRITE $deviceHash{$data} . ",";
                                }
        }
        close OVERWRITE;
    }
    &closeConnection($dbConnection);
}
Was it helpful?

Solution

You are opening overwrite.txt for writing once per $data item. It gets overwritten each time. Move that outside of the loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top