Allow commenting out of hosts file entries

This commit is contained in:
Jamie Cameron
2019-07-03 01:35:33 -07:00
parent 9d54424523
commit 1b1579ad1b
6 changed files with 41 additions and 10 deletions

View File

@@ -77,3 +77,5 @@ Added support for IPv6, DHCP and new ethernet device names on FreeBSD.
Added support for configuring intefaces with the "ip" command, as seen on RHEL 7, CentOS 7 and other new Linux distributions. Added support for configuring intefaces with the "ip" command, as seen on RHEL 7, CentOS 7 and other new Linux distributions.
---- Changes since 1.760 ---- ---- Changes since 1.760 ----
On CentOS, Fedora and Redhat systems, the DNSn lines in ifcfg-* files are now updated in sync with resolv.conf. On CentOS, Fedora and Redhat systems, the DNSn lines in ifcfg-* files are now updated in sync with resolv.conf.
---- Changes since 1.910 ----
Added an option to disable (comment out) hosts file entries.

View File

@@ -7,6 +7,7 @@ $access{'hosts'} == 2 || &error($text{'hosts_ecannot'});
&ReadParse(); &ReadParse();
if ($in{'new'}) { if ($in{'new'}) {
&ui_print_header(undef, $text{'hosts_create'}, ""); &ui_print_header(undef, $text{'hosts_create'}, "");
$h = { 'active' => 1 };
} }
else { else {
&ui_print_header(undef, $text{'hosts_edit'}, ""); &ui_print_header(undef, $text{'hosts_edit'}, "");
@@ -20,6 +21,10 @@ print &ui_hidden("new", $in{'new'});
print &ui_hidden("idx", $in{'idx'}); print &ui_hidden("idx", $in{'idx'});
print &ui_table_start($text{'hosts_detail'}, undef, 2); print &ui_table_start($text{'hosts_detail'}, undef, 2);
# Active?
print &ui_table_row($text{'hosts_active'},
&ui_yesno_radio("active", $h->{'active'}));
# IP address # IP address
print &ui_table_row($text{'hosts_ip'}, print &ui_table_row($text{'hosts_ip'},
&ui_textbox("address", $h->{'address'}, 30)); &ui_textbox("address", $h->{'address'}, 30));

View File

@@ -221,6 +221,7 @@ dns_hoststoo=Update hostname in host addresses if changed?
dns_dhcp=Hostname is set by DHCP server? dns_dhcp=Hostname is set by DHCP server?
hosts_title=Host Addresses hosts_title=Host Addresses
hosts_active=Enabled?
hosts_ip=IP Address hosts_ip=IP Address
hosts_host=Hostnames hosts_host=Hostnames
hosts_add=Add a new host address. hosts_add=Add a new host address.

View File

@@ -16,16 +16,21 @@ if ($access{'hosts'} == 2) {
} }
print &ui_columns_start([ $access{'hosts'} == 2 ? ( "" ) : ( ), print &ui_columns_start([ $access{'hosts'} == 2 ? ( "" ) : ( ),
$text{'hosts_ip'}, $text{'hosts_ip'},
$text{'hosts_active'},
$text{'hosts_host'} ], undef, 0, \@tds); $text{'hosts_host'} ], undef, 0, \@tds);
foreach $h (&list_hosts()) { foreach $h (&list_hosts()) {
local @cols; local @cols;
local $lnk;
if ($access{'hosts'} == 2) { if ($access{'hosts'} == 2) {
push(@cols, "<a href=\"edit_host.cgi?idx=$h->{'index'}\">". $lnk = &ui_link("edit_host.cgi?idx=$h->{'index'}",
&html_escape($h->{'address'})."</a>"); &html_escape($h->{'address'}));
} }
else { else {
push(@cols, &html_escape($h->{'address'})); $lnk = &html_escape($h->{'address'});
} }
push(@cols, $lnk);
push(@cols, $h->{'active'} ? $text{'yes'}
: "<font color=red>$text{'no'}</font>");
push(@cols, join(" , ", map { &html_escape($_) } push(@cols, join(" , ", map { &html_escape($_) }
@{$h->{'hosts'}})); @{$h->{'hosts'}}));
if ($access{'hosts'} == 2) { if ($access{'hosts'} == 2) {

View File

@@ -39,14 +39,19 @@ local $line="";
&open_readfile(HOSTS, $config{'hosts_file'}); &open_readfile(HOSTS, $config{'hosts_file'});
while($line=<HOSTS>) { while($line=<HOSTS>) {
local $comment = 0;
$line =~ s/\r|\n//g; $line =~ s/\r|\n//g;
if ($line =~ s/^\s*#+\s*//) {
$comment = 1;
}
$line =~ s/#.*$//g; $line =~ s/#.*$//g;
$line =~ s/\s+$//g; $line =~ s/\s+$//g;
local(@f)=split(/\s+/, $line); local @f = split(/\s+/, $line);
local($ipaddr)=shift(@f); local $ipaddr = shift(@f);
if (check_ipaddress_any($ipaddr)) { if (check_ipaddress_any($ipaddr)) {
push(@rv, { 'address' => $ipaddr, push(@rv, { 'address' => $ipaddr,
'hosts' => [ @f ], 'hosts' => [ @f ],
'active' => !$comment,
'line', $lnum, 'line', $lnum,
'index', scalar(@rv) }); 'index', scalar(@rv) });
} }
@@ -56,12 +61,22 @@ close(HOSTS);
return @rv; return @rv;
} }
# make_host_line(&host)
# Internal function to return a line for the hosts file
sub make_host_line
{
local ($host) = @_;
return ($host->{'active'} ? "" : "# ").
$host->{'address'}."\t".join(" ",@{$host->{'hosts'}})."\n";
}
# create_host(&host) # create_host(&host)
# Add a new host to /etc/hosts # Add a new host to /etc/hosts
sub create_host sub create_host
{ {
local ($host) = @_;
&open_tempfile(HOSTS, ">>$config{'hosts_file'}"); &open_tempfile(HOSTS, ">>$config{'hosts_file'}");
&print_tempfile(HOSTS, $_[0]->{'address'},"\t",join(" ",@{$_[0]->{'hosts'}}),"\n"); &print_tempfile(HOSTS, &make_host_line($host));
&close_tempfile(HOSTS); &close_tempfile(HOSTS);
} }
@@ -69,16 +84,17 @@ sub create_host
# Update the address and hosts of a line in /etc/hosts # Update the address and hosts of a line in /etc/hosts
sub modify_host sub modify_host
{ {
local ($host) = @_;
&replace_file_line($config{'hosts_file'}, &replace_file_line($config{'hosts_file'},
$_[0]->{'line'}, $_[0]->{'line'}, &make_host_line($host));
$_[0]->{'address'}."\t".join(" ",@{$_[0]->{'hosts'}})."\n");
} }
# delete_host(&host) # delete_host(&host)
# Delete a host from /etc/hosts # Delete a host from /etc/hosts
sub delete_host sub delete_host
{ {
&replace_file_line($config{'hosts_file'}, $_[0]->{'line'}); local ($host) = @_;
&replace_file_line($config{'hosts_file'}, $host->{'line'});
} }
# list_ipnodes() # list_ipnodes()

View File

@@ -26,7 +26,8 @@ else {
if ($in{'new'}) { if ($in{'new'}) {
# saving a host # saving a host
$host = { 'address' => $in{'address'}, $host = { 'address' => $in{'address'},
'hosts' => \@h }; 'hosts' => \@h,
'active' => $in{'active'} };
&create_host($host); &create_host($host);
} }
else { else {
@@ -34,6 +35,7 @@ else {
$host = $hosts[$in{'idx'}]; $host = $hosts[$in{'idx'}];
$host->{'address'} = $in{'address'}; $host->{'address'} = $in{'address'};
$host->{'hosts'} = \@h; $host->{'hosts'} = \@h;
$host->{'active'} = $in{'active'};
&modify_host($host); &modify_host($host);
} }
} }