auto lo
iface lo inet loopback
auto eth0 <--This line needs eth1, too!
iface eth0 inet dhcp
iface eth1 inet static
address 192.168.0.4
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1
####
my $sth = $dbh->prepare( "SELECT host, name, type, address, netmask, gateway FROM $table_name" );
#(PRINTED)
app17, eth1, static, 192.168.0.2, 255.255.255.0, 192.168.0.1
app15, eth0, dhcp, , ,
app15, eth1, static, 192.168.0.4, 255.255.255.0, 192.168.0.1
app12, eth1, static, 192.168.0.5, 255.255.255.0, 192.168.0.1
####
my @ifaces;
my @hosts;
while ( my @row = $sth->fetchrow_array )
{
my $host = $row[0];
my $iface = $row[1];
my $type = $row[2];
my $address = $row[3];
my $netmask = $row[4];
my $gateway = $row[5];
push( @hosts, "$host" );
########### Here's what I can't get right #############
while ( @hosts )
{
push( @ifaces, "$iface");
@ifaces = pop( @ifaces );
last;
}
########### Above is what I can't get right #############
my $tmp_file = "/tmp/interfaces.$host";
my $loopback_string = "auto lo\niface lo inet loopback\n\nauto";
open(my $interfaces_file,"+>>$tmp_file") or die "Unable to open $tmp_file: $!\n";
if ( -z $tmp_file )
{
print {$interfaces_file} "$loopback_string @ifaces\n\n";
}
if ($type eq "dhcp")
{
print {$interfaces_file} "iface $iface inet $type\n\n";
}
else
{
print {$interfaces_file} "iface $iface inet $type\naddress $address\nnetmask $netmask\nnetwork 192.168.0.0\nbroadcast 192.168.0.255\ngateway $gateway\n";
close($interfaces_file) or die "Can't close it\n";
}
}
$dbh->disconnect;