Viewing File: /usr/local/cpanel/whostmgr/bin/convertips

#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - whostmgr/bin/convertips                 Copyright 2022 cPanel, L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

BEGIN {
    $ENV{'LANG'} = 'C';
}

use strict;
use Cpanel::Ips ();

my $ETHDEV = `grep ETHDEV /etc/wwwacct.conf`;
chomp($ETHDEV);
if ( $ETHDEV =~ /\s+(\S+)$/ ) {
    $ETHDEV = $1;
}

my @IFCFG = split( /\n/, `/sbin/ip -4 addr show` );
if ( $ETHDEV =~ /^\s*$/ ) {
    foreach my $eth (@IFCFG) {
        if ( $eth =~ /^(\S+)\s+/ ) {
            $ETHDEV = $1;
            $ETHDEV =~ s/\:?$//;    # Legacy fix probably not needed
            last;
        }
    }
}

if ( $ETHDEV =~ /^\s*$/ ) {
    $ETHDEV = 'eth0';
}

my $ipok;
my @IPS;
foreach my $ip (@IFCFG) {
    if ( $ip =~ /^\d+\:\s+$ETHDEV:/ ) {
        $ipok = 1;
    }
    elsif ( $ip =~ /^\S+\s+/ ) {
        $ipok = 0;
    }

    if ( $ip =~ /inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d+)\s*/ && $ipok ) {
        my $ipalias = $1;
        my $prefix  = $2;
        my ( $ret, $mask ) = Cpanel::Ips::convert_prefix_to_quad($prefix);
        if ($ret) {
            $ipalias .= ':' . $mask;
        }
        else {
            $ipalias .= ':255.255.255.0';
        }
        if ( $ip =~ /\s+brd\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/ ) {
            $ipalias .= ':' . $1;
        }
        if ( $ipalias !~ /^127/ ) {
            push( @IPS, $ipalias );
        }
    }
}

open( my $ips_fh, ">/etc/ips" );
foreach my $ip (@IPS) {
    print $ips_fh "$ip\n";
}
close($ips_fh);
unlink("/etc/rc.d/rc.inet3");
system("rm -f /etc/sysconfig/network-scripts/ifcfg-$ETHDEV:*");
Back to Directory File Manager