#!/usr/bin/perl -w # hosts.cgi: returns a JSON form of /etc/hosts # Raphael Finkel 10/2020 use strict; use JSON; # constants my $numCols = 4; open HOSTS, "/etc/hosts" or die("Cannot open /etc/hosts"); my $answer = []; while (my $line = ) { next if $line =~ /^#/; # comments my @parts = split (/\s+/, $line); splice @parts, $numCols, 100; while (@parts < $numCols) { push @parts, ''; # make sure there are $numCols elements } push @$answer, \@parts; } # one host close HOSTS; print "Content-Type: application/json; charset=utf-8\n"; print "\n"; # separator before content my $json = JSON::encode_json({'data' => $answer}); print $json;